Skip to content

Commit

Permalink
3.10+
Browse files Browse the repository at this point in the history
  • Loading branch information
flying-sheep committed Oct 22, 2024
1 parent cd0acd7 commit c02658d
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11", "3.12"]
python-version: ["3.10", "3.11", "3.12"]
steps:
- uses: actions/checkout@v4
with:
Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ classifiers = [
'Framework :: Sphinx :: Extension',
'Typing :: Typed',
]
requires-python = '>=3.9'
requires-python = '>=3.10'
dependencies = [
'sphinx>=7.0',
'get-annotations; python_version < "3.10"',
]

[project.optional-dependencies]
Expand Down
9 changes: 1 addition & 8 deletions src/scanpydoc/elegant_typehints/_formatting.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import sys
import inspect
from types import GenericAlias
from typing import TYPE_CHECKING, Any, cast, get_args, get_origin
Expand All @@ -15,12 +14,6 @@
from sphinx.config import Config


if sys.version_info >= (3, 10):
from types import UnionType
else: # pragma: no cover
UnionType = None


def typehints_formatter(annotation: type[Any], config: Config) -> str | None:
"""Generate reStructuredText containing links to the types.
Expand All @@ -43,7 +36,7 @@ def typehints_formatter(annotation: type[Any], config: Config) -> str | None:

tilde = "" if config.typehints_fully_qualified else "~"

if isinstance(annotation, (GenericAlias, _GenericAlias)):
if isinstance(annotation, GenericAlias | _GenericAlias):
args = get_args(annotation)
annotation = cast(type[Any], get_origin(annotation))
else:
Expand Down
11 changes: 3 additions & 8 deletions src/scanpydoc/elegant_typehints/_return_tuple.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from __future__ import annotations

import re
import sys
import inspect
from types import UnionType
from typing import TYPE_CHECKING, Union, get_args, get_origin, get_type_hints
from typing import Tuple as t_Tuple # noqa: UP035
from logging import getLogger
Expand All @@ -19,12 +19,7 @@
from sphinx.ext.autodoc import Options


if sys.version_info > (3, 10):
from types import UnionType

UNION_TYPES = {Union, UnionType}
else: # pragma: no cover
UNION_TYPES = {Union}
UNION_TYPES = {Union, UnionType}


__all__ = ["process_docstring", "_parse_returns_section", "setup"]
Expand Down Expand Up @@ -77,7 +72,7 @@ def process_docstring( # noqa: PLR0913

idxs_ret_names = _get_idxs_ret_names(lines)
if len(idxs_ret_names) == len(ret_types):
for l, rt in zip(idxs_ret_names, ret_types):
for l, rt in zip(idxs_ret_names, ret_types, strict=False):
typ = format_annotation(rt, app.config)
if (line := lines[l]).lstrip() in {":returns: :", ":return: :", ":"}:
transformed = f"{line[:-1]}{typ}"
Expand Down
7 changes: 2 additions & 5 deletions src/scanpydoc/rtd_github_links/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,7 @@ def _infer_vars(config: Config) -> tuple[str, PurePosixPath]:


def _get_annotations(obj: _SourceObjectType) -> dict[str, Any]:
if sys.version_info > (3, 10):
from inspect import get_annotations
else: # pragma: no cover
from get_annotations import get_annotations
from inspect import get_annotations

try:
return get_annotations(obj) # type: ignore[no-any-return,arg-type,unused-ignore]
Expand Down Expand Up @@ -159,7 +156,7 @@ def _get_obj_module(qualname: str) -> tuple[Any, ModuleType]:
raise e from None
if isinstance(thing, ModuleType): # pragma: no cover
mod = thing
elif is_dataclass(obj) or isinstance(thing, (GenericAlias, _GenericAlias)):
elif is_dataclass(obj) or isinstance(thing, GenericAlias | _GenericAlias):
obj = thing
else:
obj = thing
Expand Down
3 changes: 2 additions & 1 deletion tests/test_definition_list_typed_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ def test_convert_params(
assert isinstance(cyr := term[2], nodes.classifier)
assert len(cyr) == len(conv_types), cyr.children
assert all(
isinstance(cyr_part, conv_type) for cyr_part, conv_type in zip(cyr, conv_types)
isinstance(cyr_part, conv_type)
for cyr_part, conv_type in zip(cyr, conv_types, strict=True)
)


Expand Down
21 changes: 5 additions & 16 deletions tests/test_elegant_typehints.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,9 @@
from __future__ import annotations

import re
import sys
import inspect
from io import StringIO
from typing import (
TYPE_CHECKING,
Any,
Union,
AnyStr,
NoReturn,
Optional,
cast,
get_origin,
)
from typing import TYPE_CHECKING, Any, AnyStr, NoReturn, cast, get_origin
from pathlib import Path
from operator import attrgetter
from collections.abc import Mapping, Callable
Expand Down Expand Up @@ -258,8 +248,8 @@ def fn_test(m: object) -> None: # pragma: no cover
AnyStr,
NoReturn,
Callable[[int], None],
Union[int, str],
Union[int, str, None],
int | str,
int | str | None,
],
ids=lambda p: str(p).replace("typing.", ""),
)
Expand All @@ -276,7 +266,6 @@ def test_typing_classes(app: Sphinx, annotation: type) -> None:
assert output is None or output.startswith(f":py:data:`typing.{name}")


@pytest.mark.skipif(sys.version_info < (3, 10), reason="requires Python 3.10+")
def test_union_type(app: Sphinx) -> None:
union = eval("int | str") # noqa: S307
assert typehints_formatter(union, app.config) is None
Expand Down Expand Up @@ -391,15 +380,15 @@ class B:
("return_ann", "foo_rendered"),
[
pytest.param(tuple[str, int], ":py:class:`str`", id="tuple"),
pytest.param(Optional[tuple[str, int]], ":py:class:`str`", id="tuple | None"),
pytest.param(tuple[str, int] | None, ":py:class:`str`", id="tuple | None"),
pytest.param(
tuple[Mapping[str, float], int],
r":py:class:`~collections.abc.Mapping`\ \["
":py:class:`str`, :py:class:`float`"
"]",
id="complex",
),
pytest.param(Optional[int], None, id="int | None"),
pytest.param(int | None, None, id="int | None"),
],
)
def test_return_tuple(
Expand Down

0 comments on commit c02658d

Please sign in to comment.