diff options
author | Ross Barnowski <rossbar@berkeley.edu> | 2022-01-26 10:06:27 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-26 13:06:27 -0500 |
commit | f33a53e64f9c92bc81353a9682f796d7485a4ed7 (patch) | |
tree | 3e9a136667602426e9e25590d88be90a86885d17 | |
parent | ffc3fa87b52702c98a5da649c79aafd49371dd74 (diff) | |
download | numpydoc-f33a53e64f9c92bc81353a9682f796d7485a4ed7.tar.gz |
Fix AttributeError in underline length check (#363)
* Add test for bad behavior.
Fix test.
* Add test for bad behavior.
* Improve object name introspection
Co-authored-by: Eric Larson <larson.eric.d@gmail.com>
* Improve test specificity for name introspection
Co-authored-by: Eric Larson <larson.eric.d@gmail.com>
Co-authored-by: Eric Larson <larson.eric.d@gmail.com>
-rw-r--r-- | numpydoc/docscrape.py | 8 | ||||
-rw-r--r-- | numpydoc/tests/test_docscrape.py | 26 |
2 files changed, 33 insertions, 1 deletions
diff --git a/numpydoc/docscrape.py b/numpydoc/docscrape.py index f0e42eb..d6544b2 100644 --- a/numpydoc/docscrape.py +++ b/numpydoc/docscrape.py @@ -426,7 +426,13 @@ class NumpyDocString(Mapping): filename = inspect.getsourcefile(self._obj) except TypeError: filename = None - msg += f" in the docstring of {self._obj.__name__}" + # Make UserWarning more descriptive via object introspection. + # Skip if introspection fails + name = getattr(self._obj, '__name__', None) + if name is None: + name = getattr(getattr(self._obj, '__class__', None), '__name__', None) + if name is not None: + msg += f" in the docstring of {name}" msg += f" in {filename}." if filename else "" if error: raise ValueError(msg) diff --git a/numpydoc/tests/test_docscrape.py b/numpydoc/tests/test_docscrape.py index 00ed9f7..83a8d5b 100644 --- a/numpydoc/tests/test_docscrape.py +++ b/numpydoc/tests/test_docscrape.py @@ -1522,6 +1522,32 @@ def test_xref(): line_by_line_compare(str(doc), xref_doc_txt_expected) +def test__error_location_no_name_attr(): + """ + Ensure that NumpyDocString._error_location doesn't fail when self._obj + does not have a __name__ attr. + + See gh-362 + """ + from collections.abc import Callable + + # Create a Callable that doesn't have a __name__ attribute + class Foo(): + def __call__(self): + pass + + + foo = Foo() # foo is a Callable, but no a function instance + assert isinstance(foo, Callable) + + # Create an NumpyDocString instance to call the _error_location method + nds = get_doc_object(foo) + + msg = "Potentially wrong underline length.*Foo.*" + with pytest.raises(ValueError, match=msg): + nds._error_location(msg=msg) + + if __name__ == "__main__": import pytest pytest.main() |