Coverage for yuio / _typing_ext.py: 83%
18 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-05 11:41 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-05 11:41 +0000
1# Yuio project, MIT license.
2#
3# https://github.com/taminomara/yuio/
4#
5# You're free to copy this file to your project and edit it for your needs,
6# just keep this copyright line please :3
8# pyright: reportDeprecated=false
9# ruff: noqa: F403, F405, I002
11from __future__ import annotations
13import abc as _abc
14import re as _re
15import types as _types
17from typing import TYPE_CHECKING
19if TYPE_CHECKING:
20 import typing_extensions as _t
21else:
22 from yuio import _typing as _t
25def is_union(origin):
26 return origin is _t.Union or origin is _types.UnionType
29if TYPE_CHECKING:
30 StrRePattern: _t.TypeAlias = _re.Pattern[str]
31 StrReMatch: _t.TypeAlias = _re.Match[str]
33else:
34 try:
35 StrRePattern = _re.Pattern[str]
36 StrReMatch = _re.Match[str]
37 except TypeError:
38 StrRePattern = _re.Pattern
39 StrReMatch = _re.Match
42try:
43 from typing import type_repr # type: ignore
44except ImportError:
46 def type_repr(obj: _t.Any) -> str:
47 # Better `type_repr` for older pythons.
48 if origin := _t.get_origin(obj):
49 return type_repr(origin) + type_repr(_t.get_args(obj))
50 if isinstance(obj, (type, _types.FunctionType, _types.BuiltinFunctionType)):
51 if obj.__module__ == "builtins":
52 return obj.__qualname__
53 return f"{obj.__module__}.{obj.__qualname__}"
54 if obj is ...:
55 return "..."
56 if isinstance(obj, _types.FunctionType):
57 return obj.__name__
58 if isinstance(obj, tuple):
59 # Special case for `repr` of types with `ParamSpec`:
60 return "[" + ", ".join(type_repr(t) for t in obj) + "]"
61 return repr(obj)
64_T_contra = _t.TypeVar("_T_contra", contravariant=True)
67class SupportsLt(_t.Protocol[_T_contra]):
68 """
69 Protocol for objects that can be compared to each other.
71 """
73 @_abc.abstractmethod
74 def __lt__(self, other: _T_contra, /) -> bool: ...