Skip to content

Commit 0455954

Browse files
author
AntoineD
committed
Code clean up
1 parent 5b1872f commit 0455954

File tree

3 files changed

+34
-37
lines changed

3 files changed

+34
-37
lines changed

src/docstring_inheritance/class_docstrings_inheritor.py

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from types import WrapperDescriptorType
2626
from typing import TYPE_CHECKING
2727
from typing import Any
28+
from typing import Final
2829

2930
from docstring_inheritance.docstring_inheritors.bases.inheritor import (
3031
BaseDocstringInheritor,
@@ -39,14 +40,14 @@
3940
class ClassDocstringsInheritor:
4041
"""A class for inheriting class docstrings."""
4142

42-
_cls: type
43+
__cls: type
4344
"""The class to process."""
4445

45-
_docstring_inheritor: DocstringInheritorClass
46+
__docstring_inheritor: DocstringInheritorClass
4647
"""The docstring inheritor."""
4748

4849
_init_in_class: bool
49-
"""Whether the ``__init__`` arguments documentation is in the class docstring."""
50+
"""Whether the `__init__` arguments documentation is in the class docstring."""
5051

5152
__mro_classes: list[type]
5253
"""The MRO classes."""
@@ -63,14 +64,14 @@ def __init__(
6364
Args:
6465
cls: The class to process.
6566
docstring_inheritor: The docstring inheritor.
66-
init_in_class: Whether the ``__init__`` arguments documentation is in the
67+
init_in_class: Whether the `__init__` arguments documentation is in the
6768
class docstring.
6869
""" # noqa: D205, D212
6970
# Remove the new class itself and the object class from the mro,
7071
# object's docstrings have no interest.
7172
self.__mro_classes = cls.mro()[1:-1]
72-
self._cls = cls
73-
self._docstring_inheritor = docstring_inheritor
73+
self.__cls = cls
74+
self.__docstring_inheritor = docstring_inheritor
7475
self._init_in_class = init_in_class
7576

7677
@classmethod
@@ -89,31 +90,29 @@ def inherit_docstrings(
8990
class docstring.
9091
"""
9192
inheritor = cls(class_, docstring_inheritor, init_in_class)
92-
inheritor._inherit_attrs_docstrings()
93-
inheritor._inherit_class_docstring()
93+
inheritor.__inherit_methods_docstrings()
94+
inheritor.__inherit_class_docstring()
9495

95-
def _inherit_class_docstring(
96-
self,
97-
) -> None:
96+
def __inherit_class_docstring(self) -> None:
9897
"""Create the inherited docstring for the class docstring."""
9998
func = None
10099
old_init_doc = None
101100
init_doc_changed = False
102101

103102
if self._init_in_class:
104-
init_method: Callable[..., None] = self._cls.__init__ # type: ignore[misc]
103+
init_method: Callable[..., None] = self.__cls.__init__ # type: ignore[misc]
105104
# Ignore the case when __init__ is from object since there is no docstring
106105
# and its __doc__ cannot be assigned.
107106
if not isinstance(init_method, WrapperDescriptorType):
108107
old_init_doc = init_method.__doc__
109-
init_method.__doc__ = self._cls.__doc__
108+
init_method.__doc__ = self.__cls.__doc__
110109
func = init_method
111110
init_doc_changed = True
112111

113112
if func is None:
114-
func = self._create_dummy_func_with_doc(self._cls.__doc__)
113+
func = _create_dummy_func_with_doc(self.__cls.__doc__)
115114

116-
docstring_inheritor = self._docstring_inheritor(func)
115+
docstring_inheritor = self.__docstring_inheritor(func)
117116

118117
for parent_cls in self.__mro_classes:
119118
# As opposed to the attribute inheritance, and following the way a class is
@@ -123,15 +122,13 @@ def _inherit_class_docstring(
123122

124123
docstring_inheritor.render()
125124

126-
self._cls.__doc__ = func.__doc__
125+
self.__cls.__doc__ = func.__doc__
127126

128127
if self._init_in_class and init_doc_changed:
129128
init_method.__doc__ = old_init_doc
130129

131-
def _inherit_attrs_docstrings(
132-
self,
133-
) -> None:
134-
"""Create the inherited docstrings for the class attributes."""
130+
def __inherit_methods_docstrings(self) -> None:
131+
"""Create the inherited docstrings for the class methods."""
135132
mro_classes = self.__mro_classes
136133
object_init_doc = self.__object_init_doc
137134
init_method_name = "__init__"
@@ -163,19 +160,19 @@ def _inherit_attrs_docstrings(
163160

164161
docstring_inheritor.render()
165162

166-
@staticmethod
167-
def _create_dummy_func_with_doc(docstring: str | None) -> Callable[..., Any]:
168-
"""Create a dummy function with a given docstring.
169163

170-
Args:
171-
docstring: The docstring to be assigned.
164+
def _create_dummy_func_with_doc(docstring: str | None) -> Callable[..., Any]:
165+
"""Create a dummy function with a given docstring.
172166
173-
Returns:
174-
The function with the given docstring.
175-
"""
167+
Args:
168+
docstring: The docstring to be assigned.
169+
170+
Returns:
171+
The function with the given docstring.
172+
"""
176173

177-
def func() -> None: # pragma: no cover
178-
pass
174+
def func() -> None: # pragma: no cover
175+
pass
179176

180-
func.__doc__ = docstring
181-
return func
177+
func.__doc__ = docstring
178+
return func

src/docstring_inheritance/docstring_inheritors/bases/inheritor.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
def get_similarity_ratio(env_ratio: str | None) -> float:
4949
"""Check the value of the similarity ratio.
5050
51-
If the passed ratio is ``None`` then the default value of 0. is returned.
51+
If the passed ratio is `None` then the default value of 0. is returned.
5252
5353
Args:
5454
env_ratio: The raw value of the ratio from the environment variable.
@@ -314,8 +314,8 @@ def _filter_args_section(
314314
) -> SubSectionType:
315315
"""Filter the args section items with the args of a signature.
316316
317-
The argument ``self`` is removed. The arguments are ordered according to the
318-
signature of ``func``. An argument of ``func`` missing in ``section_items`` gets
317+
The argument `self` is removed. The arguments are ordered according to the
318+
signature of `func`. An argument of `func` missing in `section_items` gets
319319
a default description defined in :attr:`._MISSING_ARG_TEXT`.
320320
321321
Args:

tests/test_inheritance_for_functions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
from docstring_inheritance import inherit_google_docstring
2727
from docstring_inheritance import inherit_numpy_docstring
28-
from docstring_inheritance.class_docstrings_inheritor import ClassDocstringsInheritor
28+
from docstring_inheritance.class_docstrings_inheritor import _create_dummy_func_with_doc
2929

3030

3131
def test_side_effect():
@@ -141,6 +141,6 @@ def child(x, missing_doc, *child_varargs, **child_kwargs):
141141
def test_simple(
142142
inherit_docstring, parent_docstring, child_docstring, expected_docstring
143143
):
144-
dummy_func = ClassDocstringsInheritor._create_dummy_func_with_doc(child_docstring)
144+
dummy_func = _create_dummy_func_with_doc(child_docstring)
145145
inherit_docstring(parent_docstring, dummy_func)
146146
assert dummy_func.__doc__ == expected_docstring

0 commit comments

Comments
 (0)