diff --git a/src/scanpydoc/elegant_typehints/_formatting.py b/src/scanpydoc/elegant_typehints/_formatting.py index dd1ef66..058bb68 100644 --- a/src/scanpydoc/elegant_typehints/_formatting.py +++ b/src/scanpydoc/elegant_typehints/_formatting.py @@ -6,6 +6,7 @@ from types import UnionType from typing import TYPE_CHECKING, Any, Literal, Union, get_args, get_origin from typing import Callable as t_Callable +from typing import Dict as t_Dict # noqa: UP035 from typing import Mapping as t_Mapping # noqa: UP035 from docutils import nodes @@ -64,7 +65,7 @@ def _format_terse(annotation: type[Any], config: Config) -> str: return f":py:class:`{tilde}collections.abc.Mapping`" # display dict as {k: v} - if origin is dict and len(args) == 2: # noqa: PLR2004 + if origin in (dict, t_Dict) and len(args) == 2: # noqa: UP006, PLR2004 k, v = args return f"{{{fmt(k)}: {fmt(v)}}}" diff --git a/src/scanpydoc/elegant_typehints/_return_tuple.py b/src/scanpydoc/elegant_typehints/_return_tuple.py index c002b81..0064c33 100644 --- a/src/scanpydoc/elegant_typehints/_return_tuple.py +++ b/src/scanpydoc/elegant_typehints/_return_tuple.py @@ -2,14 +2,17 @@ import inspect import re -from collections.abc import Collection from logging import getLogger -from typing import TYPE_CHECKING, Any, get_args, get_origin, get_type_hints +from types import UnionType +from typing import TYPE_CHECKING, Any, Union, get_args, get_origin, get_type_hints +from typing import Tuple as t_Tuple # noqa: UP035 from ._formatting import format_both if TYPE_CHECKING: + from collections.abc import Collection + from sphinx.application import Sphinx from sphinx.ext.autodoc import Options @@ -19,17 +22,15 @@ def get_tuple_annot(annotation: type | None) -> tuple[type, ...] | None: - from typing import Union - if annotation is None: return None origin = get_origin(annotation) if not origin: return None - if origin is Union: + if origin in (Union, UnionType): for annot in get_args(annotation): origin = get_origin(annot) - if origin in (tuple, tuple): + if origin in (tuple, t_Tuple): # noqa: UP006 annotation = annot break else: