Skip to content

Commit 84d731b

Browse files
committed
feat: add support for TypeAliasType
1 parent c2a309d commit 84d731b

File tree

13 files changed

+47
-34
lines changed

13 files changed

+47
-34
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ repos:
2626
- types-docutils
2727
- legacy-api-wrap
2828
- myst-parser
29+
- sphinx-autodoc-typehints

docs/conf.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
import re
66
from typing import TYPE_CHECKING
77
from pathlib import PurePosixPath
8-
from datetime import datetime
9-
from datetime import timezone as tz
8+
from datetime import UTC, datetime
109
from importlib.metadata import metadata
1110

1211
from jinja2.tests import TESTS
@@ -42,7 +41,7 @@
4241
meta = metadata("scanpydoc")
4342
project = meta["name"]
4443
author = meta["author-email"].split(" <")[0]
45-
copyright = f"{datetime.now(tz=tz.utc):%Y}, {author}." # noqa: A001
44+
copyright = f"{datetime.now(tz=UTC):%Y}, {author}." # noqa: A001
4645
version = release = meta["version"]
4746

4847
master_doc = "index"

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ classifiers = [
1717
'Framework :: Sphinx :: Extension',
1818
'Typing :: Typed',
1919
]
20-
requires-python = '>=3.10'
20+
requires-python = '>=3.12'
2121
dependencies = [
2222
'sphinx>=7.0',
2323
]

src/scanpydoc/__init__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from __future__ import annotations
77

8-
from typing import TYPE_CHECKING, Any, TypeVar
8+
from typing import TYPE_CHECKING, Any
99
from textwrap import indent
1010
from collections.abc import Callable
1111

@@ -31,10 +31,8 @@
3131
:ref:`Metadata <sphinx:ext-metadata>` for this extension.
3232
"""
3333

34-
C = TypeVar("C", bound=Callable[..., Any])
3534

36-
37-
def _setup_sig(fn: C) -> C:
35+
def _setup_sig[C: Callable[..., Any]](fn: C) -> C:
3836
fn.__doc__ = f"{fn.__doc__ or ''}\n\n{indent(setup_sig_str, ' ' * 4)}"
3937
return fn
4038

src/scanpydoc/autosummary_generate_imported.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,7 @@
1313

1414

1515
if TYPE_CHECKING:
16-
import sys
17-
18-
if sys.version_info >= (3, 11):
19-
from typing import Never
20-
else: # pragma: no cover
21-
from typing import NoReturn as Never
16+
from typing import Never
2217

2318
from sphinx.application import Sphinx
2419

src/scanpydoc/definition_list_typed_field.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020

2121

2222
if TYPE_CHECKING:
23-
from typing import Any, TypeAlias
23+
from typing import Any
2424
from collections.abc import Iterable
2525

2626
from sphinx.application import Sphinx
2727
from sphinx.environment import BuildEnvironment
2828

29-
TextLikeNode: TypeAlias = nodes.Text | nodes.TextElement
29+
type TextLikeNode = nodes.Text | nodes.TextElement
3030

3131

3232
class DLTypedField(PyTypedField):

src/scanpydoc/elegant_typehints/_formatting.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import inspect
44
from types import GenericAlias
5-
from typing import TYPE_CHECKING, cast, get_args, get_origin
5+
from typing import TYPE_CHECKING, TypeAliasType, cast, get_args, get_origin
66

77
from sphinx_autodoc_typehints import format_annotation
88

@@ -16,7 +16,7 @@
1616
from sphinx.config import Config
1717

1818

19-
def typehints_formatter(annotation: type[Any], config: Config) -> str | None:
19+
def typehints_formatter(annotation: object, config: Config) -> str | None:
2020
"""Generate reStructuredText containing links to the types.
2121
2222
Can be used as ``typehints_formatter`` for :mod:`sphinx_autodoc_typehints`,
@@ -33,6 +33,9 @@ def typehints_formatter(annotation: type[Any], config: Config) -> str | None:
3333
-------
3434
reStructuredText describing the type
3535
"""
36+
if isinstance(annotation, TypeAliasType):
37+
return format_annotation(annotation.__value__, config)
38+
3639
if inspect.isclass(annotation) and annotation.__module__ == "builtins":
3740
return None
3841

src/scanpydoc/elegant_typehints/_role_mapping.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def __getitem__(self, key: tuple[str | None, str]) -> tuple[str | None, str]:
5050
for known_role in chain([None], {r for r, _ in self}):
5151
try:
5252
return self.data[known_role, key[1]]
53-
except KeyError: # noqa: PERF203
53+
except KeyError:
5454
pass
5555
raise KeyError(key)
5656

src/scanpydoc/rtd_github_links/__init__.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@
7575

7676
if TYPE_CHECKING:
7777
from types import CodeType, FrameType, MethodType, FunctionType, TracebackType
78-
from typing import Any, TypeAlias
78+
from typing import Any
7979
from collections.abc import Callable
8080

81-
_SourceObjectType: TypeAlias = (
81+
type _SourceObjectType = (
8282
ModuleType
8383
| type[Any]
8484
| MethodType
@@ -211,8 +211,7 @@ def github_url(qualname: str) -> str:
211211
try:
212212
obj, module = _get_obj_module(qualname)
213213
except Exception as e:
214-
if sys.version_info >= (3, 11):
215-
e.add_note(f"Qualname: {qualname!r}")
214+
e.add_note(f"Qualname: {qualname!r}")
216215
raise
217216
assert rtd_links_prefix is not None # noqa: S101
218217
path = rtd_links_prefix / _module_path(obj, module)

src/scanpydoc/rtd_github_links/_linkcode.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ class JSInfo(TypedDict):
2020

2121

2222
if TYPE_CHECKING:
23-
from typing import Literal, TypeAlias
23+
from typing import Literal
2424

2525
Domain = Literal["py", "c", "cpp", "javascript"]
26-
DomainInfo: TypeAlias = PyInfo | CInfo | JSInfo
26+
type DomainInfo = PyInfo | CInfo | JSInfo
2727

2828

2929
@overload

0 commit comments

Comments
 (0)