5

We've made a library which uses massively (with inheritance) numpy's MaskedArrays. But I want to run sphinx's make doctest without testing the inherited methods from numpy, because they make round about 100 failures.

This lookls like this:

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)

And now that our library also supports numpy's functions, therefore we use:

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

If I disable self.__doc__ = getattr(MaskedArray, func_name).__doc__, the failures of make doctest disappear. But I'd like to retain the inherited documentation; so that the users can still use mylibrary.min? in ipython.

Anyone an idea how I can prevent sphinx from executing this "inherited" doctests?

4

1 回答 1

2

I've use this solution now:

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

Maybe someone has a smarter way!

于 2012-06-01T16:06:51.163 回答