Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

prevent sphinx from executing inherited doctests

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?

like image 964
Themerius Avatar asked Nov 03 '22 23:11

Themerius


1 Answers

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!

like image 97
Themerius Avatar answered Nov 15 '22 04:11

Themerius