It has been a while since I started looking for a way to implement this. I have already come across several discussions and ideas about it, with the most relevant/recent one being in this topic:
The topic clearly explains what we are trying to implement, but I would like to expand upon the example given there. I believe that the @dataclass
decorator already solves the example concerning the __init__
function. My proposal is a decorator that functions as follows:
class A:
def fn(self, param1: int, param2: str):
"""
:param param1: Documentation for param1
:param param2: Documentation for param2
"""
# do something
class B(A):
@extends(A.fn) # or only @extends
def fn(self, param3: int, *args, **kwargs):
"""
:param param3: Documentation for param3
"""
super().fn(*args, **kwargs) # args/kwargs should contain param1 and param2
The fn
method in B
expects to receive exactly the same arguments as fn
in A
, plus the specific arguments added in B
. This would even be a generalization of what is discussed on this topic:
The @copy_signature
that is discussed there would only be the case when no extra arguments are added.
Currently, we can implement this to work in runtime environments, like Jupyter Notebooks, by manipulating the signature of each function. However, this approach does not generalize well to static type checkers in IDEs.
As I mentioned, it seems to me that the @dataclass
decorator has successfully implemented this behavior. For example:
@dataclass
class A:
param1: int = 1
param2: float = 0.4
@dataclass
class B(A):
param3: int = 2
An instance of class B
will have self.param1
, self.param2
, and self.param3
as attributes, and when instantiating it in an IDE, it will correctly show that it takes param1
, param2
, and param3
to be instantiated. Unfortunately, it appears that the documentation is still not fully functional, but avoiding the need to copy/paste some parameters is already a significant improvement!
So, as of today, do we have any workaround for this pattern? Do you think we could implement something similar to the @dataclass
decorator?