Skip to content

Commit 1583c4b

Browse files
authored
Fix the CI and 3.10 regressions (#188)
1 parent e285adc commit 1583c4b

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ test =
3535
typing_extensions >= 3.5
3636
dataclasses; python_version == "3.6"
3737
sphobjinv >= 2.0
38-
Sphinx >= 3.2.0
38+
Sphinx >= 4.3
3939
type_comments =
4040
typed_ast >= 1.4.0; python_version < "3.8"

sphinx_autodoc_typehints.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import sys
33
import textwrap
44
import typing
5-
from typing import Any, AnyStr, Tuple, TypeVar, get_type_hints
5+
from typing import Any, AnyStr, NewType, Tuple, TypeVar, get_type_hints
66

77
from sphinx.util import logging
88
from sphinx.util.inspect import signature as Signature
@@ -18,6 +18,9 @@ def get_annotation_module(annotation) -> str:
1818
if annotation is None:
1919
return 'builtins'
2020

21+
if sys.version_info >= (3, 10) and isinstance(annotation, NewType):
22+
return 'typing'
23+
2124
if hasattr(annotation, '__module__'):
2225
return annotation.__module__
2326

@@ -35,7 +38,9 @@ def get_annotation_class_name(annotation, module: str) -> str:
3538
return 'Any'
3639
elif annotation is AnyStr:
3740
return 'AnyStr'
38-
elif inspect.isfunction(annotation) and hasattr(annotation, '__supertype__'):
41+
elif (sys.version_info < (3, 10) and inspect.isfunction(annotation)
42+
and hasattr(annotation, '__supertype__')) or \
43+
(sys.version_info >= (3, 10) and isinstance(annotation, NewType)):
3944
return 'NewType'
4045

4146
if getattr(annotation, '__qualname__', None):
@@ -120,7 +125,9 @@ def format_annotation(annotation,
120125
# Some types require special handling
121126
if full_name == 'typing.NewType':
122127
args_format = '\\(:py:data:`~{name}`, {{}})'.format(name=annotation.__name__)
123-
role = 'func'
128+
role = 'class' if sys.version_info >= (3, 10) else 'func'
129+
elif full_name == 'typing.Optional':
130+
args = tuple(x for x in args if x is not type(None)) # noqa: E721
124131
elif full_name == 'typing.Union' and type(None) in args:
125132
if len(args) == 2:
126133
full_name = 'typing.Optional'

tests/test_sphinx_autodoc_typehints.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ class Metaclass(type):
5353
pass
5454

5555

56+
PY310_PLUS = sys.version_info >= (3, 10)
57+
58+
5659
@pytest.mark.parametrize('annotation, module, class_name, args', [
5760
pytest.param(str, 'builtins', 'str', (), id='str'),
5861
pytest.param(None, 'builtins', 'None', (), id='None'),
@@ -151,7 +154,8 @@ def test_parse_annotation(annotation, module, class_name, args):
151154
(D, ':py:class:`~%s.D`' % __name__),
152155
(E, ':py:class:`~%s.E`' % __name__),
153156
(E[int], ':py:class:`~%s.E`\\[:py:class:`int`]' % __name__),
154-
(W, ':py:func:`~typing.NewType`\\(:py:data:`~W`, :py:class:`str`)')
157+
(W, f':py:{"class" if PY310_PLUS else "func"}:'
158+
f'`~typing.NewType`\\(:py:data:`~W`, :py:class:`str`)')
155159
])
156160
def test_format_annotation(inv, annotation, expected_result):
157161
result = format_annotation(annotation)
@@ -352,7 +356,7 @@ class InnerClass
352356
Return type:
353357
"str"
354358
355-
property a_property
359+
property a_property: str
356360
357361
Property docstring
358362
@@ -506,9 +510,7 @@ class dummy_module.DataClass(x)
506510
507511
Class docstring.{undoc_params_0}
508512
509-
__init__(x)
510-
511-
Initialize self. See help(type(self)) for accurate signature.{undoc_params_1}
513+
__init__(x){undoc_params_1}
512514
513515
@dummy_module.Decorator(func)
514516

0 commit comments

Comments
 (0)