Skip to content

Commit

Permalink
Make sure custom extension methods keep their docstrings (see #3707)
Browse files Browse the repository at this point in the history
  • Loading branch information
ines committed May 11, 2019
1 parent 931513f commit 9285c05
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
14 changes: 14 additions & 0 deletions spacy/tests/doc/test_underscore.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,17 @@ def test_underscore_dir(en_vocab):
doc = Doc(en_vocab, words=["hello", "world"])
assert "_" in dir(doc)
assert "test_dir" in dir(doc._)


def test_underscore_docstring(en_vocab):
"""Test that docstrings are available for extension methods, even though
they're partials."""

def test_method(doc, arg1=1, arg2=2):
"""I am a docstring."""
return (arg1, arg2)

Doc.set_extension("test_docstrings", method=test_method)
doc = Doc(en_vocab, words=["hello", "world"])
assert test_method.__doc__ == "I am a docstring."
assert doc._.test_docstrings.__doc__ == "I am a docstring."
6 changes: 5 additions & 1 deletion spacy/tokens/underscore.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ def __getattr__(self, name):
if getter is not None:
return getter(self._obj)
elif method is not None:
return functools.partial(method, self._obj)
method_partial = functools.partial(method, self._obj)
# Hack to port over docstrings of the original function
# See https://stackoverflow.com/q/27362727/6400719
method_partial.__doc__ = method.__doc__
return method_partial
else:
key = self._get_key(name)
if key in self._doc.user_data:
Expand Down

0 comments on commit 9285c05

Please sign in to comment.