Skip to content

Commit 1ef3776

Browse files
authored
Check 3.11 support (#247)
1 parent 13ea31f commit 1ef3776

File tree

8 files changed

+29
-17
lines changed

8 files changed

+29
-17
lines changed

.github/workflows/check.yml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ concurrency:
1212
jobs:
1313
test:
1414
name: test with CPython ${{ matrix.py }}
15-
runs-on: ubuntu-20.04
15+
runs-on: ubuntu-22.04
1616
strategy:
1717
fail-fast: false
1818
matrix:
1919
py:
20+
- "3.11.0-beta.4"
2021
- "3.10"
2122
- "3.9"
2223
- "3.8"
@@ -66,7 +67,7 @@ jobs:
6667

6768
coverage:
6869
name: Combine coverage
69-
runs-on: ubuntu-20.04
70+
runs-on: ubuntu-22.04
7071
needs: test
7172
steps:
7273
- uses: actions/checkout@v3
@@ -98,7 +99,7 @@ jobs:
9899

99100
check:
100101
name: tox env ${{ matrix.tox_env }}
101-
runs-on: ubuntu-20.04
102+
runs-on: ubuntu-22.04
102103
strategy:
103104
fail-fast: false
104105
matrix:
@@ -124,7 +125,7 @@ jobs:
124125
publish:
125126
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
126127
needs: [check, coverage]
127-
runs-on: ubuntu-20.04
128+
runs-on: ubuntu-22.04
128129
steps:
129130
- name: Setup python to build package
130131
uses: actions/setup-python@v4
@@ -138,7 +139,7 @@ jobs:
138139
- name: Build sdist and wheel
139140
run: python -m build -s -w . -o dist
140141
- name: Publish to PyPi
141-
uses: pypa/gh-action-pypi-publish@master
142+
uses: pypa/gh-action-pypi-publish@v1.5.1
142143
with:
143144
skip_existing: true
144145
user: __token__

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 1.19.0
4+
5+
- Support for CPython 3.11, no longer adds `Optional` when the argument is default per
6+
[recommendation from PEP-484](https://github.com/tox-dev/sphinx-autodoc-typehints/pull/247).
7+
38
## 1.18.3
49

510
- Support and require `nptyping>=2.1.2`

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ The following configuration options are accepted:
6464
directive, if present, otherwise fall back to using `:rtype:`. Use in conjunction with
6565
[napoleon_use_rtype](https://www.sphinx-doc.org/en/master/usage/extensions/napoleon.html#confval-napoleon_use_rtype)
6666
to avoid generation of duplicate or redundant return type information.
67-
- `typehints_defaults` (default: `None`): If `None`, defaults are not added. Otherwise adds a default annotation:
67+
- `typehints_defaults` (default: `None`): If `None`, defaults are not added. Otherwise, adds a default annotation:
6868

6969
- `'comma'` adds it after the type, changing Sphinx’ default look to “**param** (_int_, default: `1`) -- text”.
7070
- `'braces'` adds `(default: ...)` after the type (useful for numpydoc like styles).

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ other =
7272
*\sphinx-autodoc-typehints
7373

7474
[coverage:report]
75-
fail_under = 82
75+
fail_under = 81
7676

7777
[coverage:html]
7878
show_contexts = true

src/sphinx_autodoc_typehints/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@ def get_annotation_args(annotation: Any, module: str, class_name: str) -> tuple[
9090
return annotation.__values__ # type: ignore # deduced Any
9191
elif class_name == "Generic":
9292
return annotation.__parameters__ # type: ignore # deduced Any
93-
return getattr(annotation, "__args__", ())
93+
result = getattr(annotation, "__args__", ())
94+
# 3.10 and earlier Tuple[()] returns ((), ) instead of () the tuple does
95+
result = () if len(result) == 1 and result[0] == () else result # type: ignore
96+
return result
9497

9598

9699
def format_internal_tuple(t: tuple[Any, ...], config: Config) -> str:
@@ -161,7 +164,7 @@ def format_annotation(annotation: Any, config: Config) -> str: # noqa: C901 # t
161164
formatted_args = None if args else args_format
162165
elif full_name == "typing.Optional":
163166
args = tuple(x for x in args if x is not type(None)) # noqa: E721
164-
elif full_name == "typing.Union" and type(None) in args:
167+
elif full_name in ("typing.Union", "types.UnionType") and type(None) in args:
165168
if len(args) == 2:
166169
full_name = "typing.Optional"
167170
args = tuple(x for x in args if x is not type(None)) # noqa: E721

tests/roots/test-dummy/dummy_module.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import typing
22
from dataclasses import dataclass
33
from mailbox import Mailbox
4-
from typing import Callable, Union
4+
from typing import Callable, Optional, Union
55

66

77
def get_local_function():
@@ -191,7 +191,7 @@ def method_without_typehint(self, x): # noqa: U100
191191

192192

193193
def function_with_typehint_comment_not_inline(x=None, *y, z, **kwargs): # noqa: U100
194-
# type: (Union[str, bytes], *str, bytes, **int) -> None
194+
# type: (Union[str, bytes, None], *str, bytes, **int) -> None
195195
"""
196196
Function docstring.
197197
@@ -210,7 +210,7 @@ class ClassWithTypehintsNotInline:
210210
"""
211211

212212
def __init__(self, x=None): # noqa: U100
213-
# type: (Callable[[int, bytes], int]) -> None
213+
# type: (Optional[Callable[[int, bytes], int]]) -> None
214214
pass
215215

216216
def foo(self, x=1):
@@ -224,7 +224,7 @@ def foo(self, x=1):
224224

225225
@classmethod
226226
def mk(cls, x=None):
227-
# type: (Callable[[int, bytes], int]) -> ClassWithTypehintsNotInline
227+
# type: (Optional[Callable[[int, bytes], int]]) -> ClassWithTypehintsNotInline
228228
"""
229229
Method docstring.
230230

tests/test_sphinx_autodoc_typehints.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ def test_parse_annotation(annotation: Any, module: str, class_name: str, args: t
232232
(S, ":py:class:`~typing.TypeVar`\\(``S``, bound= miss)"),
233233
# ## These test for correct internal tuple rendering, even if not all are valid Tuple types
234234
# Zero-length tuple remains
235-
(Tuple[()], ":py:data:`~typing.Tuple`\\[()]"),
235+
(Tuple[()], ":py:data:`~typing.Tuple`"),
236236
# Internal single tuple with simple types is flattened in the output
237237
(Tuple[(int,)], ":py:data:`~typing.Tuple`\\[:py:class:`int`]"),
238238
(Tuple[(int, int)], ":py:data:`~typing.Tuple`\\[:py:class:`int`, :py:class:`int`]"),
@@ -374,7 +374,7 @@ def set_python_path() -> None:
374374
def maybe_fix_py310(expected_contents: str) -> str:
375375
if PY310_PLUS:
376376
for old, new in [
377-
("*bool** | **None*", '"bool" | "None"'),
377+
("*bool** | **None*", '"Optional"["bool"]'),
378378
("*int** | **str** | **float*", '"int" | "str" | "float"'),
379379
("*str** | **None*", '"Optional"["str"]'),
380380
("(*bool*)", '("bool")'),
@@ -719,7 +719,8 @@ def test_sphinx_output_future_annotations(app: SphinxTestApp, status: StringIO)
719719
Return type:
720720
str
721721
"""
722-
assert text_contents == maybe_fix_py310(dedent(expected_contents))
722+
expected_contents = maybe_fix_py310(dedent(expected_contents))
723+
assert text_contents == expected_contents
723724

724725

725726
@pytest.mark.parametrize(

tox.ini

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[tox]
22
envlist =
33
fix
4+
py311
45
py310
56
py39
67
py38
@@ -45,7 +46,7 @@ description = run type check on code base
4546
setenv =
4647
{tty:MYPY_FORCE_COLOR = 1}
4748
deps =
48-
mypy==0.961
49+
mypy==0.971
4950
types-docutils
5051
commands =
5152
mypy --python-version 3.10 src
@@ -71,6 +72,7 @@ commands =
7172
coverage html -d {toxworkdir}/htmlcov
7273
diff-cover --compare-branch {env:DIFF_AGAINST:origin/main} {toxworkdir}/coverage.xml
7374
depends =
75+
py311
7476
py310
7577
py39
7678
py38

0 commit comments

Comments
 (0)