gpt4 book ai didi

python - 防止 sphinx 执行继承的 doctests

转载 作者:太空狗 更新时间:2023-10-30 01:30:01 24 4
gpt4 key购买 nike

我们制作了一个大量使用(通过继承)numpy 的 MaskedArrays 的库。但是我想运行 sphinx 的 make doctest 而不测试从 numpy 继承的方法,因为它们会导致大约 100 次失败。

这看起来像这样:

class _frommethod:
"""
Adapted from numpy.ma._frommethod
"""

def __init__(self, func_name):
self.__name__ = func_name
self.__doc__ = getattr(MaskedArray, func_name).__doc__
self.obj = None

def __get__(self, obj, objtype=None):
self.obj = obj
return self

def __call__(self, a, *args, **params):
# Get the method from the array (if possible)
method_name = self.__name__
method = getattr(a, method_name, None)
if method is not None:
return method(*args, **params)
# Still here ? Then a is not a MaskedArray
method = getattr(MaskedTimeData, method_name, None)
if method is not None:
return method(MaskedTimeData(a), *args, **params)
# Still here ? OK, let's call the corresponding np function
method = getattr(np, method_name)

现在我们的库也支持 numpy 的函数,因此我们使用:

min = _frommethod('min')
max = _frommethod('max')
...

如果我禁用 self.__doc__ = getattr(MaskedArray, func_name).__doc__make doctest 的失败就会消失。但我想保留继承的文档;这样用户仍然可以在 ipython 中使用 mylibrary.min?

有人知道如何防止 sphinx 执行这个“继承的”doctests 吗?

最佳答案

我现在使用这个解决方案:

def _dont_doctest_inherited_docstrings(docstring):
docstring_disabled = ""
for line in docstring.splitlines():
docstring_disabled += line + "#doctest: +DISABLE"
return docstring_disabled

class _frommethod:
"""
Adapted from numpy.ma._frommethod
"""

def __init__(self, func_name):
self.__name__ = func_name
docstring = getattr(MaskedArray, func_name).__doc__
self.__doc__ = _dont_doctest_inherited_docstrings(docstring)
self.obj = None

也许有人有更聪明的方法!

关于python - 防止 sphinx 执行继承的 doctests,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10675198/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com