diff --git a/numpydoc/docscrape_sphinx.py b/numpydoc/docscrape_sphinx.py index ee1187d4..a76f3d29 100644 --- a/numpydoc/docscrape_sphinx.py +++ b/numpydoc/docscrape_sphinx.py @@ -154,7 +154,8 @@ def _process_param(self, param, desc, fake_autosummary): if prefix: link_prefix = f"{prefix}." else: - link_prefix = "" + # leading dot: resolve in the class scope before the module scope + link_prefix = "." # Referenced object has a docstring display_param = f":obj:`{param} <{link_prefix}{param}>`" diff --git a/numpydoc/tests/test_docscrape.py b/numpydoc/tests/test_docscrape.py index 8b34ae73..30036753 100644 --- a/numpydoc/tests/test_docscrape.py +++ b/numpydoc/tests/test_docscrape.py @@ -1353,7 +1353,7 @@ def no_period(self): * hello * world - :obj:`an_attribute ` : float + :obj:`an_attribute <.an_attribute>` : float Test attribute **no_docstring** : str @@ -1362,13 +1362,13 @@ def no_period(self): **no_docstring2** : str .. - :obj:`multiline_sentence ` + :obj:`multiline_sentence <.multiline_sentence>` This is a sentence. - :obj:`midword_period ` + :obj:`midword_period <.midword_period>` The sentence for numpy.org. - :obj:`no_period ` + :obj:`no_period <.no_period>` This does not have a period .. rubric:: Methods @@ -1415,7 +1415,7 @@ def an_attribute(self): attr_doc = """:Attributes: - :obj:`an_attribute ` + :obj:`an_attribute <.an_attribute>` Test attribute""" assert attr_doc in str(SphinxClassDoc(Foo)) @@ -1433,6 +1433,31 @@ def an_attribute(self): assert "Another description" not in str(SphinxClassDoc(Foo, config=cfg)) +def test_attribute_link_is_class_scoped(): + class Foo: + """ + Class docstring. + + Attributes + ---------- + identity + Another description that is not used. + + """ + + @property + def identity(self): + """Test attribute""" + return + + attr_doc = """:Attributes: + + :obj:`identity <.identity>` + Test attribute""" + + assert attr_doc in str(SphinxClassDoc(Foo)) + + def test_templated_sections(): doc = SphinxClassDoc( None,