I am trying to add a util to my package where a subclass will inherit the superclasses's docstring. Here is what I set up so far.
from functools import wraps
import typing as t
def inherit_parent_docs(klass):
@wraps(klass)
def inside(*args: t.Any, **kwargs: t.Any):
parent_doc = B.__bases__[0].__doc__
klass.__doc__ = parent_doc
klass_out = klass(*args, **kwargs)
return klass_out
return inside
class A:
"""super"""
@inherit_parent_docs
class B(A):
"""sub"""
def __init__(self):
pass
However, this does not work because it says B
doesn't have bases
dunder because it thinks its a function. And I checked, strangely enough, type(B)
is function
. Also I tried wrapping the constructor, but then the docstring is only updated when instantiated.
I feel like I am missing something obvious here...
CodePudding user response:
So that is what decoration does. It assigned the function, inside
to B
.
So when you
@inherit_parent_docs
class B(A):
...
That is syntactic sugar for:
class B(A):
...
B = inherit_parent_docs(B)
Note, you could just not return a wrapper
. Return the Klass
after you've modified it:
def inherit_parent_docs(klass):
doc = klass.__doc__
parent_doc = klass.__bases__[0].__doc__
klass.__doc__ = f"{doc}\n{parent_doc}\n"
return klass