Skip to content

Commit

Permalink
Fix for inheritance with DocSubstitutionMeta (#114)
Browse files Browse the repository at this point in the history
* Fix for inheritance with DocSubstitutionMeta
  • Loading branch information
tomdele committed Dec 16, 2020
1 parent 87a65ad commit 86414fb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
6 changes: 6 additions & 0 deletions bluepysnap/_doctools.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ def _word_swapper(doc, source_word, target_word):
"""Swap a word with another in a docstring."""
if doc is None:
return doc
if source_word is None or target_word is None:
return doc
return doc.replace(source_word, target_word)


Expand Down Expand Up @@ -52,6 +54,7 @@ class DocSubstitutionMeta(type):
"""
def __new__(mcs, name, parents, attrs, source_word=None, target_word=None):
"""Define the new class to return."""
original_attrs = attrs.copy()
for parent in parents:
# skip classmethod with isfunction if I use also ismethod as a predicate I can have the
# classmethod docstring changed but then the cls argument is not automatically skipped.
Expand All @@ -62,6 +65,9 @@ def __new__(mcs, name, parents, attrs, source_word=None, target_word=None):
continue
except AttributeError:
pass
# skip overrode functions
if fun_name in original_attrs:
continue
# skip special methods
if fun_name.startswith("__"):
continue
Expand Down
2 changes: 1 addition & 1 deletion bluepysnap/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __init__(self, config):
"""Initializes a circuit object from a SONATA config file.
Args:
config (str): Path to a SONATA config file.
config (str/dict): Path to a SONATA config file or sonata config dict.
Returns:
Circuit: A Circuit object.
Expand Down
12 changes: 12 additions & 0 deletions tests/test__doctools.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ class TestClassB(TestClass, metaclass=test_module.DocSubstitutionMeta, source_wo
"""New class with changed docstrings."""


class TestClassC(TestClassA):
def bar(self):
"""Is overrode correctly"""
return 42


def test_DocSubstitutionMeta():
default = TestClass()
tested = TestClassA()
Expand All @@ -49,3 +55,9 @@ def test_DocSubstitutionMeta():
assert tested.bar.__doc__ == expected
assert tested.foo_bar.__doc__ is None
assert tested.__dict__ == default.__dict__

# I can inherit from a class above
tested = TestClassC()
assert tested.foo.__doc__ == TestClassA.foo.__doc__
assert tested.bar.__doc__ == "Is overrode correctly"
assert tested.bar() == 42

0 comments on commit 86414fb

Please sign in to comment.