Skip to content

Commit 2ea48f0

Browse files
committed
Fixed inner classes missing their parent name(s)
Fixes #117.
1 parent 894dfe0 commit 2ea48f0

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

CHANGELOG.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
1.10.2
2+
======
3+
4+
* Fixed inner classes missing their parent class name(s) when rendered
5+
6+
17
1.10.1
28
======
39

sphinx_autodoc_typehints.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,29 @@ def get_annotation_class_name(annotation) -> str:
3232
return 'None'
3333
elif annotation is Any:
3434
return 'Any'
35+
elif annotation is AnyStr:
36+
return 'AnyStr'
3537
elif inspect.isfunction(annotation) and hasattr(annotation, '__supertype__'):
3638
return 'NewType'
3739

38-
if getattr(annotation, '__name__', None):
39-
return annotation.__name__
40+
if getattr(annotation, '__qualname__', None):
41+
return annotation.__qualname__
4042
elif getattr(annotation, '_name', None): # Required for generic aliases on Python 3.7+
4143
return annotation._name
4244
elif getattr(annotation, 'name', None): # Required for at least Pattern
4345
return annotation.name
4446

4547
origin = getattr(annotation, '__origin__', None)
4648
if origin:
47-
if getattr(origin, '__name__', None): # Required for Protocol subclasses
48-
return origin.__name__
49+
if getattr(origin, '__qualname__', None): # Required for Protocol subclasses
50+
return origin.__qualname__
4951
elif getattr(origin, '_name', None): # Required for Union on Python 3.7+
5052
return origin._name
5153
else:
52-
return origin.__class__.__name__.lstrip('_') # Required for Union on Python < 3.7
54+
return origin.__class__.__qualname__.lstrip('_') # Required for Union on Python < 3.7
5355

5456
annotation_cls = annotation if inspect.isclass(annotation) else annotation.__class__
55-
return annotation_cls.__name__.lstrip('_')
57+
return annotation_cls.__qualname__.lstrip('_')
5658

5759

5860
def get_annotation_args(annotation, module: str, class_name: str) -> Tuple:

tests/test_sphinx_autodoc_typehints.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ class A:
2929
def get_type(self):
3030
return type(self)
3131

32+
class Inner:
33+
pass
34+
3235

3336
class B(Generic[T]):
3437
pass
@@ -83,6 +86,7 @@ class Metaclass(type):
8386
pytest.param(D, __name__, 'D', (), id='D'),
8487
pytest.param(E, __name__, 'E', (), id='E'),
8588
pytest.param(E[int], __name__, 'E', (int,), id='E_parametrized'),
89+
pytest.param(A.Inner, __name__, 'A.Inner', (), id='Inner')
8690
])
8791
def test_parse_annotation(annotation, module, class_name, args):
8892
assert get_annotation_module(annotation) == module

0 commit comments

Comments
 (0)