Skip to content

POC: docstring inheritance #31110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,24 @@ def _ensure_type(self: T, obj) -> T:
assert isinstance(obj, type(self)), type(obj)
return obj

@classmethod
def __init_subclass__(cls):
"""Automatically inherit docstrings."""
import types

for name, attr in cls.__dict__.items():
if isinstance(attr, types.FunctionType):
# TODO: property/cache_readonly?
if attr.__doc__ is None:
for parent in cls.__mro__:
if parent is cls:
continue
if hasattr(parent, name):
sup = getattr(parent, name)
if sup.__doc__ is not None:
attr.__doc__ = sup.__doc__
break


class NoNewAttributesMixin:
"""Mixin which prevents adding new attributes.
Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/base/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import pandas._testing as tm
from pandas.core.accessor import PandasDelegate
from pandas.core.base import NoNewAttributesMixin, PandasObject
from pandas.core.indexes.extension import ExtensionIndex


class TestPandasDelegate:
Expand Down Expand Up @@ -140,3 +141,7 @@ def test_constructor_datetime_outofbound(self, a, klass):
# Forced conversion fails for all -> all cases raise error
with pytest.raises(pd.errors.OutOfBoundsDatetime):
klass(a, dtype="datetime64[ns]")


def test_docstring_inheritance():
assert ExtensionIndex.dropna.__doc__ == Index.dropna.__doc__