Skip to content

Docstring inheritance #17

Open
Open
@Chilipp

Description

@Chilipp

Hey!

There is a new feature that I would like to discuss for docrep and I'd very much like your feedback (pinging @jgostick, @mennthor, @amnona, @lesteve). It's about docstring inheritance (i.e. the docrep.DocstringProcessor inserts parameters based on the base class).

Wouldn't it be nice to have something like

class A:

    def my_method(self, a=1):
        """Do something awesome

        Parameters
        ----------
        a: int
            A number

        Returns
        -------
        int
            The changed number
        """"
        return a + 1


@docstrings.resolve_inheritance
class B:

    @docstrings.inherit_parameters
    @docstring.inherit_returns
    def my_method(self, a=1):
        """Do something better"""
        return a + 2

and then have B.my_method the same docstring as A.my_method? inherit_parameters and inherit_returns would mark the method to be processed then in resolve_inheritance (Note: this separate treatment is necessary as, during class compilation, B is not yet defined and we therefore cannot resolve that B inherits A within inherit_parameters).

I'd like to discuss with you, how this should be set up (besides the implementation discussed above). Here are some ideas that I have and I highly appreciate your feedback and suggestions 😃

Specifying the base class

We could extend this by optionally specifying the base class, e.g. via

def funca(a=1):
    """"Do something

    Parameters
    ----------
    a: int
        The input
    """"
    return a+ 1

@docstrings.resolve_inheritance
@docstrings.inherit_parameters(funca)
def funcb(a=1):
    """Do something else"""
    return a + 2

This would then make it available for functions as well.

Replace arguments

We could also replace arguments in the sub class method (see alsp #16), e.g.

def funca(a=1, b=2):
    """"Do something

    Parameters
    ----------
    a: int
        The input (Default 1)
    b: int
        Another input (Default 2).
    """"
    return a+ b

@docstrings.resolve_inheritance
@docstrings.inherit_parameters(funca)
def funcb(a=3, b=2):
    """Do something else

    Parameters
    ----------
    a: int
        The input (Default 3)"""
    return a + 2

Specify parameters

I would in general use inspect.getfullargspec and match the parameters of the function with the parameters of base. But it should also have the possibility to specify them manually, e.g. to resolve stuff like *args, **kwargs. Here is an example

@docstrings.resolve_inheritance
@docstrings.inherit_parameters(funca, params=['a'])
def funcb(*args):
    """Do something else

    Parameters
    ----------
    b: int
        The input (Default 3)"""
    pass

funcb in this case should have used the docstring of parameter a although it is not in the argspec of funcb.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions