Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autodoc: Allow TypeVars to be reimported from other modules #13277

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
9 changes: 9 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,9 @@
from typing import TypeVar

from ._private import T
from ._usage import add

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

__all__ = ['T', 'V', 'add']
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"""
8 changes: 8 additions & 0 deletions tests/roots/test-ext-autodoc/target/reimport/_usage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from __future__ import annotations

from ._private import T

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

View workflow job for this annotation

GitHub Actions / ruff

Ruff (TC001)

tests/roots/test-ext-autodoc/target/reimport/_usage.py:3:23: TC001 Move application import `._private.T` into a type-checking block


def add(x: T, y: T) -> T:
"""Add two things together"""
return x + y
37 changes: 37 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,43 @@ 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')",
'',
'',
'.. py:function:: add(x: ~target.reimport.T, y: ~target.reimport.T) ->'
' ~target.reimport.T',
' :module: target.reimport',
'',
' Add two things together',
'',
]


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