Skip to content

Commit e53f1c2

Browse files
committed
#13276: autodoc: Allow TypeVars to be reimported from other modules
1 parent 5667050 commit e53f1c2

File tree

5 files changed

+53
-0
lines changed

5 files changed

+53
-0
lines changed

CHANGES.rst

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ Bugs fixed
121121
* #12975: Avoid rendering a trailing comma in C and C++ multi-line signatures.
122122
* #13178: autodoc: Fix resolution for ``pathlib`` types.
123123
Patch by Adam Turner.
124+
* #13276: autodoc: Allow TypeVars to be reimported from other modules
124125

125126
Testing
126127
-------

sphinx/ext/autodoc/__init__.py

+11
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,17 @@ def is_filtered_inherited_member(name: str, obj: Any) -> bool:
763763
for obj in members:
764764
membername = obj.__name__
765765
member = obj.object
766+
# If the member doesn't have docs in the current module, see if it does
767+
# where it is imported from
768+
if (namespace, membername) not in attr_docs and hasattr(obj.object, "__module__"):
769+
try:
770+
analyzer = ModuleAnalyzer.for_module(obj.object.__module__)
771+
except PycodeError:
772+
pass
773+
else:
774+
orig_docs = analyzer.find_attr_docs()
775+
if (namespace, membername) in orig_docs:
776+
attr_docs[(namespace, membername)] = orig_docs[(namespace, membername)]
766777

767778
# if isattr is True, the member is documented as an attribute
768779
isattr = member is INSTANCEATTR or (namespace, membername) in attr_docs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from ._private import T
2+
3+
from typing import TypeVar
4+
5+
V = TypeVar("V")
6+
"""A locally defined TypeVar"""
7+
8+
__all__ = ["T", "V"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from typing import TypeVar
2+
3+
T = TypeVar("T", bound=int | str)
4+
"""A reimported TypeVar"""

tests/test_extensions/test_ext_autodoc.py

+29
Original file line numberDiff line numberDiff line change
@@ -3118,6 +3118,35 @@ def test_canonical(app):
31183118
'',
31193119
]
31203120

3121+
@pytest.mark.sphinx('html', testroot='ext-autodoc')
3122+
def test_reimport_typevar(app):
3123+
options = {
3124+
'members': None,
3125+
'imported-members': None,
3126+
}
3127+
actual = do_autodoc(app, 'module', 'target.reimport', options)
3128+
assert list(actual) == [
3129+
'',
3130+
'.. py:module:: target.reimport',
3131+
'',
3132+
'',
3133+
'.. py:class:: T',
3134+
' :module: target.reimport',
3135+
'',
3136+
' A reimported TypeVar',
3137+
'',
3138+
" alias of TypeVar('T', bound=\\ :py:class:`int` | :py:class:`str`)",
3139+
'',
3140+
'',
3141+
'.. py:class:: V',
3142+
' :module: target.reimport',
3143+
'',
3144+
' A locally defined TypeVar',
3145+
'',
3146+
" alias of TypeVar('V')",
3147+
'',
3148+
]
3149+
31213150

31223151
@pytest.mark.sphinx('html', testroot='ext-autodoc')
31233152
def test_literal_render(app):

0 commit comments

Comments
 (0)