Skip to content

Commit 6b896af

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

File tree

5 files changed

+55
-0
lines changed

5 files changed

+55
-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

+12
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,18 @@ 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+
key = (namespace, membername)
769+
if key not in attr_docs and hasattr(obj.object, '__module__'):
770+
try:
771+
analyzer = ModuleAnalyzer.for_module(obj.object.__module__)
772+
except PycodeError:
773+
pass
774+
else:
775+
orig_docs = analyzer.find_attr_docs()
776+
if key in orig_docs:
777+
attr_docs[key] = orig_docs[key]
766778

767779
# if isattr is True, the member is documented as an attribute
768780
isattr = member is INSTANCEATTR or (namespace, membername) in attr_docs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from typing import TypeVar
2+
3+
from ._private import T
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

+30
Original file line numberDiff line numberDiff line change
@@ -3119,6 +3119,36 @@ def test_canonical(app):
31193119
]
31203120

31213121

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

0 commit comments

Comments
 (0)