Skip to content

Commit

Permalink
fix: inheritance of method docstrings with multiple parents
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine DECHAUME committed Jan 15, 2023
1 parent e4d341e commit ed60d96
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 29 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.1.0] - 2022-11
## [1.1.1] - 2023-01
### Fixed
- Docstring inheritance for methods with multiple parents.

## [1.1.0] - 2023-01
### Added
- Support class docstrings with `__init__` signature description.
### Changed
- Some performance improvements.
### Fixed
- Meta-classes API no longer leaks into the classes they create.
- Metaclasses API no longer leaks into the classes they create.

## [1.0.1] - 2022-11
### Added
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ from docstring_inheritance import NumpyDocstringInheritanceMeta


class Parent(metaclass=NumpyDocstringInheritanceMeta):
def meth(self, x, y=None):
def method(self, x, y=None):
"""Parent summary.
Parameters
Expand All @@ -99,7 +99,7 @@ class Parent(metaclass=NumpyDocstringInheritanceMeta):


class Child(Parent):
def meth(self, x, z):
def method(self, x, z):
"""
Parameters
----------
Expand All @@ -117,7 +117,7 @@ class Child(Parent):


# The inherited docstring is
Child.meth.__doc__ == """Parent summary.
Child.method.__doc__ == """Parent summary.
Parameters
----------
Expand Down Expand Up @@ -296,7 +296,7 @@ from docstring_inheritance import GoogleDocstringInheritanceMeta


class Parent(metaclass=GoogleDocstringInheritanceMeta):
def meth(self, w, x, y):
def method(self, w, x, y):
"""
Args:
w: Description for w
Expand All @@ -306,7 +306,7 @@ class Parent(metaclass=GoogleDocstringInheritanceMeta):


class Child(Parent):
def meth(self, w, y, z):
def method(self, w, y, z):
"""
Args:
z: Description for z
Expand All @@ -315,7 +315,7 @@ class Child(Parent):


