防止sphinx执行继承的doctests

时间:2012-05-20 16:32:03

标签: python numpy python-sphinx doctest

我们创建了一个大量使用(带继承)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?

1 个答案:

答案 0 :(得分: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

也许某人有更聪明的方式!