Skip to content

Commit

Permalink
autodoc: an imported TypeVar is not resolved (refs: sphinx-doc#8415)
Browse files Browse the repository at this point in the history
So far, a TypeVar is rendered without module name. As a result, it
could not be resolved if it is imported from other modules.  This
prepends its module name and make it resolvable.  This is available
only in Python 3.7 or above.

As a side effect, all of TypeVars are displayed with module name. It
should be fixed in latter step (refs: sphinx-doc#7119)
  • Loading branch information
tk0miya committed Nov 22, 2020
1 parent 24a329e commit 6c71b5b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ Features added
* #8209: autodoc: Add ``:no-value:`` option to :rst:dir:`autoattribute` and
:rst:dir:`autodata` directive to suppress the default value of the variable
* #8460: autodoc: Support custom types defined by typing.NewType
* #8415: autodoc: a TypeVar imported from other module is not resolved (in
Python 3.7 or above)
* #6914: Add a new event :event:`warn-missing-reference` to custom warning
messages when failed to resolve a cross-reference
* #6914: Emit a detailed warning when failed to resolve a ``:ref:`` reference
Expand Down
5 changes: 4 additions & 1 deletion sphinx/util/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,10 @@ def stringify(annotation: Any) -> str:
else:
return annotation
elif isinstance(annotation, TypeVar):
return annotation.__name__
if annotation.__module__ == 'typing':
return annotation.__name__
else:
return '.'.join([annotation.__module__, annotation.__name__])
elif inspect.isNewType(annotation):
# Could not get the module where it defiend
return annotation.__name__
Expand Down
8 changes: 7 additions & 1 deletion tests/test_util_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,13 @@ def test_signature_annotations():

# TypeVars and generic types with TypeVars
sig = inspect.signature(f2)
assert stringify_signature(sig) == '(x: List[T], y: List[T_co], z: T) -> List[T_contra]'
if sys.version_info < (3, 7):
assert stringify_signature(sig) == '(x: List[T], y: List[T_co], z: T) -> List[T_contra]'
else:
assert stringify_signature(sig) == ('(x: List[tests.typing_test_data.T],'
' y: List[tests.typing_test_data.T_co],'
' z: tests.typing_test_data.T'
') -> List[tests.typing_test_data.T_contra]')

# Union types
sig = inspect.signature(f3)
Expand Down
15 changes: 11 additions & 4 deletions tests/test_util_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,17 @@ def test_stringify_type_hints_typevars():
T_co = TypeVar('T_co', covariant=True)
T_contra = TypeVar('T_contra', contravariant=True)

assert stringify(T) == "T"
assert stringify(T_co) == "T_co"
assert stringify(T_contra) == "T_contra"
assert stringify(List[T]) == "List[T]"
if sys.version_info < (3, 7):
assert stringify(T) == "T"
assert stringify(T_co) == "T_co"
assert stringify(T_contra) == "T_contra"
assert stringify(List[T]) == "List[T]"
else:
assert stringify(T) == "tests.test_util_typing.T"
assert stringify(T_co) == "tests.test_util_typing.T_co"
assert stringify(T_contra) == "tests.test_util_typing.T_contra"
assert stringify(List[T]) == "List[tests.test_util_typing.T]"

assert stringify(MyInt) == "MyInt"


Expand Down

0 comments on commit 6c71b5b

Please sign in to comment.