Skip to content

Commit

Permalink
\#13276: autodoc: Allow TypeVars to be reimported from other modules
Browse files Browse the repository at this point in the history
  • Loading branch information
coretl committed Jan 29, 2025
1 parent 5667050 commit afdd070
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ Bugs fixed
* #12975: Avoid rendering a trailing comma in C and C++ multi-line signatures.
* #13178: autodoc: Fix resolution for ``pathlib`` types.
Patch by Adam Turner.
* #13276: autodoc: Allow TypeVars to be reimported from other modules

Testing
-------
Expand Down
12 changes: 12 additions & 0 deletions sphinx/ext/autodoc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,18 @@ def is_filtered_inherited_member(name: str, obj: Any) -> bool:
for obj in members:
membername = obj.__name__
member = obj.object
# If the member doesn't have docs in the current module, see if it does
# where it is imported from
key = (namespace, membername)
if key not in attr_docs and hasattr(obj.object, '__module__'):
try:
analyzer = ModuleAnalyzer.for_module(obj.object.__module__)
except PycodeError:
pass
else:
orig_docs = analyzer.find_attr_docs()
if key in orig_docs:
attr_docs[key] = orig_docs[key]

# if isattr is True, the member is documented as an attribute
isattr = member is INSTANCEATTR or (namespace, membername) in attr_docs
Expand Down
8 changes: 8 additions & 0 deletions tests/roots/test-ext-autodoc/target/reimport/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from ._private import T

from typing import TypeVar

Check failure on line 3 in tests/roots/test-ext-autodoc/target/reimport/__init__.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

tests/roots/test-ext-autodoc/target/reimport/__init__.py:1:1: I001 Import block is un-sorted or un-formatted

V = TypeVar('V')
"""A locally defined TypeVar"""

__all__ = ['T', 'V']
4 changes: 4 additions & 0 deletions tests/roots/test-ext-autodoc/target/reimport/_private.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from typing import TypeVar

T = TypeVar('T', bound=int | str)
"""A reimported TypeVar"""
30 changes: 30 additions & 0 deletions tests/test_extensions/test_ext_autodoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3119,6 +3119,36 @@ def test_canonical(app):
]


@pytest.mark.sphinx('html', testroot='ext-autodoc')
def test_reimport_typevar(app):
options = {
'members': None,
'imported-members': None,
}
actual = do_autodoc(app, 'module', 'target.reimport', options)
assert list(actual) == [
'',
'.. py:module:: target.reimport',
'',
'',
'.. py:class:: T',
' :module: target.reimport',
'',
' A reimported TypeVar',
'',
" alias of TypeVar('T', bound=\\ :py:class:`int` | :py:class:`str`)",
'',
'',
'.. py:class:: V',
' :module: target.reimport',
'',
' A locally defined TypeVar',
'',
" alias of TypeVar('V')",
'',
]


@pytest.mark.sphinx('html', testroot='ext-autodoc')
def test_literal_render(app):
def bounded_typevar_rst(name, bound):
Expand Down

0 comments on commit afdd070

Please sign in to comment.