Skip to content

Commit d33e5fe

Browse files
authored
FIX: resolve attribute cross-links in the class scope first (#706)
* FIX: resolve attribute cross-links in the class scope first * TST: add regression test for class-scoped attribute links
1 parent c127da1 commit d33e5fe

2 files changed

Lines changed: 32 additions & 6 deletions

File tree

‎numpydoc/docscrape_sphinx.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ def _process_param(self, param, desc, fake_autosummary):
154154
if prefix:
155155
link_prefix = f"{prefix}."
156156
else:
157-
link_prefix = ""
157+
# leading dot: resolve in the class scope before the module scope
158+
link_prefix = "."
158159

159160
# Referenced object has a docstring
160161
display_param = f":obj:`{param} <{link_prefix}{param}>`"

‎numpydoc/tests/test_docscrape.py‎

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,7 +1353,7 @@ def no_period(self):
13531353
* hello
13541354
* world
13551355
1356-
:obj:`an_attribute <an_attribute>` : float
1356+
:obj:`an_attribute <.an_attribute>` : float
13571357
Test attribute
13581358
13591359
**no_docstring** : str
@@ -1362,13 +1362,13 @@ def no_period(self):
13621362
**no_docstring2** : str
13631363
..
13641364
1365-
:obj:`multiline_sentence <multiline_sentence>`
1365+
:obj:`multiline_sentence <.multiline_sentence>`
13661366
This is a sentence.
13671367
1368-
:obj:`midword_period <midword_period>`
1368+
:obj:`midword_period <.midword_period>`
13691369
The sentence for numpy.org.
13701370
1371-
:obj:`no_period <no_period>`
1371+
:obj:`no_period <.no_period>`
13721372
This does not have a period
13731373
13741374
.. rubric:: Methods
@@ -1415,7 +1415,7 @@ def an_attribute(self):
14151415

14161416
attr_doc = """:Attributes:
14171417
1418-
:obj:`an_attribute <an_attribute>`
1418+
:obj:`an_attribute <.an_attribute>`
14191419
Test attribute"""
14201420

14211421
assert attr_doc in str(SphinxClassDoc(Foo))
@@ -1433,6 +1433,31 @@ def an_attribute(self):
14331433
assert "Another description" not in str(SphinxClassDoc(Foo, config=cfg))
14341434

14351435

1436+
def test_attribute_link_is_class_scoped():
1437+
class Foo:
1438+
"""
1439+
Class docstring.
1440+
1441+
Attributes
1442+
----------
1443+
identity
1444+
Another description that is not used.
1445+
1446+
"""
1447+
1448+
@property
1449+
def identity(self):
1450+
"""Test attribute"""
1451+
return
1452+
1453+
attr_doc = """:Attributes:
1454+
1455+
:obj:`identity <.identity>`
1456+
Test attribute"""
1457+
1458+
assert attr_doc in str(SphinxClassDoc(Foo))
1459+
1460+
14361461
def test_templated_sections():
14371462
doc = SphinxClassDoc(
14381463
None,

0 commit comments

Comments
 (0)