防止sphinx执行继承的doctests

The*_*ius 5 python doctest numpy python-sphinx

我们创建了一个大量使用(带继承)numpy的MaskedArrays的库.但是我想在make doctest不测试numpy的继承方法的情况下运行sphinx ,因为它们会导致大约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)
Run Code Online (Sandbox Code Playgroud)

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

min = _frommethod('min')
max = _frommethod('max')
...
Run Code Online (Sandbox Code Playgroud)

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

任何人都知道如何防止sphinx执行这个"继承"的doctests?

The*_*ius 2

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

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
Run Code Online (Sandbox Code Playgroud)

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