Skip to content
Snippets Groups Projects
Commit 38e4e525 authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

Merge branch 'master' into develop

parents 0bae016c a2d62d3f
No related branches found
No related tags found
No related merge requests found
......@@ -15,7 +15,7 @@ __all__ = ['metaclass_maker', 'wrap_with', 'dont_wrap', 'wrap_property',
'DocsFromParent']
class DocsFromParent(type):
def DocsFromParent(name, bases, dictionary):
"""
A metaclass that fetches missing docstring's for methods from the classes' bases,
looked up BFS.
......@@ -29,23 +29,21 @@ class DocsFromParent(type):
>>> ...
>>> assert Child.test.__doc__ == 'my docstring'
"""
def __call__(cls, name, bases, dictionary):
def extract_bases(obj):
if isinstance(obj, tuple):
return obj
else:
return [v_base for v_base in obj.__bases__ if v_base is not object]
for key, value in dictionary.items():
if callable(value) and not value.__doc__:
for base in walk(bases, extract_bases, deep_first=False):
if hasattr(base, key):
if getattr(base, key).__doc__:
value.__doc__ = getattr(base, key).__doc__
dictionary[key] = value
break
return super().__call__(name, bases, dictionary)
def extract_bases(cls):
if isinstance(cls, (tuple, list)):
return cls
else:
return [v_base for v_base in cls.__bases__ if v_base is not object]
for key, value in dictionary.items():
if not value.__doc__ and callable(value):
for base in walk(bases, extract_bases, deep_first=False):
if hasattr(base, key):
if getattr(base, key).__doc__:
value.__doc__ = getattr(base, key).__doc__
dictionary[key] = value
break
return type(name, bases, dictionary)
def skip_redundant(iterable, skip_set=None):
......
......@@ -33,6 +33,8 @@ class TestMetaclasses(unittest.TestCase):
"""my docstring"""
class Child(Father, metaclass=DocsFromParent):
a: int = 2
def test(self):
pass
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment