Skip to content

Commit a25fa05

Browse files
committed
Fixed TypeError with generic classes having the "name" property
Fixes #118.
1 parent 2ea48f0 commit a25fa05

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

CHANGELOG.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
1.10.3
2+
======
3+
4+
* Fixed ``TypeError`` (or wrong rendered class name) when an annotation is a generic class that has
5+
a ``name`` property
6+
7+
18
1.10.2
29
======
310

sphinx_autodoc_typehints.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def get_annotation_module(annotation) -> str:
2626
raise ValueError('Cannot determine the module of {}'.format(annotation))
2727

2828

29-
def get_annotation_class_name(annotation) -> str:
29+
def get_annotation_class_name(annotation, module: str) -> str:
3030
# Special cases
3131
if annotation is None:
3232
return 'None'
@@ -41,7 +41,8 @@ def get_annotation_class_name(annotation) -> str:
4141
return annotation.__qualname__
4242
elif getattr(annotation, '_name', None): # Required for generic aliases on Python 3.7+
4343
return annotation._name
44-
elif getattr(annotation, 'name', None): # Required for at least Pattern
44+
elif getattr(annotation, 'name', None) and module in ('typing', 'typing_extensions'):
45+
# Required for at least Pattern and Match
4546
return annotation.name
4647

4748
origin = getattr(annotation, '__origin__', None)
@@ -108,7 +109,7 @@ def format_annotation(annotation, fully_qualified: bool = False) -> str:
108109

109110
try:
110111
module = get_annotation_module(annotation)
111-
class_name = get_annotation_class_name(annotation)
112+
class_name = get_annotation_class_name(annotation, module)
112113
args = get_annotation_args(annotation, module, class_name)
113114
except ValueError:
114115
return str(annotation)

tests/test_sphinx_autodoc_typehints.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ class Inner:
3434

3535

3636
class B(Generic[T]):
37-
pass
37+
# This is set to make sure the correct class name ("B") is picked up
38+
name = 'Foo'
3839

3940

4041
class C(B[str]):
@@ -90,7 +91,7 @@ class Metaclass(type):
9091
])
9192
def test_parse_annotation(annotation, module, class_name, args):
9293
assert get_annotation_module(annotation) == module
93-
assert get_annotation_class_name(annotation) == class_name
94+
assert get_annotation_class_name(annotation, module) == class_name
9495
assert get_annotation_args(annotation, module, class_name) == args
9596

9697

0 commit comments

Comments
 (0)