-
Notifications
You must be signed in to change notification settings - Fork 4
Description
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 + 2and 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 + 2This 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 + 2Specify 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)"""
passfuncb in this case should have used the docstring of parameter a although it is not in the argspec of funcb.