# The inherited docstring is
Child.meth.__doc__ == """
Child.method.__doc__ == """
Args:
w: Description for w
y: Overridden description for y
Expand Down
6 changes: 1 addition & 5 deletions src/docstring_inheritance/class_docstrings_inheritor.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,7 @@ def _inherit_attrs_docstrings(
continue
parent_doc = method.__doc__
if parent_doc is not None:
break
else:
continue

self._docstring_inheritor(parent_doc, attr)
self._docstring_inheritor(parent_doc, attr)

@staticmethod
def _create_dummy_func_with_doc(docstring: str | None) -> Callable[..., Any]:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_base_inheritor.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def func_all(arg1, arg2=None, *varargs, **varkw):
"func,section_items,expected",
[
(func_none, {}, {}),
# Non existing args are removed.
# Non-existing args are removed.
(func_none, {"arg": ""}, {}),
# Self arg is removed.
(func_with_self, {"self": ""}, {}),
Expand Down
6 changes: 3 additions & 3 deletions tests/test_metaclass_google.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
@parametrize_inheritance
def test_args_inheritance(inheritance_class):
class Parent(metaclass=inheritance_class):
def meth(self, w, x, *args, y=None, **kwargs):
def method(self, w, x, *args, y=None, **kwargs):
"""
Args:
w
Expand All @@ -44,7 +44,7 @@ def meth(self, w, x, *args, y=None, **kwargs):
"""

class Child(Parent):
def meth(self, xx, x, *args, yy=None, y=None, **kwargs):
def method(self, xx, x, *args, yy=None, y=None, **kwargs):
"""
Args:
xx: int
Expand All @@ -59,7 +59,7 @@ def meth(self, xx, x, *args, yy=None, y=None, **kwargs):
y: float
**kwargs: int"""

assert Child.meth.__doc__ == excepted
assert Child.method.__doc__ == excepted


@parametrize_inheritance
Expand Down
72 changes: 62 additions & 10 deletions tests/test_metaclass_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ def assert_args_inheritance(cls):
y: float
**kwargs: int"""

assert cls.meth.__doc__ == excepted
assert cls.method.__doc__ == excepted


@parametrize_inheritance
def test_args_inheritance_parent_meta(inheritance_class):
class Parent(metaclass=inheritance_class):
def meth(self, w, x, *args, y=None, **kwargs):
def method(self, w, x, *args, y=None, **kwargs):
"""
Parameters
----------
Expand All @@ -62,7 +62,7 @@ def meth(self, w, x, *args, y=None, **kwargs):
"""

class Child(Parent):
def meth(self, xx, x, *args, yy=None, y=None, **kwargs):
def method(self, xx, x, *args, yy=None, y=None, **kwargs):
"""
Parameters
----------
Expand All @@ -75,7 +75,7 @@ def meth(self, xx, x, *args, yy=None, y=None, **kwargs):
@parametrize_inheritance
def test_args_inheritance_child_meta(inheritance_class):
class Parent:
def meth(self, w, x, *args, y=None, **kwargs):
def method(self, w, x, *args, y=None, **kwargs):
"""
Parameters
----------
Expand All @@ -87,7 +87,7 @@ def meth(self, w, x, *args, y=None, **kwargs):
"""

class Child(Parent, metaclass=inheritance_class):
def meth(self, xx, x, *args, yy=None, y=None, **kwargs):
def method(self, xx, x, *args, yy=None, y=None, **kwargs):
"""
Parameters
----------
Expand All @@ -97,7 +97,7 @@ def meth(self, xx, x, *args, yy=None, y=None, **kwargs):
assert_args_inheritance(Child)


def assert_missing_attr(cls):
def assert_docstring(cls):
excepted = "Summary"

assert cls.method.__doc__ == excepted
Expand Down Expand Up @@ -127,7 +127,7 @@ def staticmethod():
def prop(self):
"""Summary"""

assert_missing_attr(Child)
assert_docstring(Child)


@parametrize_inheritance
Expand All @@ -151,7 +151,7 @@ def staticmethod():
def prop(self):
"""Summary"""

assert_missing_attr(Child)
assert_docstring(Child)


@parametrize_inheritance
Expand Down Expand Up @@ -188,7 +188,7 @@ def staticmethod():
def prop(self):
"""Summary"""

assert_missing_attr(Child)
assert_docstring(Child)


@parametrize_inheritance
Expand Down Expand Up @@ -225,7 +225,7 @@ def staticmethod():
def prop(self):
"""Summary"""

assert_missing_attr(Child)
assert_docstring(Child)


def assert_multiple_inheritance(cls):
Expand Down Expand Up @@ -307,6 +307,58 @@ class Child(Parent1, Parent2, metaclass=inheritance_class):
assert_multiple_inheritance(Child)


@parametrize_inheritance
def test_multiple_inheritance_child_meta_method(inheritance_class):
class Parent1:
def method(self, w, x):
"""Summary 1
Parameters
----------
w: w doc
x: x doc
Returns
-------
int
"""

class Parent2:
def method(self, *args, y=None, **kwargs):
"""Summary 2
Parameters
----------
*args: int
y: float
**kwargs: int
Returns
-------
float
"""

class Child(Parent1, Parent2, metaclass=inheritance_class):
def method(self, w, x, *args, y=None, **kwargs):
pass

excepted = """Summary 1
Parameters
----------
w: w doc
x: x doc
*args: int
y: float
**kwargs: int
Returns
-------
int"""

assert Child.method.__doc__ == excepted


@parametrize_inheritance
def test_several_parents_parent_meta(inheritance_class):
class GrandParent(metaclass=inheritance_class):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_numpy_inheritor.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def test_parse_one_section(line1, line2s, expected):
assert NumpyDocstringInheritor._parse_one_section(line1, line2s, []) == expected


# The following are test for methods of AbstractDocstringInheritor that depend
# The following are test for methods of AbstractDocstringInheritor that depend on
# concrete implementation of abstract methods.


Expand Down Expand Up @@ -215,7 +215,7 @@ def test_parse_one_section(line1, line2s, expected):
{"Parameters": {}},
{"Parameters": {}},
),
# Standard section_items names come before non standard ones.
# Standard section_items names come before non-standard ones.
({0: 0, "Notes": 0}, {}, {"Notes": 0, 0: 0}),
],
)
Expand Down

0 comments on commit ed60d96

Please sign in to comment.