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.

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 15, 2020
1 parent 4c582b3 commit 174c1e7
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Features added
* autodoc: Add ``Documenter.config`` as a shortcut to access the config object
* autodoc: Add Optional[t] to annotation of function and method if a default
value equal to None is set.
* #8415: autodoc: a TypeVar imported from other module is not resolved
* #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
2 changes: 1 addition & 1 deletion sphinx/util/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ def stringify(annotation: Any) -> str:
else:
return annotation
elif isinstance(annotation, TypeVar):
return annotation.__name__
return '.'.join([annotation.__module__, annotation.__name__])
elif not annotation:
return repr(annotation)
elif annotation is NoneType:
Expand Down
8 changes: 4 additions & 4 deletions tests/test_util_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,10 @@ 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]"
assert stringify(T) == "test_util_typing.T"
assert stringify(T_co) == "test_util_typing.T_co"
assert stringify(T_contra) == "test_util_typing.T_contra"
assert stringify(List[T]) == "List[test_util_typing.T]"


def test_stringify_type_hints_custom_class():
Expand Down

0 comments on commit 174c1e7

Please sign in to comment.