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

fix metaclass madness

parent e172553a
No related branches found
No related tags found
No related merge requests found
import inspect import inspect
from .recast_exceptions import silence_excs
from .decorators import wraps from .decorators import wraps
from .sequences.iterators import walk from .sequences.iterators import walk
...@@ -15,6 +14,13 @@ __all__ = ['metaclass_maker', 'wrap_with', 'dont_wrap', 'wrap_property', ...@@ -15,6 +14,13 @@ __all__ = ['metaclass_maker', 'wrap_with', 'dont_wrap', 'wrap_property',
'DocsFromParent'] 'DocsFromParent']
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]
def DocsFromParent(name: str, bases: tp.Tuple[type], dictionary: dict) -> tp.Type: def DocsFromParent(name: str, bases: tp.Tuple[type], dictionary: dict) -> tp.Type:
""" """
A metaclass that fetches missing docstring's for methods from the classes' bases, A metaclass that fetches missing docstring's for methods from the classes' bases,
...@@ -30,14 +36,8 @@ def DocsFromParent(name: str, bases: tp.Tuple[type], dictionary: dict) -> tp.Typ ...@@ -30,14 +36,8 @@ def DocsFromParent(name: str, bases: tp.Tuple[type], dictionary: dict) -> tp.Typ
>>> ... >>> ...
>>> assert Child.test.__doc__ == 'my docstring' >>> assert Child.test.__doc__ == 'my docstring'
""" """
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]
if '__doc__' not in dictionary: if '__doc__' not in dictionary:
for base in walk(bases, extract_bases, deep_first=False): for base in walk(bases, _extract_bases, deep_first=False):
if hasattr(base, '__doc__'): if hasattr(base, '__doc__'):
if base.__doc__: if base.__doc__:
dictionary['__doc__'] = base.__doc__ dictionary['__doc__'] = base.__doc__
...@@ -45,7 +45,7 @@ def DocsFromParent(name: str, bases: tp.Tuple[type], dictionary: dict) -> tp.Typ ...@@ -45,7 +45,7 @@ def DocsFromParent(name: str, bases: tp.Tuple[type], dictionary: dict) -> tp.Typ
for key, value in dictionary.items(): for key, value in dictionary.items():
if not value.__doc__ and callable(value): if not value.__doc__ and callable(value):
for base in walk(bases, extract_bases, deep_first=False): for base in walk(bases, _extract_bases, deep_first=False):
if hasattr(base, key): if hasattr(base, key):
if getattr(base, key).__doc__: if getattr(base, key).__doc__:
value.__doc__ = getattr(base, key).__doc__ value.__doc__ = getattr(base, key).__doc__
......
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