Skip to content

fixed generic inheritance edge case and added test #67

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

Closed
Closed
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
43 changes: 42 additions & 1 deletion sphinx_autodoc_typehints.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,48 @@ def format_annotation(annotation):
if Generic in annotation_cls.mro():
params = (getattr(annotation, '__parameters__', None) or
getattr(annotation, '__args__', None))
extra = '\\[{}]'.format(', '.join(format_annotation(param) for param in params))

try:
# join type annotation together in a formatted string
extra = '\\[{}]'.format(', '.join(format_annotation(param) for param in params))
except TypeError:
# in some cases, a "Generic" has been inherited from another Generic
# with a concrete type, and therefore cannot be assigned a Type
# annotation. In this case, ``params`` will return None. Consider the
# following:
#
# T = TypeVar('T')
#
# class A(Generic[T])
# def method() -> T
# pass
#
#
# class B(A[str])
# pass
#
#
# def some_method_for_a(value: A[int]) -> None:
#
#
# def some_method_for_b(value: B) -> None:
# pass
#
# In the above, B inherits from a Generic type, but supplies a concrete
# type (str). As such, it does not get an additional annotation when
# used in a signature, since it will always be B[str]. This is unlike A,
# which is truly Generic. See the difference between the signatures of
# the two example functions above.
#
# B still passes as a Generic type, since it inherits from one, but has
# no annotations, since its Type Variable is set upon class definition.
# This causes, ``params`` above to return None, and throw a TypeError
# when joined.
#
# Here we need to detect this case, and just set ``extra`` to a blank
# string, so no additional annotation information is returned for B in
# signature annotations.
extra = ''

return ':py:class:`~{}.{}`{}'.format(annotation.__module__, annotation_cls.__qualname__,
extra)
Expand Down
8 changes: 7 additions & 1 deletion tests/test_sphinx_autodoc_typehints.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ class B(Generic[T]):
pass


# Tests inheriting a concrete generic
class C(B[str]):
pass


class Slotted:
__slots__ = ()

Expand Down Expand Up @@ -71,7 +76,8 @@ class Slotted:
(Pattern[str], ':py:class:`~typing.Pattern`\\[:py:class:`str`]'),
(A, ':py:class:`~%s.A`' % __name__),
(B, ':py:class:`~%s.B`\\[\\~T]' % __name__),
(B[int], ':py:class:`~%s.B`\\[:py:class:`int`]' % __name__)
(B[int], ':py:class:`~%s.B`\\[:py:class:`int`]' % __name__),
(C, ':py:class:`~%s.C`' % __name__)
])
def test_format_annotation(annotation, expected_result):
result = format_annotation(annotation)
Expand Down