Add loan-advisor-agent
Browse files- loan-advisor-agent/.venv/Lib/site-packages/typing_inspection/__init__.py +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/typing_inspection/__pycache__/__init__.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/typing_inspection/__pycache__/introspection.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/typing_inspection/__pycache__/typing_objects.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/typing_inspection/introspection.py +587 -0
- loan-advisor-agent/.venv/Lib/site-packages/typing_inspection/py.typed +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/typing_inspection/typing_objects.py +607 -0
- loan-advisor-agent/.venv/Lib/site-packages/typing_inspection/typing_objects.pyi +417 -0
loan-advisor-agent/.venv/Lib/site-packages/typing_inspection/__init__.py
ADDED
|
File without changes
|
loan-advisor-agent/.venv/Lib/site-packages/typing_inspection/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (194 Bytes). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/typing_inspection/__pycache__/introspection.cpython-312.pyc
ADDED
|
Binary file (17.8 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/typing_inspection/__pycache__/typing_objects.cpython-312.pyc
ADDED
|
Binary file (17.4 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/typing_inspection/introspection.py
ADDED
|
@@ -0,0 +1,587 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""High-level introspection utilities, used to inspect type annotations."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
import sys
|
| 6 |
+
import types
|
| 7 |
+
from collections.abc import Generator
|
| 8 |
+
from dataclasses import InitVar
|
| 9 |
+
from enum import Enum, IntEnum, auto
|
| 10 |
+
from typing import Any, Literal, NamedTuple, cast
|
| 11 |
+
|
| 12 |
+
from typing_extensions import TypeAlias, assert_never, get_args, get_origin
|
| 13 |
+
|
| 14 |
+
from . import typing_objects
|
| 15 |
+
|
| 16 |
+
__all__ = (
|
| 17 |
+
'AnnotationSource',
|
| 18 |
+
'ForbiddenQualifier',
|
| 19 |
+
'InspectedAnnotation',
|
| 20 |
+
'Qualifier',
|
| 21 |
+
'get_literal_values',
|
| 22 |
+
'inspect_annotation',
|
| 23 |
+
'is_union_origin',
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
if sys.version_info >= (3, 14) or sys.version_info < (3, 10):
|
| 27 |
+
|
| 28 |
+
def is_union_origin(obj: Any, /) -> bool:
|
| 29 |
+
"""Return whether the provided origin is the union form.
|
| 30 |
+
|
| 31 |
+
```pycon
|
| 32 |
+
>>> is_union_origin(typing.Union)
|
| 33 |
+
True
|
| 34 |
+
>>> is_union_origin(get_origin(int | str))
|
| 35 |
+
True
|
| 36 |
+
>>> is_union_origin(types.UnionType)
|
| 37 |
+
True
|
| 38 |
+
```
|
| 39 |
+
|
| 40 |
+
!!! note
|
| 41 |
+
Since Python 3.14, both `Union[<t1>, <t2>, ...]` and `<t1> | <t2> | ...` forms create instances
|
| 42 |
+
of the same [`typing.Union`][] class. As such, it is recommended to not use this function
|
| 43 |
+
anymore (provided that you only support Python 3.14 or greater), and instead use the
|
| 44 |
+
[`typing_objects.is_union()`][typing_inspection.typing_objects.is_union] function directly:
|
| 45 |
+
|
| 46 |
+
```python
|
| 47 |
+
from typing import Union, get_origin
|
| 48 |
+
|
| 49 |
+
from typing_inspection import typing_objects
|
| 50 |
+
|
| 51 |
+
typ = int | str # Or Union[int, str]
|
| 52 |
+
origin = get_origin(typ)
|
| 53 |
+
if typing_objects.is_union(origin):
|
| 54 |
+
...
|
| 55 |
+
```
|
| 56 |
+
"""
|
| 57 |
+
return typing_objects.is_union(obj)
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
else:
|
| 61 |
+
|
| 62 |
+
def is_union_origin(obj: Any, /) -> bool:
|
| 63 |
+
"""Return whether the provided origin is the union form.
|
| 64 |
+
|
| 65 |
+
```pycon
|
| 66 |
+
>>> is_union_origin(typing.Union)
|
| 67 |
+
True
|
| 68 |
+
>>> is_union_origin(get_origin(int | str))
|
| 69 |
+
True
|
| 70 |
+
>>> is_union_origin(types.UnionType)
|
| 71 |
+
True
|
| 72 |
+
```
|
| 73 |
+
|
| 74 |
+
!!! note
|
| 75 |
+
Since Python 3.14, both `Union[<t1>, <t2>, ...]` and `<t1> | <t2> | ...` forms create instances
|
| 76 |
+
of the same [`typing.Union`][] class. As such, it is recommended to not use this function
|
| 77 |
+
anymore (provided that you only support Python 3.14 or greater), and instead use the
|
| 78 |
+
[`typing_objects.is_union()`][typing_inspection.typing_objects.is_union] function directly:
|
| 79 |
+
|
| 80 |
+
```python
|
| 81 |
+
from typing import Union, get_origin
|
| 82 |
+
|
| 83 |
+
from typing_inspection import typing_objects
|
| 84 |
+
|
| 85 |
+
typ = int | str # Or Union[int, str]
|
| 86 |
+
origin = get_origin(typ)
|
| 87 |
+
if typing_objects.is_union(origin):
|
| 88 |
+
...
|
| 89 |
+
```
|
| 90 |
+
"""
|
| 91 |
+
return typing_objects.is_union(obj) or obj is types.UnionType
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
def _literal_type_check(value: Any, /) -> None:
|
| 95 |
+
"""Type check the provided literal value against the legal parameters."""
|
| 96 |
+
if (
|
| 97 |
+
not isinstance(value, (int, bytes, str, bool, Enum, typing_objects.NoneType))
|
| 98 |
+
and value is not typing_objects.NoneType
|
| 99 |
+
):
|
| 100 |
+
raise TypeError(f'{value} is not a valid literal value, must be one of: int, bytes, str, Enum, None.')
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def get_literal_values(
|
| 104 |
+
annotation: Any,
|
| 105 |
+
/,
|
| 106 |
+
*,
|
| 107 |
+
type_check: bool = False,
|
| 108 |
+
unpack_type_aliases: Literal['skip', 'lenient', 'eager'] = 'eager',
|
| 109 |
+
) -> Generator[Any]:
|
| 110 |
+
"""Yield the values contained in the provided [`Literal`][typing.Literal] [special form][].
|
| 111 |
+
|
| 112 |
+
Args:
|
| 113 |
+
annotation: The [`Literal`][typing.Literal] [special form][] to unpack.
|
| 114 |
+
type_check: Whether to check if the literal values are [legal parameters][literal-legal-parameters].
|
| 115 |
+
Raises a [`TypeError`][] otherwise.
|
| 116 |
+
unpack_type_aliases: What to do when encountering [PEP 695](https://peps.python.org/pep-0695/)
|
| 117 |
+
[type aliases][type-aliases]. Can be one of:
|
| 118 |
+
|
| 119 |
+
- `'skip'`: Do not try to parse type aliases. Note that this can lead to incorrect results:
|
| 120 |
+
```pycon
|
| 121 |
+
>>> type MyAlias = Literal[1, 2]
|
| 122 |
+
>>> list(get_literal_values(Literal[MyAlias, 3], unpack_type_aliases="skip"))
|
| 123 |
+
[MyAlias, 3]
|
| 124 |
+
```
|
| 125 |
+
|
| 126 |
+
- `'lenient'`: Try to parse type aliases, and fallback to `'skip'` if the type alias can't be inspected
|
| 127 |
+
(because of an undefined forward reference).
|
| 128 |
+
|
| 129 |
+
- `'eager'`: Parse type aliases and raise any encountered [`NameError`][] exceptions (the default):
|
| 130 |
+
```pycon
|
| 131 |
+
>>> type MyAlias = Literal[1, 2]
|
| 132 |
+
>>> list(get_literal_values(Literal[MyAlias, 3], unpack_type_aliases="eager"))
|
| 133 |
+
[1, 2, 3]
|
| 134 |
+
```
|
| 135 |
+
|
| 136 |
+
Note:
|
| 137 |
+
While `None` is [equivalent to][none] `type(None)`, the runtime implementation of [`Literal`][typing.Literal]
|
| 138 |
+
does not de-duplicate them. This function makes sure this de-duplication is applied:
|
| 139 |
+
|
| 140 |
+
```pycon
|
| 141 |
+
>>> list(get_literal_values(Literal[NoneType, None]))
|
| 142 |
+
[None]
|
| 143 |
+
```
|
| 144 |
+
|
| 145 |
+
Example:
|
| 146 |
+
```pycon
|
| 147 |
+
>>> type Ints = Literal[1, 2]
|
| 148 |
+
>>> list(get_literal_values(Literal[1, Ints], unpack_type_alias="skip"))
|
| 149 |
+
["a", Ints]
|
| 150 |
+
>>> list(get_literal_values(Literal[1, Ints]))
|
| 151 |
+
[1, 2]
|
| 152 |
+
>>> list(get_literal_values(Literal[1.0], type_check=True))
|
| 153 |
+
Traceback (most recent call last):
|
| 154 |
+
...
|
| 155 |
+
TypeError: 1.0 is not a valid literal value, must be one of: int, bytes, str, Enum, None.
|
| 156 |
+
```
|
| 157 |
+
"""
|
| 158 |
+
# `literal` is guaranteed to be a `Literal[...]` special form, so use
|
| 159 |
+
# `__args__` directly instead of calling `get_args()`.
|
| 160 |
+
|
| 161 |
+
if unpack_type_aliases == 'skip':
|
| 162 |
+
_has_none = False
|
| 163 |
+
# `Literal` parameters are already deduplicated, no need to do it ourselves.
|
| 164 |
+
# (we only check for `None` and `NoneType`, which should be considered as duplicates).
|
| 165 |
+
for arg in annotation.__args__:
|
| 166 |
+
if type_check:
|
| 167 |
+
_literal_type_check(arg)
|
| 168 |
+
if arg is None or arg is typing_objects.NoneType:
|
| 169 |
+
if not _has_none:
|
| 170 |
+
yield None
|
| 171 |
+
_has_none = True
|
| 172 |
+
else:
|
| 173 |
+
yield arg
|
| 174 |
+
else:
|
| 175 |
+
# We'll need to manually deduplicate parameters, see the `Literal` implementation in `typing`.
|
| 176 |
+
values_and_type: list[tuple[Any, type[Any]]] = []
|
| 177 |
+
|
| 178 |
+
for arg in annotation.__args__:
|
| 179 |
+
# Note: we could also check for generic aliases with a type alias as an origin.
|
| 180 |
+
# However, it is very unlikely that this happens as type variables can't appear in
|
| 181 |
+
# `Literal` forms, so the only valid (but unnecessary) use case would be something like:
|
| 182 |
+
# `type Test[T] = Literal['a']` (and then use `Test[SomeType]`).
|
| 183 |
+
if typing_objects.is_typealiastype(arg):
|
| 184 |
+
try:
|
| 185 |
+
alias_value = arg.__value__
|
| 186 |
+
except NameError:
|
| 187 |
+
if unpack_type_aliases == 'eager':
|
| 188 |
+
raise
|
| 189 |
+
# unpack_type_aliases == "lenient":
|
| 190 |
+
if type_check:
|
| 191 |
+
_literal_type_check(arg)
|
| 192 |
+
values_and_type.append((arg, type(arg)))
|
| 193 |
+
else:
|
| 194 |
+
sub_args = get_literal_values(
|
| 195 |
+
alias_value, type_check=type_check, unpack_type_aliases=unpack_type_aliases
|
| 196 |
+
)
|
| 197 |
+
values_and_type.extend((a, type(a)) for a in sub_args) # pyright: ignore[reportUnknownArgumentType]
|
| 198 |
+
else:
|
| 199 |
+
if type_check:
|
| 200 |
+
_literal_type_check(arg)
|
| 201 |
+
if arg is typing_objects.NoneType:
|
| 202 |
+
values_and_type.append((None, typing_objects.NoneType))
|
| 203 |
+
else:
|
| 204 |
+
values_and_type.append((arg, type(arg))) # pyright: ignore[reportUnknownArgumentType]
|
| 205 |
+
|
| 206 |
+
try:
|
| 207 |
+
dct = dict.fromkeys(values_and_type)
|
| 208 |
+
except TypeError:
|
| 209 |
+
# Unhashable parameters, the Python implementation allows them
|
| 210 |
+
yield from (p for p, _ in values_and_type)
|
| 211 |
+
else:
|
| 212 |
+
yield from (p for p, _ in dct)
|
| 213 |
+
|
| 214 |
+
|
| 215 |
+
Qualifier: TypeAlias = Literal['required', 'not_required', 'read_only', 'class_var', 'init_var', 'final']
|
| 216 |
+
"""A [type qualifier][]."""
|
| 217 |
+
|
| 218 |
+
_all_qualifiers: set[Qualifier] = set(get_args(Qualifier))
|
| 219 |
+
|
| 220 |
+
|
| 221 |
+
# TODO at some point, we could switch to an enum flag, so that multiple sources
|
| 222 |
+
# can be combined. However, is there a need for this?
|
| 223 |
+
class AnnotationSource(IntEnum):
|
| 224 |
+
# TODO if/when https://peps.python.org/pep-0767/ is accepted, add 'read_only'
|
| 225 |
+
# to CLASS and NAMED_TUPLE (even though for named tuples it is redundant).
|
| 226 |
+
|
| 227 |
+
"""The source of an annotation, e.g. a class or a function.
|
| 228 |
+
|
| 229 |
+
Depending on the source, different [type qualifiers][type qualifier] may be (dis)allowed.
|
| 230 |
+
"""
|
| 231 |
+
|
| 232 |
+
ASSIGNMENT_OR_VARIABLE = auto()
|
| 233 |
+
"""An annotation used in an assignment or variable annotation:
|
| 234 |
+
|
| 235 |
+
```python
|
| 236 |
+
x: Final[int] = 1
|
| 237 |
+
y: Final[str]
|
| 238 |
+
```
|
| 239 |
+
|
| 240 |
+
**Allowed type qualifiers:** [`Final`][typing.Final].
|
| 241 |
+
"""
|
| 242 |
+
|
| 243 |
+
CLASS = auto()
|
| 244 |
+
"""An annotation used in the body of a class:
|
| 245 |
+
|
| 246 |
+
```python
|
| 247 |
+
class Test:
|
| 248 |
+
x: Final[int] = 1
|
| 249 |
+
y: ClassVar[str]
|
| 250 |
+
```
|
| 251 |
+
|
| 252 |
+
**Allowed type qualifiers:** [`ClassVar`][typing.ClassVar], [`Final`][typing.Final].
|
| 253 |
+
"""
|
| 254 |
+
|
| 255 |
+
DATACLASS = auto()
|
| 256 |
+
"""An annotation used in the body of a dataclass:
|
| 257 |
+
|
| 258 |
+
```python
|
| 259 |
+
@dataclass
|
| 260 |
+
class Test:
|
| 261 |
+
x: Final[int] = 1
|
| 262 |
+
y: InitVar[str] = 'test'
|
| 263 |
+
```
|
| 264 |
+
|
| 265 |
+
**Allowed type qualifiers:** [`ClassVar`][typing.ClassVar], [`Final`][typing.Final], [`InitVar`][dataclasses.InitVar].
|
| 266 |
+
""" # noqa: E501
|
| 267 |
+
|
| 268 |
+
TYPED_DICT = auto()
|
| 269 |
+
"""An annotation used in the body of a [`TypedDict`][typing.TypedDict]:
|
| 270 |
+
|
| 271 |
+
```python
|
| 272 |
+
class TD(TypedDict):
|
| 273 |
+
x: Required[ReadOnly[int]]
|
| 274 |
+
y: ReadOnly[NotRequired[str]]
|
| 275 |
+
```
|
| 276 |
+
|
| 277 |
+
**Allowed type qualifiers:** [`ReadOnly`][typing.ReadOnly], [`Required`][typing.Required],
|
| 278 |
+
[`NotRequired`][typing.NotRequired].
|
| 279 |
+
"""
|
| 280 |
+
|
| 281 |
+
NAMED_TUPLE = auto()
|
| 282 |
+
"""An annotation used in the body of a [`NamedTuple`][typing.NamedTuple].
|
| 283 |
+
|
| 284 |
+
```python
|
| 285 |
+
class NT(NamedTuple):
|
| 286 |
+
x: int
|
| 287 |
+
y: str
|
| 288 |
+
```
|
| 289 |
+
|
| 290 |
+
**Allowed type qualifiers:** none.
|
| 291 |
+
"""
|
| 292 |
+
|
| 293 |
+
FUNCTION = auto()
|
| 294 |
+
"""An annotation used in a function, either for a parameter or the return value.
|
| 295 |
+
|
| 296 |
+
```python
|
| 297 |
+
def func(a: int) -> str:
|
| 298 |
+
...
|
| 299 |
+
```
|
| 300 |
+
|
| 301 |
+
**Allowed type qualifiers:** none.
|
| 302 |
+
"""
|
| 303 |
+
|
| 304 |
+
ANY = auto()
|
| 305 |
+
"""An annotation that might come from any source.
|
| 306 |
+
|
| 307 |
+
**Allowed type qualifiers:** all.
|
| 308 |
+
"""
|
| 309 |
+
|
| 310 |
+
BARE = auto()
|
| 311 |
+
"""An annotation that is inspected as is.
|
| 312 |
+
|
| 313 |
+
**Allowed type qualifiers:** none.
|
| 314 |
+
"""
|
| 315 |
+
|
| 316 |
+
@property
|
| 317 |
+
def allowed_qualifiers(self) -> set[Qualifier]:
|
| 318 |
+
"""The allowed [type qualifiers][type qualifier] for this annotation source."""
|
| 319 |
+
# TODO use a match statement when Python 3.9 support is dropped.
|
| 320 |
+
if self is AnnotationSource.ASSIGNMENT_OR_VARIABLE:
|
| 321 |
+
return {'final'}
|
| 322 |
+
elif self is AnnotationSource.CLASS:
|
| 323 |
+
return {'final', 'class_var'}
|
| 324 |
+
elif self is AnnotationSource.DATACLASS:
|
| 325 |
+
return {'final', 'class_var', 'init_var'}
|
| 326 |
+
elif self is AnnotationSource.TYPED_DICT:
|
| 327 |
+
return {'required', 'not_required', 'read_only'}
|
| 328 |
+
elif self in (AnnotationSource.NAMED_TUPLE, AnnotationSource.FUNCTION, AnnotationSource.BARE):
|
| 329 |
+
return set()
|
| 330 |
+
elif self is AnnotationSource.ANY:
|
| 331 |
+
return _all_qualifiers
|
| 332 |
+
else: # pragma: no cover
|
| 333 |
+
assert_never(self)
|
| 334 |
+
|
| 335 |
+
|
| 336 |
+
class ForbiddenQualifier(Exception):
|
| 337 |
+
"""The provided [type qualifier][] is forbidden."""
|
| 338 |
+
|
| 339 |
+
qualifier: Qualifier
|
| 340 |
+
"""The forbidden qualifier."""
|
| 341 |
+
|
| 342 |
+
def __init__(self, qualifier: Qualifier, /) -> None:
|
| 343 |
+
self.qualifier = qualifier
|
| 344 |
+
|
| 345 |
+
|
| 346 |
+
class _UnknownTypeEnum(Enum):
|
| 347 |
+
UNKNOWN = auto()
|
| 348 |
+
|
| 349 |
+
def __str__(self) -> str:
|
| 350 |
+
return 'UNKNOWN'
|
| 351 |
+
|
| 352 |
+
def __repr__(self) -> str:
|
| 353 |
+
return '<UNKNOWN>'
|
| 354 |
+
|
| 355 |
+
|
| 356 |
+
UNKNOWN = _UnknownTypeEnum.UNKNOWN
|
| 357 |
+
"""A sentinel value used when no [type expression][] is present."""
|
| 358 |
+
|
| 359 |
+
_UnkownType: TypeAlias = Literal[_UnknownTypeEnum.UNKNOWN]
|
| 360 |
+
"""The type of the [`UNKNOWN`][typing_inspection.introspection.UNKNOWN] sentinel value."""
|
| 361 |
+
|
| 362 |
+
|
| 363 |
+
class InspectedAnnotation(NamedTuple):
|
| 364 |
+
"""The result of the inspected annotation."""
|
| 365 |
+
|
| 366 |
+
type: Any | _UnkownType
|
| 367 |
+
"""The final [type expression][], with [type qualifiers][type qualifier] and annotated metadata stripped.
|
| 368 |
+
|
| 369 |
+
If no type expression is available, the [`UNKNOWN`][typing_inspection.introspection.UNKNOWN] sentinel
|
| 370 |
+
value is used instead. This is the case when a [type qualifier][] is used with no type annotation:
|
| 371 |
+
|
| 372 |
+
```python
|
| 373 |
+
ID: Final = 1
|
| 374 |
+
|
| 375 |
+
class C:
|
| 376 |
+
x: ClassVar = 'test'
|
| 377 |
+
```
|
| 378 |
+
"""
|
| 379 |
+
|
| 380 |
+
qualifiers: set[Qualifier]
|
| 381 |
+
"""The [type qualifiers][type qualifier] present on the annotation."""
|
| 382 |
+
|
| 383 |
+
metadata: list[Any]
|
| 384 |
+
"""The annotated metadata."""
|
| 385 |
+
|
| 386 |
+
|
| 387 |
+
def inspect_annotation( # noqa: PLR0915
|
| 388 |
+
annotation: Any,
|
| 389 |
+
/,
|
| 390 |
+
*,
|
| 391 |
+
annotation_source: AnnotationSource,
|
| 392 |
+
unpack_type_aliases: Literal['skip', 'lenient', 'eager'] = 'skip',
|
| 393 |
+
) -> InspectedAnnotation:
|
| 394 |
+
"""Inspect an [annotation expression][], extracting any [type qualifier][] and metadata.
|
| 395 |
+
|
| 396 |
+
An [annotation expression][] is a [type expression][] optionally surrounded by one or more
|
| 397 |
+
[type qualifiers][type qualifier] or by [`Annotated`][typing.Annotated]. This function will:
|
| 398 |
+
|
| 399 |
+
- Unwrap the type expression, keeping track of the type qualifiers.
|
| 400 |
+
- Unwrap [`Annotated`][typing.Annotated] forms, keeping track of the annotated metadata.
|
| 401 |
+
|
| 402 |
+
Args:
|
| 403 |
+
annotation: The annotation expression to be inspected.
|
| 404 |
+
annotation_source: The source of the annotation. Depending on the source (e.g. a class), different type
|
| 405 |
+
qualifiers may be (dis)allowed. To allow any type qualifier, use
|
| 406 |
+
[`AnnotationSource.ANY`][typing_inspection.introspection.AnnotationSource.ANY].
|
| 407 |
+
unpack_type_aliases: What to do when encountering [PEP 695](https://peps.python.org/pep-0695/)
|
| 408 |
+
[type aliases][type-aliases]. Can be one of:
|
| 409 |
+
|
| 410 |
+
- `'skip'`: Do not try to parse type aliases (the default):
|
| 411 |
+
```pycon
|
| 412 |
+
>>> type MyInt = Annotated[int, 'meta']
|
| 413 |
+
>>> inspect_annotation(MyInt, annotation_source=AnnotationSource.BARE, unpack_type_aliases='skip')
|
| 414 |
+
InspectedAnnotation(type=MyInt, qualifiers={}, metadata=[])
|
| 415 |
+
```
|
| 416 |
+
|
| 417 |
+
- `'lenient'`: Try to parse type aliases, and fallback to `'skip'` if the type alias
|
| 418 |
+
can't be inspected (because of an undefined forward reference):
|
| 419 |
+
```pycon
|
| 420 |
+
>>> type MyInt = Annotated[Undefined, 'meta']
|
| 421 |
+
>>> inspect_annotation(MyInt, annotation_source=AnnotationSource.BARE, unpack_type_aliases='lenient')
|
| 422 |
+
InspectedAnnotation(type=MyInt, qualifiers={}, metadata=[])
|
| 423 |
+
>>> Undefined = int
|
| 424 |
+
>>> inspect_annotation(MyInt, annotation_source=AnnotationSource.BARE, unpack_type_aliases='lenient')
|
| 425 |
+
InspectedAnnotation(type=int, qualifiers={}, metadata=['meta'])
|
| 426 |
+
```
|
| 427 |
+
|
| 428 |
+
- `'eager'`: Parse type aliases and raise any encountered [`NameError`][] exceptions.
|
| 429 |
+
|
| 430 |
+
Returns:
|
| 431 |
+
The result of the inspected annotation, where the type expression, used qualifiers and metadata is stored.
|
| 432 |
+
|
| 433 |
+
Example:
|
| 434 |
+
```pycon
|
| 435 |
+
>>> inspect_annotation(
|
| 436 |
+
... Final[Annotated[ClassVar[Annotated[int, 'meta_1']], 'meta_2']],
|
| 437 |
+
... annotation_source=AnnotationSource.CLASS,
|
| 438 |
+
... )
|
| 439 |
+
...
|
| 440 |
+
InspectedAnnotation(type=int, qualifiers={'class_var', 'final'}, metadata=['meta_1', 'meta_2'])
|
| 441 |
+
```
|
| 442 |
+
"""
|
| 443 |
+
allowed_qualifiers = annotation_source.allowed_qualifiers
|
| 444 |
+
qualifiers: set[Qualifier] = set()
|
| 445 |
+
metadata: list[Any] = []
|
| 446 |
+
|
| 447 |
+
while True:
|
| 448 |
+
annotation, _meta = _unpack_annotated(annotation, unpack_type_aliases=unpack_type_aliases)
|
| 449 |
+
if _meta:
|
| 450 |
+
metadata = _meta + metadata
|
| 451 |
+
continue
|
| 452 |
+
|
| 453 |
+
origin = get_origin(annotation)
|
| 454 |
+
if origin is not None:
|
| 455 |
+
if typing_objects.is_classvar(origin):
|
| 456 |
+
if 'class_var' not in allowed_qualifiers:
|
| 457 |
+
raise ForbiddenQualifier('class_var')
|
| 458 |
+
qualifiers.add('class_var')
|
| 459 |
+
annotation = annotation.__args__[0]
|
| 460 |
+
elif typing_objects.is_final(origin):
|
| 461 |
+
if 'final' not in allowed_qualifiers:
|
| 462 |
+
raise ForbiddenQualifier('final')
|
| 463 |
+
qualifiers.add('final')
|
| 464 |
+
annotation = annotation.__args__[0]
|
| 465 |
+
elif typing_objects.is_required(origin):
|
| 466 |
+
if 'required' not in allowed_qualifiers:
|
| 467 |
+
raise ForbiddenQualifier('required')
|
| 468 |
+
qualifiers.add('required')
|
| 469 |
+
annotation = annotation.__args__[0]
|
| 470 |
+
elif typing_objects.is_notrequired(origin):
|
| 471 |
+
if 'not_required' not in allowed_qualifiers:
|
| 472 |
+
raise ForbiddenQualifier('not_required')
|
| 473 |
+
qualifiers.add('not_required')
|
| 474 |
+
annotation = annotation.__args__[0]
|
| 475 |
+
elif typing_objects.is_readonly(origin):
|
| 476 |
+
if 'read_only' not in allowed_qualifiers:
|
| 477 |
+
raise ForbiddenQualifier('not_required')
|
| 478 |
+
qualifiers.add('read_only')
|
| 479 |
+
annotation = annotation.__args__[0]
|
| 480 |
+
else:
|
| 481 |
+
# origin is not None but not a type qualifier nor `Annotated` (e.g. `list[int]`):
|
| 482 |
+
break
|
| 483 |
+
elif isinstance(annotation, InitVar):
|
| 484 |
+
if 'init_var' not in allowed_qualifiers:
|
| 485 |
+
raise ForbiddenQualifier('init_var')
|
| 486 |
+
qualifiers.add('init_var')
|
| 487 |
+
annotation = cast(Any, annotation.type)
|
| 488 |
+
else:
|
| 489 |
+
break
|
| 490 |
+
|
| 491 |
+
# `Final`, `ClassVar` and `InitVar` are type qualifiers allowed to be used as a bare annotation:
|
| 492 |
+
if typing_objects.is_final(annotation):
|
| 493 |
+
if 'final' not in allowed_qualifiers:
|
| 494 |
+
raise ForbiddenQualifier('final')
|
| 495 |
+
qualifiers.add('final')
|
| 496 |
+
annotation = UNKNOWN
|
| 497 |
+
elif typing_objects.is_classvar(annotation):
|
| 498 |
+
if 'class_var' not in allowed_qualifiers:
|
| 499 |
+
raise ForbiddenQualifier('class_var')
|
| 500 |
+
qualifiers.add('class_var')
|
| 501 |
+
annotation = UNKNOWN
|
| 502 |
+
elif annotation is InitVar:
|
| 503 |
+
if 'init_var' not in allowed_qualifiers:
|
| 504 |
+
raise ForbiddenQualifier('init_var')
|
| 505 |
+
qualifiers.add('init_var')
|
| 506 |
+
annotation = UNKNOWN
|
| 507 |
+
|
| 508 |
+
return InspectedAnnotation(annotation, qualifiers, metadata)
|
| 509 |
+
|
| 510 |
+
|
| 511 |
+
def _unpack_annotated_inner(
|
| 512 |
+
annotation: Any, unpack_type_aliases: Literal['lenient', 'eager'], check_annotated: bool
|
| 513 |
+
) -> tuple[Any, list[Any]]:
|
| 514 |
+
origin = get_origin(annotation)
|
| 515 |
+
if check_annotated and typing_objects.is_annotated(origin):
|
| 516 |
+
annotated_type = annotation.__origin__
|
| 517 |
+
metadata = list(annotation.__metadata__)
|
| 518 |
+
|
| 519 |
+
# The annotated type might be a PEP 695 type alias, so we need to recursively
|
| 520 |
+
# unpack it. Because Python already flattens `Annotated[Annotated[<type>, ...], ...]` forms,
|
| 521 |
+
# we can skip the `is_annotated()` check in the next call:
|
| 522 |
+
annotated_type, sub_meta = _unpack_annotated_inner(
|
| 523 |
+
annotated_type, unpack_type_aliases=unpack_type_aliases, check_annotated=False
|
| 524 |
+
)
|
| 525 |
+
metadata = sub_meta + metadata
|
| 526 |
+
return annotated_type, metadata
|
| 527 |
+
elif typing_objects.is_typealiastype(annotation):
|
| 528 |
+
try:
|
| 529 |
+
value = annotation.__value__
|
| 530 |
+
except NameError:
|
| 531 |
+
if unpack_type_aliases == 'eager':
|
| 532 |
+
raise
|
| 533 |
+
else:
|
| 534 |
+
typ, metadata = _unpack_annotated_inner(
|
| 535 |
+
value, unpack_type_aliases=unpack_type_aliases, check_annotated=True
|
| 536 |
+
)
|
| 537 |
+
if metadata:
|
| 538 |
+
# Having metadata means the type alias' `__value__` was an `Annotated` form
|
| 539 |
+
# (or, recursively, a type alias to an `Annotated` form). It is important to check
|
| 540 |
+
# for this, as we don't want to unpack other type aliases (e.g. `type MyInt = int`).
|
| 541 |
+
return typ, metadata
|
| 542 |
+
return annotation, []
|
| 543 |
+
elif typing_objects.is_typealiastype(origin):
|
| 544 |
+
# When parameterized, PEP 695 type aliases become generic aliases
|
| 545 |
+
# (e.g. with `type MyList[T] = Annotated[list[T], ...]`, `MyList[int]`
|
| 546 |
+
# is a generic alias).
|
| 547 |
+
try:
|
| 548 |
+
value = origin.__value__
|
| 549 |
+
except NameError:
|
| 550 |
+
if unpack_type_aliases == 'eager':
|
| 551 |
+
raise
|
| 552 |
+
else:
|
| 553 |
+
# While Python already handles type variable replacement for simple `Annotated` forms,
|
| 554 |
+
# we need to manually apply the same logic for PEP 695 type aliases:
|
| 555 |
+
# - With `MyList = Annotated[list[T], ...]`, `MyList[int] == Annotated[list[int], ...]`
|
| 556 |
+
# - With `type MyList[T] = Annotated[list[T], ...]`, `MyList[int].__value__ == Annotated[list[T], ...]`.
|
| 557 |
+
|
| 558 |
+
try:
|
| 559 |
+
# To do so, we emulate the parameterization of the value with the arguments:
|
| 560 |
+
# with `type MyList[T] = Annotated[list[T], ...]`, to emulate `MyList[int]`,
|
| 561 |
+
# we do `Annotated[list[T], ...][int]` (which gives `Annotated[list[T], ...]`):
|
| 562 |
+
value = value[annotation.__args__]
|
| 563 |
+
except TypeError:
|
| 564 |
+
# Might happen if the type alias is parameterized, but its value doesn't have any
|
| 565 |
+
# type variables, e.g. `type MyInt[T] = int`.
|
| 566 |
+
pass
|
| 567 |
+
typ, metadata = _unpack_annotated_inner(
|
| 568 |
+
value, unpack_type_aliases=unpack_type_aliases, check_annotated=True
|
| 569 |
+
)
|
| 570 |
+
if metadata:
|
| 571 |
+
return typ, metadata
|
| 572 |
+
return annotation, []
|
| 573 |
+
|
| 574 |
+
return annotation, []
|
| 575 |
+
|
| 576 |
+
|
| 577 |
+
# This could eventually be made public:
|
| 578 |
+
def _unpack_annotated(
|
| 579 |
+
annotation: Any, /, *, unpack_type_aliases: Literal['skip', 'lenient', 'eager'] = 'eager'
|
| 580 |
+
) -> tuple[Any, list[Any]]:
|
| 581 |
+
if unpack_type_aliases == 'skip':
|
| 582 |
+
if typing_objects.is_annotated(get_origin(annotation)):
|
| 583 |
+
return annotation.__origin__, list(annotation.__metadata__)
|
| 584 |
+
else:
|
| 585 |
+
return annotation, []
|
| 586 |
+
|
| 587 |
+
return _unpack_annotated_inner(annotation, unpack_type_aliases=unpack_type_aliases, check_annotated=True)
|
loan-advisor-agent/.venv/Lib/site-packages/typing_inspection/py.typed
ADDED
|
File without changes
|
loan-advisor-agent/.venv/Lib/site-packages/typing_inspection/typing_objects.py
ADDED
|
@@ -0,0 +1,607 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Low-level introspection utilities for [`typing`][] members.
|
| 2 |
+
|
| 3 |
+
The provided functions in this module check against both the [`typing`][] and [`typing_extensions`][]
|
| 4 |
+
variants, if they exists and are different.
|
| 5 |
+
"""
|
| 6 |
+
# ruff: noqa: UP006
|
| 7 |
+
|
| 8 |
+
import collections.abc
|
| 9 |
+
import contextlib
|
| 10 |
+
import re
|
| 11 |
+
import sys
|
| 12 |
+
import typing
|
| 13 |
+
import warnings
|
| 14 |
+
from textwrap import dedent
|
| 15 |
+
from types import FunctionType, GenericAlias
|
| 16 |
+
from typing import Any, Final
|
| 17 |
+
|
| 18 |
+
import typing_extensions
|
| 19 |
+
from typing_extensions import LiteralString, TypeAliasType, TypeIs, deprecated
|
| 20 |
+
|
| 21 |
+
__all__ = (
|
| 22 |
+
'DEPRECATED_ALIASES',
|
| 23 |
+
'NoneType',
|
| 24 |
+
'is_annotated',
|
| 25 |
+
'is_any',
|
| 26 |
+
'is_classvar',
|
| 27 |
+
'is_concatenate',
|
| 28 |
+
'is_deprecated',
|
| 29 |
+
'is_final',
|
| 30 |
+
'is_forwardref',
|
| 31 |
+
'is_generic',
|
| 32 |
+
'is_literal',
|
| 33 |
+
'is_literalstring',
|
| 34 |
+
'is_namedtuple',
|
| 35 |
+
'is_never',
|
| 36 |
+
'is_newtype',
|
| 37 |
+
'is_nodefault',
|
| 38 |
+
'is_noextraitems',
|
| 39 |
+
'is_noreturn',
|
| 40 |
+
'is_notrequired',
|
| 41 |
+
'is_paramspec',
|
| 42 |
+
'is_paramspecargs',
|
| 43 |
+
'is_paramspeckwargs',
|
| 44 |
+
'is_readonly',
|
| 45 |
+
'is_required',
|
| 46 |
+
'is_self',
|
| 47 |
+
'is_typealias',
|
| 48 |
+
'is_typealiastype',
|
| 49 |
+
'is_typeguard',
|
| 50 |
+
'is_typeis',
|
| 51 |
+
'is_typevar',
|
| 52 |
+
'is_typevartuple',
|
| 53 |
+
'is_union',
|
| 54 |
+
'is_unpack',
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
_IS_PY310 = sys.version_info[:2] == (3, 10)
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def _compile_identity_check_function(member: LiteralString, function_name: LiteralString) -> FunctionType:
|
| 61 |
+
"""Create a function checking that the function argument is the (unparameterized) typing `member`.
|
| 62 |
+
|
| 63 |
+
The function will make sure to check against both the `typing` and `typing_extensions`
|
| 64 |
+
variants as depending on the Python version, the `typing_extensions` variant might be different.
|
| 65 |
+
For instance, on Python 3.9:
|
| 66 |
+
|
| 67 |
+
```pycon
|
| 68 |
+
>>> from typing import Literal as t_Literal
|
| 69 |
+
>>> from typing_extensions import Literal as te_Literal, get_origin
|
| 70 |
+
|
| 71 |
+
>>> t_Literal is te_Literal
|
| 72 |
+
False
|
| 73 |
+
>>> get_origin(t_Literal[1])
|
| 74 |
+
typing.Literal
|
| 75 |
+
>>> get_origin(te_Literal[1])
|
| 76 |
+
typing_extensions.Literal
|
| 77 |
+
```
|
| 78 |
+
"""
|
| 79 |
+
in_typing = hasattr(typing, member)
|
| 80 |
+
in_typing_extensions = hasattr(typing_extensions, member)
|
| 81 |
+
|
| 82 |
+
if in_typing and in_typing_extensions:
|
| 83 |
+
if getattr(typing, member) is getattr(typing_extensions, member):
|
| 84 |
+
check_code = f'obj is typing.{member}'
|
| 85 |
+
else:
|
| 86 |
+
check_code = f'obj is typing.{member} or obj is typing_extensions.{member}'
|
| 87 |
+
elif in_typing and not in_typing_extensions:
|
| 88 |
+
check_code = f'obj is typing.{member}'
|
| 89 |
+
elif not in_typing and in_typing_extensions:
|
| 90 |
+
check_code = f'obj is typing_extensions.{member}'
|
| 91 |
+
else:
|
| 92 |
+
check_code = 'False'
|
| 93 |
+
|
| 94 |
+
func_code = dedent(f"""
|
| 95 |
+
def {function_name}(obj: Any, /) -> bool:
|
| 96 |
+
return {check_code}
|
| 97 |
+
""")
|
| 98 |
+
|
| 99 |
+
locals_: dict[str, Any] = {}
|
| 100 |
+
globals_: dict[str, Any] = {'Any': Any, 'typing': typing, 'typing_extensions': typing_extensions}
|
| 101 |
+
exec(func_code, globals_, locals_)
|
| 102 |
+
return locals_[function_name]
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
def _compile_isinstance_check_function(member: LiteralString, function_name: LiteralString) -> FunctionType:
|
| 106 |
+
"""Create a function checking that the function is an instance of the typing `member`.
|
| 107 |
+
|
| 108 |
+
The function will make sure to check against both the `typing` and `typing_extensions`
|
| 109 |
+
variants as depending on the Python version, the `typing_extensions` variant might be different.
|
| 110 |
+
"""
|
| 111 |
+
in_typing = hasattr(typing, member)
|
| 112 |
+
in_typing_extensions = hasattr(typing_extensions, member)
|
| 113 |
+
|
| 114 |
+
if in_typing and in_typing_extensions:
|
| 115 |
+
if getattr(typing, member) is getattr(typing_extensions, member):
|
| 116 |
+
check_code = f'isinstance(obj, typing.{member})'
|
| 117 |
+
else:
|
| 118 |
+
check_code = f'isinstance(obj, (typing.{member}, typing_extensions.{member}))'
|
| 119 |
+
elif in_typing and not in_typing_extensions:
|
| 120 |
+
check_code = f'isinstance(obj, typing.{member})'
|
| 121 |
+
elif not in_typing and in_typing_extensions:
|
| 122 |
+
check_code = f'isinstance(obj, typing_extensions.{member})'
|
| 123 |
+
else:
|
| 124 |
+
check_code = 'False'
|
| 125 |
+
|
| 126 |
+
func_code = dedent(f"""
|
| 127 |
+
def {function_name}(obj: Any, /) -> 'TypeIs[{member}]':
|
| 128 |
+
return {check_code}
|
| 129 |
+
""")
|
| 130 |
+
|
| 131 |
+
locals_: dict[str, Any] = {}
|
| 132 |
+
globals_: dict[str, Any] = {'Any': Any, 'typing': typing, 'typing_extensions': typing_extensions}
|
| 133 |
+
exec(func_code, globals_, locals_)
|
| 134 |
+
return locals_[function_name]
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
if sys.version_info >= (3, 10):
|
| 138 |
+
from types import NoneType
|
| 139 |
+
else:
|
| 140 |
+
NoneType = type(None)
|
| 141 |
+
|
| 142 |
+
# Keep this ordered, as per `typing.__all__`:
|
| 143 |
+
|
| 144 |
+
is_annotated = _compile_identity_check_function('Annotated', 'is_annotated')
|
| 145 |
+
is_annotated.__doc__ = """
|
| 146 |
+
Return whether the argument is the [`Annotated`][typing.Annotated] [special form][].
|
| 147 |
+
|
| 148 |
+
```pycon
|
| 149 |
+
>>> is_annotated(Annotated)
|
| 150 |
+
True
|
| 151 |
+
>>> is_annotated(Annotated[int, ...])
|
| 152 |
+
False
|
| 153 |
+
```
|
| 154 |
+
"""
|
| 155 |
+
|
| 156 |
+
is_any = _compile_identity_check_function('Any', 'is_any')
|
| 157 |
+
is_any.__doc__ = """
|
| 158 |
+
Return whether the argument is the [`Any`][typing.Any] [special form][].
|
| 159 |
+
|
| 160 |
+
```pycon
|
| 161 |
+
>>> is_any(Any)
|
| 162 |
+
True
|
| 163 |
+
```
|
| 164 |
+
"""
|
| 165 |
+
|
| 166 |
+
is_classvar = _compile_identity_check_function('ClassVar', 'is_classvar')
|
| 167 |
+
is_classvar.__doc__ = """
|
| 168 |
+
Return whether the argument is the [`ClassVar`][typing.ClassVar] [type qualifier][].
|
| 169 |
+
|
| 170 |
+
```pycon
|
| 171 |
+
>>> is_classvar(ClassVar)
|
| 172 |
+
True
|
| 173 |
+
>>> is_classvar(ClassVar[int])
|
| 174 |
+
>>> False
|
| 175 |
+
```
|
| 176 |
+
"""
|
| 177 |
+
|
| 178 |
+
is_concatenate = _compile_identity_check_function('Concatenate', 'is_concatenate')
|
| 179 |
+
is_concatenate.__doc__ = """
|
| 180 |
+
Return whether the argument is the [`Concatenate`][typing.Concatenate] [special form][].
|
| 181 |
+
|
| 182 |
+
```pycon
|
| 183 |
+
>>> is_concatenate(Concatenate)
|
| 184 |
+
True
|
| 185 |
+
>>> is_concatenate(Concatenate[int, P])
|
| 186 |
+
False
|
| 187 |
+
```
|
| 188 |
+
"""
|
| 189 |
+
|
| 190 |
+
is_final = _compile_identity_check_function('Final', 'is_final')
|
| 191 |
+
is_final.__doc__ = """
|
| 192 |
+
Return whether the argument is the [`Final`][typing.Final] [type qualifier][].
|
| 193 |
+
|
| 194 |
+
```pycon
|
| 195 |
+
>>> is_final(Final)
|
| 196 |
+
True
|
| 197 |
+
>>> is_final(Final[int])
|
| 198 |
+
False
|
| 199 |
+
```
|
| 200 |
+
"""
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
# Unlikely to have a different version in `typing-extensions`, but keep it consistent.
|
| 204 |
+
# Also note that starting in 3.14, this is an alias to `annotationlib.ForwardRef`, but
|
| 205 |
+
# accessing it from `typing` doesn't seem to be deprecated.
|
| 206 |
+
is_forwardref = _compile_isinstance_check_function('ForwardRef', 'is_forwardref')
|
| 207 |
+
is_forwardref.__doc__ = """
|
| 208 |
+
Return whether the argument is an instance of [`ForwardRef`][typing.ForwardRef].
|
| 209 |
+
|
| 210 |
+
```pycon
|
| 211 |
+
>>> is_forwardref(ForwardRef('T'))
|
| 212 |
+
True
|
| 213 |
+
```
|
| 214 |
+
"""
|
| 215 |
+
|
| 216 |
+
|
| 217 |
+
is_generic = _compile_identity_check_function('Generic', 'is_generic')
|
| 218 |
+
is_generic.__doc__ = """
|
| 219 |
+
Return whether the argument is the [`Generic`][typing.Generic] [special form][].
|
| 220 |
+
|
| 221 |
+
```pycon
|
| 222 |
+
>>> is_generic(Generic)
|
| 223 |
+
True
|
| 224 |
+
>>> is_generic(Generic[T])
|
| 225 |
+
False
|
| 226 |
+
```
|
| 227 |
+
"""
|
| 228 |
+
|
| 229 |
+
is_literal = _compile_identity_check_function('Literal', 'is_literal')
|
| 230 |
+
is_literal.__doc__ = """
|
| 231 |
+
Return whether the argument is the [`Literal`][typing.Literal] [special form][].
|
| 232 |
+
|
| 233 |
+
```pycon
|
| 234 |
+
>>> is_literal(Literal)
|
| 235 |
+
True
|
| 236 |
+
>>> is_literal(Literal["a"])
|
| 237 |
+
False
|
| 238 |
+
```
|
| 239 |
+
"""
|
| 240 |
+
|
| 241 |
+
|
| 242 |
+
# `get_origin(Optional[int]) is Union`, so `is_optional()` isn't implemented.
|
| 243 |
+
|
| 244 |
+
is_paramspec = _compile_isinstance_check_function('ParamSpec', 'is_paramspec')
|
| 245 |
+
is_paramspec.__doc__ = """
|
| 246 |
+
Return whether the argument is an instance of [`ParamSpec`][typing.ParamSpec].
|
| 247 |
+
|
| 248 |
+
```pycon
|
| 249 |
+
>>> P = ParamSpec('P')
|
| 250 |
+
>>> is_paramspec(P)
|
| 251 |
+
True
|
| 252 |
+
```
|
| 253 |
+
"""
|
| 254 |
+
|
| 255 |
+
# Protocol?
|
| 256 |
+
|
| 257 |
+
is_typevar = _compile_isinstance_check_function('TypeVar', 'is_typevar')
|
| 258 |
+
is_typevar.__doc__ = """
|
| 259 |
+
Return whether the argument is an instance of [`TypeVar`][typing.TypeVar].
|
| 260 |
+
|
| 261 |
+
```pycon
|
| 262 |
+
>>> T = TypeVar('T')
|
| 263 |
+
>>> is_typevar(T)
|
| 264 |
+
True
|
| 265 |
+
```
|
| 266 |
+
"""
|
| 267 |
+
|
| 268 |
+
is_typevartuple = _compile_isinstance_check_function('TypeVarTuple', 'is_typevartuple')
|
| 269 |
+
is_typevartuple.__doc__ = """
|
| 270 |
+
Return whether the argument is an instance of [`TypeVarTuple`][typing.TypeVarTuple].
|
| 271 |
+
|
| 272 |
+
```pycon
|
| 273 |
+
>>> Ts = TypeVarTuple('Ts')
|
| 274 |
+
>>> is_typevartuple(Ts)
|
| 275 |
+
True
|
| 276 |
+
```
|
| 277 |
+
"""
|
| 278 |
+
|
| 279 |
+
is_union = _compile_identity_check_function('Union', 'is_union')
|
| 280 |
+
is_union.__doc__ = """
|
| 281 |
+
Return whether the argument is the [`Union`][typing.Union] [special form][].
|
| 282 |
+
|
| 283 |
+
This function can also be used to check for the [`Optional`][typing.Optional] [special form][],
|
| 284 |
+
as at runtime, `Optional[int]` is equivalent to `Union[int, None]`.
|
| 285 |
+
|
| 286 |
+
```pycon
|
| 287 |
+
>>> is_union(Union)
|
| 288 |
+
True
|
| 289 |
+
>>> is_union(Union[int, str])
|
| 290 |
+
False
|
| 291 |
+
```
|
| 292 |
+
|
| 293 |
+
!!! warning
|
| 294 |
+
This does not check for unions using the [new syntax][types-union] (e.g. `int | str`).
|
| 295 |
+
"""
|
| 296 |
+
|
| 297 |
+
|
| 298 |
+
def is_namedtuple(obj: Any, /) -> bool:
|
| 299 |
+
"""Return whether the argument is a named tuple type.
|
| 300 |
+
|
| 301 |
+
This includes [`NamedTuple`][typing.NamedTuple] subclasses and classes created from the
|
| 302 |
+
[`collections.namedtuple`][] factory function.
|
| 303 |
+
|
| 304 |
+
```pycon
|
| 305 |
+
>>> class User(NamedTuple):
|
| 306 |
+
... name: str
|
| 307 |
+
...
|
| 308 |
+
>>> is_namedtuple(User)
|
| 309 |
+
True
|
| 310 |
+
>>> City = collections.namedtuple('City', [])
|
| 311 |
+
>>> is_namedtuple(City)
|
| 312 |
+
True
|
| 313 |
+
>>> is_namedtuple(NamedTuple)
|
| 314 |
+
False
|
| 315 |
+
```
|
| 316 |
+
"""
|
| 317 |
+
return isinstance(obj, type) and issubclass(obj, tuple) and hasattr(obj, '_fields') # pyright: ignore[reportUnknownArgumentType]
|
| 318 |
+
|
| 319 |
+
|
| 320 |
+
# TypedDict?
|
| 321 |
+
|
| 322 |
+
# BinaryIO? IO? TextIO?
|
| 323 |
+
|
| 324 |
+
is_literalstring = _compile_identity_check_function('LiteralString', 'is_literalstring')
|
| 325 |
+
is_literalstring.__doc__ = """
|
| 326 |
+
Return whether the argument is the [`LiteralString`][typing.LiteralString] [special form][].
|
| 327 |
+
|
| 328 |
+
```pycon
|
| 329 |
+
>>> is_literalstring(LiteralString)
|
| 330 |
+
True
|
| 331 |
+
```
|
| 332 |
+
"""
|
| 333 |
+
|
| 334 |
+
is_never = _compile_identity_check_function('Never', 'is_never')
|
| 335 |
+
is_never.__doc__ = """
|
| 336 |
+
Return whether the argument is the [`Never`][typing.Never] [special form][].
|
| 337 |
+
|
| 338 |
+
```pycon
|
| 339 |
+
>>> is_never(Never)
|
| 340 |
+
True
|
| 341 |
+
```
|
| 342 |
+
"""
|
| 343 |
+
|
| 344 |
+
if sys.version_info >= (3, 10):
|
| 345 |
+
is_newtype = _compile_isinstance_check_function('NewType', 'is_newtype')
|
| 346 |
+
else: # On Python 3.10, `NewType` is a function.
|
| 347 |
+
|
| 348 |
+
def is_newtype(obj: Any, /) -> bool:
|
| 349 |
+
return hasattr(obj, '__supertype__')
|
| 350 |
+
|
| 351 |
+
|
| 352 |
+
is_newtype.__doc__ = """
|
| 353 |
+
Return whether the argument is a [`NewType`][typing.NewType].
|
| 354 |
+
|
| 355 |
+
```pycon
|
| 356 |
+
>>> UserId = NewType("UserId", int)
|
| 357 |
+
>>> is_newtype(UserId)
|
| 358 |
+
True
|
| 359 |
+
```
|
| 360 |
+
"""
|
| 361 |
+
|
| 362 |
+
is_nodefault = _compile_identity_check_function('NoDefault', 'is_nodefault')
|
| 363 |
+
is_nodefault.__doc__ = """
|
| 364 |
+
Return whether the argument is the [`NoDefault`][typing.NoDefault] sentinel object.
|
| 365 |
+
|
| 366 |
+
```pycon
|
| 367 |
+
>>> is_nodefault(NoDefault)
|
| 368 |
+
True
|
| 369 |
+
```
|
| 370 |
+
"""
|
| 371 |
+
|
| 372 |
+
is_noextraitems = _compile_identity_check_function('NoExtraItems', 'is_noextraitems')
|
| 373 |
+
is_noextraitems.__doc__ = """
|
| 374 |
+
Return whether the argument is the `NoExtraItems` sentinel object.
|
| 375 |
+
|
| 376 |
+
```pycon
|
| 377 |
+
>>> is_noextraitems(NoExtraItems)
|
| 378 |
+
True
|
| 379 |
+
```
|
| 380 |
+
"""
|
| 381 |
+
|
| 382 |
+
is_noreturn = _compile_identity_check_function('NoReturn', 'is_noreturn')
|
| 383 |
+
is_noreturn.__doc__ = """
|
| 384 |
+
Return whether the argument is the [`NoReturn`][typing.NoReturn] [special form][].
|
| 385 |
+
|
| 386 |
+
```pycon
|
| 387 |
+
>>> is_noreturn(NoReturn)
|
| 388 |
+
True
|
| 389 |
+
>>> is_noreturn(Never)
|
| 390 |
+
False
|
| 391 |
+
```
|
| 392 |
+
"""
|
| 393 |
+
|
| 394 |
+
is_notrequired = _compile_identity_check_function('NotRequired', 'is_notrequired')
|
| 395 |
+
is_notrequired.__doc__ = """
|
| 396 |
+
Return whether the argument is the [`NotRequired`][typing.NotRequired] [special form][].
|
| 397 |
+
|
| 398 |
+
```pycon
|
| 399 |
+
>>> is_notrequired(NotRequired)
|
| 400 |
+
True
|
| 401 |
+
```
|
| 402 |
+
"""
|
| 403 |
+
|
| 404 |
+
is_paramspecargs = _compile_isinstance_check_function('ParamSpecArgs', 'is_paramspecargs')
|
| 405 |
+
is_paramspecargs.__doc__ = """
|
| 406 |
+
Return whether the argument is an instance of [`ParamSpecArgs`][typing.ParamSpecArgs].
|
| 407 |
+
|
| 408 |
+
```pycon
|
| 409 |
+
>>> P = ParamSpec('P')
|
| 410 |
+
>>> is_paramspecargs(P.args)
|
| 411 |
+
True
|
| 412 |
+
```
|
| 413 |
+
"""
|
| 414 |
+
|
| 415 |
+
is_paramspeckwargs = _compile_isinstance_check_function('ParamSpecKwargs', 'is_paramspeckwargs')
|
| 416 |
+
is_paramspeckwargs.__doc__ = """
|
| 417 |
+
Return whether the argument is an instance of [`ParamSpecKwargs`][typing.ParamSpecKwargs].
|
| 418 |
+
|
| 419 |
+
```pycon
|
| 420 |
+
>>> P = ParamSpec('P')
|
| 421 |
+
>>> is_paramspeckwargs(P.kwargs)
|
| 422 |
+
True
|
| 423 |
+
```
|
| 424 |
+
"""
|
| 425 |
+
|
| 426 |
+
is_readonly = _compile_identity_check_function('ReadOnly', 'is_readonly')
|
| 427 |
+
is_readonly.__doc__ = """
|
| 428 |
+
Return whether the argument is the [`ReadOnly`][typing.ReadOnly] [special form][].
|
| 429 |
+
|
| 430 |
+
```pycon
|
| 431 |
+
>>> is_readonly(ReadOnly)
|
| 432 |
+
True
|
| 433 |
+
```
|
| 434 |
+
"""
|
| 435 |
+
|
| 436 |
+
is_required = _compile_identity_check_function('Required', 'is_required')
|
| 437 |
+
is_required.__doc__ = """
|
| 438 |
+
Return whether the argument is the [`Required`][typing.Required] [special form][].
|
| 439 |
+
|
| 440 |
+
```pycon
|
| 441 |
+
>>> is_required(Required)
|
| 442 |
+
True
|
| 443 |
+
```
|
| 444 |
+
"""
|
| 445 |
+
|
| 446 |
+
is_self = _compile_identity_check_function('Self', 'is_self')
|
| 447 |
+
is_self.__doc__ = """
|
| 448 |
+
Return whether the argument is the [`Self`][typing.Self] [special form][].
|
| 449 |
+
|
| 450 |
+
```pycon
|
| 451 |
+
>>> is_self(Self)
|
| 452 |
+
True
|
| 453 |
+
```
|
| 454 |
+
"""
|
| 455 |
+
|
| 456 |
+
# TYPE_CHECKING?
|
| 457 |
+
|
| 458 |
+
is_typealias = _compile_identity_check_function('TypeAlias', 'is_typealias')
|
| 459 |
+
is_typealias.__doc__ = """
|
| 460 |
+
Return whether the argument is the [`TypeAlias`][typing.TypeAlias] [special form][].
|
| 461 |
+
|
| 462 |
+
```pycon
|
| 463 |
+
>>> is_typealias(TypeAlias)
|
| 464 |
+
True
|
| 465 |
+
```
|
| 466 |
+
"""
|
| 467 |
+
|
| 468 |
+
is_typeguard = _compile_identity_check_function('TypeGuard', 'is_typeguard')
|
| 469 |
+
is_typeguard.__doc__ = """
|
| 470 |
+
Return whether the argument is the [`TypeGuard`][typing.TypeGuard] [special form][].
|
| 471 |
+
|
| 472 |
+
```pycon
|
| 473 |
+
>>> is_typeguard(TypeGuard)
|
| 474 |
+
True
|
| 475 |
+
```
|
| 476 |
+
"""
|
| 477 |
+
|
| 478 |
+
is_typeis = _compile_identity_check_function('TypeIs', 'is_typeis')
|
| 479 |
+
is_typeis.__doc__ = """
|
| 480 |
+
Return whether the argument is the [`TypeIs`][typing.TypeIs] [special form][].
|
| 481 |
+
|
| 482 |
+
```pycon
|
| 483 |
+
>>> is_typeis(TypeIs)
|
| 484 |
+
True
|
| 485 |
+
```
|
| 486 |
+
"""
|
| 487 |
+
|
| 488 |
+
_is_typealiastype_inner = _compile_isinstance_check_function('TypeAliasType', '_is_typealiastype_inner')
|
| 489 |
+
|
| 490 |
+
|
| 491 |
+
if _IS_PY310:
|
| 492 |
+
# Parameterized PEP 695 type aliases are instances of `types.GenericAlias` in typing_extensions>=4.13.0.
|
| 493 |
+
# On Python 3.10, with `Alias[int]` being such an instance of `GenericAlias`,
|
| 494 |
+
# `isinstance(Alias[int], TypeAliasType)` returns `True`.
|
| 495 |
+
# See https://github.com/python/cpython/issues/89828.
|
| 496 |
+
def is_typealiastype(obj: Any, /) -> 'TypeIs[TypeAliasType]':
|
| 497 |
+
return type(obj) is not GenericAlias and _is_typealiastype_inner(obj)
|
| 498 |
+
else:
|
| 499 |
+
is_typealiastype = _compile_isinstance_check_function('TypeAliasType', 'is_typealiastype')
|
| 500 |
+
|
| 501 |
+
is_typealiastype.__doc__ = """
|
| 502 |
+
Return whether the argument is a [`TypeAliasType`][typing.TypeAliasType] instance.
|
| 503 |
+
|
| 504 |
+
```pycon
|
| 505 |
+
>>> type MyInt = int
|
| 506 |
+
>>> is_typealiastype(MyInt)
|
| 507 |
+
True
|
| 508 |
+
>>> MyStr = TypeAliasType("MyStr", str)
|
| 509 |
+
>>> is_typealiastype(MyStr):
|
| 510 |
+
True
|
| 511 |
+
>>> type MyList[T] = list[T]
|
| 512 |
+
>>> is_typealiastype(MyList[int])
|
| 513 |
+
False
|
| 514 |
+
```
|
| 515 |
+
"""
|
| 516 |
+
|
| 517 |
+
is_unpack = _compile_identity_check_function('Unpack', 'is_unpack')
|
| 518 |
+
is_unpack.__doc__ = """
|
| 519 |
+
Return whether the argument is the [`Unpack`][typing.Unpack] [special form][].
|
| 520 |
+
|
| 521 |
+
```pycon
|
| 522 |
+
>>> is_unpack(Unpack)
|
| 523 |
+
True
|
| 524 |
+
>>> is_unpack(Unpack[Ts])
|
| 525 |
+
False
|
| 526 |
+
```
|
| 527 |
+
"""
|
| 528 |
+
|
| 529 |
+
|
| 530 |
+
if sys.version_info >= (3, 13):
|
| 531 |
+
|
| 532 |
+
def is_deprecated(obj: Any, /) -> 'TypeIs[deprecated]':
|
| 533 |
+
return isinstance(obj, (warnings.deprecated, typing_extensions.deprecated))
|
| 534 |
+
|
| 535 |
+
else:
|
| 536 |
+
|
| 537 |
+
def is_deprecated(obj: Any, /) -> 'TypeIs[deprecated]':
|
| 538 |
+
return isinstance(obj, typing_extensions.deprecated)
|
| 539 |
+
|
| 540 |
+
|
| 541 |
+
is_deprecated.__doc__ = """
|
| 542 |
+
Return whether the argument is a [`deprecated`][warnings.deprecated] instance.
|
| 543 |
+
|
| 544 |
+
This also includes the [`typing_extensions` backport][typing_extensions.deprecated].
|
| 545 |
+
|
| 546 |
+
```pycon
|
| 547 |
+
>>> is_deprecated(warnings.deprecated('message'))
|
| 548 |
+
True
|
| 549 |
+
>>> is_deprecated(typing_extensions.deprecated('message'))
|
| 550 |
+
True
|
| 551 |
+
```
|
| 552 |
+
"""
|
| 553 |
+
|
| 554 |
+
|
| 555 |
+
# Aliases defined in the `typing` module using `typing._SpecialGenericAlias` (itself aliased as `alias()`):
|
| 556 |
+
DEPRECATED_ALIASES: Final[dict[Any, type[Any]]] = {
|
| 557 |
+
typing.Hashable: collections.abc.Hashable,
|
| 558 |
+
typing.Awaitable: collections.abc.Awaitable,
|
| 559 |
+
typing.Coroutine: collections.abc.Coroutine,
|
| 560 |
+
typing.AsyncIterable: collections.abc.AsyncIterable,
|
| 561 |
+
typing.AsyncIterator: collections.abc.AsyncIterator,
|
| 562 |
+
typing.Iterable: collections.abc.Iterable,
|
| 563 |
+
typing.Iterator: collections.abc.Iterator,
|
| 564 |
+
typing.Reversible: collections.abc.Reversible,
|
| 565 |
+
typing.Sized: collections.abc.Sized,
|
| 566 |
+
typing.Container: collections.abc.Container,
|
| 567 |
+
typing.Collection: collections.abc.Collection,
|
| 568 |
+
# type ignore reason: https://github.com/python/typeshed/issues/6257:
|
| 569 |
+
typing.Callable: collections.abc.Callable, # pyright: ignore[reportAssignmentType, reportUnknownMemberType]
|
| 570 |
+
typing.AbstractSet: collections.abc.Set,
|
| 571 |
+
typing.MutableSet: collections.abc.MutableSet,
|
| 572 |
+
typing.Mapping: collections.abc.Mapping,
|
| 573 |
+
typing.MutableMapping: collections.abc.MutableMapping,
|
| 574 |
+
typing.Sequence: collections.abc.Sequence,
|
| 575 |
+
typing.MutableSequence: collections.abc.MutableSequence,
|
| 576 |
+
typing.Tuple: tuple,
|
| 577 |
+
typing.List: list,
|
| 578 |
+
typing.Deque: collections.deque,
|
| 579 |
+
typing.Set: set,
|
| 580 |
+
typing.FrozenSet: frozenset,
|
| 581 |
+
typing.MappingView: collections.abc.MappingView,
|
| 582 |
+
typing.KeysView: collections.abc.KeysView,
|
| 583 |
+
typing.ItemsView: collections.abc.ItemsView,
|
| 584 |
+
typing.ValuesView: collections.abc.ValuesView,
|
| 585 |
+
typing.Dict: dict,
|
| 586 |
+
typing.DefaultDict: collections.defaultdict,
|
| 587 |
+
typing.OrderedDict: collections.OrderedDict,
|
| 588 |
+
typing.Counter: collections.Counter,
|
| 589 |
+
typing.ChainMap: collections.ChainMap,
|
| 590 |
+
typing.Generator: collections.abc.Generator,
|
| 591 |
+
typing.AsyncGenerator: collections.abc.AsyncGenerator,
|
| 592 |
+
typing.Type: type,
|
| 593 |
+
# Defined in `typing.__getattr__`:
|
| 594 |
+
typing.Pattern: re.Pattern,
|
| 595 |
+
typing.Match: re.Match,
|
| 596 |
+
typing.ContextManager: contextlib.AbstractContextManager,
|
| 597 |
+
typing.AsyncContextManager: contextlib.AbstractAsyncContextManager,
|
| 598 |
+
# Skipped: `ByteString` (deprecated, removed in 3.14)
|
| 599 |
+
}
|
| 600 |
+
"""A mapping between the deprecated typing aliases to their replacement, as per [PEP 585](https://peps.python.org/pep-0585/)."""
|
| 601 |
+
|
| 602 |
+
|
| 603 |
+
# Add the `typing_extensions` aliases:
|
| 604 |
+
for alias, target in list(DEPRECATED_ALIASES.items()):
|
| 605 |
+
# Use `alias.__name__` when we drop support for Python 3.9
|
| 606 |
+
if (te_alias := getattr(typing_extensions, alias._name, None)) is not None:
|
| 607 |
+
DEPRECATED_ALIASES[te_alias] = target
|
loan-advisor-agent/.venv/Lib/site-packages/typing_inspection/typing_objects.pyi
ADDED
|
@@ -0,0 +1,417 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
# `stubgen --inspect-mode --include-docstrings -m typing_inspection.typing_objects`
|
| 3 |
+
# (manual edits need to be applied).
|
| 4 |
+
"""Low-level introspection utilities for [`typing`][] members.
|
| 5 |
+
|
| 6 |
+
The provided functions in this module check against both the [`typing`][] and [`typing_extensions`][]
|
| 7 |
+
variants, if they exists and are different.
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import sys
|
| 11 |
+
from typing import Any, Final, ForwardRef, NewType, TypeVar
|
| 12 |
+
|
| 13 |
+
from typing_extensions import ParamSpec, ParamSpecArgs, ParamSpecKwargs, TypeAliasType, TypeIs, TypeVarTuple, deprecated
|
| 14 |
+
|
| 15 |
+
__all__ = [
|
| 16 |
+
'DEPRECATED_ALIASES',
|
| 17 |
+
'NoneType',
|
| 18 |
+
'is_annotated',
|
| 19 |
+
'is_any',
|
| 20 |
+
'is_classvar',
|
| 21 |
+
'is_concatenate',
|
| 22 |
+
'is_deprecated',
|
| 23 |
+
'is_final',
|
| 24 |
+
'is_generic',
|
| 25 |
+
'is_literal',
|
| 26 |
+
'is_literalstring',
|
| 27 |
+
'is_namedtuple',
|
| 28 |
+
'is_never',
|
| 29 |
+
'is_newtype',
|
| 30 |
+
'is_nodefault',
|
| 31 |
+
'is_noextraitems',
|
| 32 |
+
'is_noreturn',
|
| 33 |
+
'is_notrequired',
|
| 34 |
+
'is_paramspec',
|
| 35 |
+
'is_paramspecargs',
|
| 36 |
+
'is_paramspeckwargs',
|
| 37 |
+
'is_readonly',
|
| 38 |
+
'is_required',
|
| 39 |
+
'is_self',
|
| 40 |
+
'is_typealias',
|
| 41 |
+
'is_typealiastype',
|
| 42 |
+
'is_typeguard',
|
| 43 |
+
'is_typeis',
|
| 44 |
+
'is_typevar',
|
| 45 |
+
'is_typevartuple',
|
| 46 |
+
'is_union',
|
| 47 |
+
'is_unpack',
|
| 48 |
+
]
|
| 49 |
+
|
| 50 |
+
if sys.version_info >= (3, 10):
|
| 51 |
+
from types import NoneType
|
| 52 |
+
else:
|
| 53 |
+
NoneType = type(None)
|
| 54 |
+
|
| 55 |
+
def is_annotated(obj: Any, /) -> bool:
|
| 56 |
+
"""
|
| 57 |
+
Return whether the argument is the [`Annotated`][typing.Annotated] [special form][].
|
| 58 |
+
|
| 59 |
+
```pycon
|
| 60 |
+
>>> is_annotated(Annotated)
|
| 61 |
+
True
|
| 62 |
+
>>> is_annotated(Annotated[int, ...])
|
| 63 |
+
False
|
| 64 |
+
```
|
| 65 |
+
"""
|
| 66 |
+
|
| 67 |
+
def is_any(obj: Any, /) -> bool:
|
| 68 |
+
"""
|
| 69 |
+
Return whether the argument is the [`Any`][typing.Any] [special form][].
|
| 70 |
+
|
| 71 |
+
```pycon
|
| 72 |
+
>>> is_any(Any)
|
| 73 |
+
True
|
| 74 |
+
```
|
| 75 |
+
"""
|
| 76 |
+
|
| 77 |
+
def is_classvar(obj: Any, /) -> bool:
|
| 78 |
+
"""
|
| 79 |
+
Return whether the argument is the [`ClassVar`][typing.ClassVar] [type qualifier][].
|
| 80 |
+
|
| 81 |
+
```pycon
|
| 82 |
+
>>> is_classvar(ClassVar)
|
| 83 |
+
True
|
| 84 |
+
>>> is_classvar(ClassVar[int])
|
| 85 |
+
>>> False
|
| 86 |
+
```
|
| 87 |
+
"""
|
| 88 |
+
|
| 89 |
+
def is_concatenate(obj: Any, /) -> bool:
|
| 90 |
+
"""
|
| 91 |
+
Return whether the argument is the [`Concatenate`][typing.Concatenate] [special form][].
|
| 92 |
+
|
| 93 |
+
```pycon
|
| 94 |
+
>>> is_concatenate(Concatenate)
|
| 95 |
+
True
|
| 96 |
+
>>> is_concatenate(Concatenate[int, P])
|
| 97 |
+
False
|
| 98 |
+
```
|
| 99 |
+
"""
|
| 100 |
+
|
| 101 |
+
def is_final(obj: Any, /) -> bool:
|
| 102 |
+
"""
|
| 103 |
+
Return whether the argument is the [`Final`][typing.Final] [type qualifier][].
|
| 104 |
+
|
| 105 |
+
```pycon
|
| 106 |
+
>>> is_final(Final)
|
| 107 |
+
True
|
| 108 |
+
>>> is_final(Final[int])
|
| 109 |
+
False
|
| 110 |
+
```
|
| 111 |
+
"""
|
| 112 |
+
|
| 113 |
+
def is_forwardref(obj: Any, /) -> TypeIs[ForwardRef]:
|
| 114 |
+
"""
|
| 115 |
+
Return whether the argument is an instance of [`ForwardRef`][typing.ForwardRef].
|
| 116 |
+
|
| 117 |
+
```pycon
|
| 118 |
+
>>> is_forwardref(ForwardRef('T'))
|
| 119 |
+
True
|
| 120 |
+
```
|
| 121 |
+
"""
|
| 122 |
+
|
| 123 |
+
def is_generic(obj: Any, /) -> bool:
|
| 124 |
+
"""
|
| 125 |
+
Return whether the argument is the [`Generic`][typing.Generic] [special form][].
|
| 126 |
+
|
| 127 |
+
```pycon
|
| 128 |
+
>>> is_generic(Generic)
|
| 129 |
+
True
|
| 130 |
+
>>> is_generic(Generic[T])
|
| 131 |
+
False
|
| 132 |
+
```
|
| 133 |
+
"""
|
| 134 |
+
|
| 135 |
+
def is_literal(obj: Any, /) -> bool:
|
| 136 |
+
"""
|
| 137 |
+
Return whether the argument is the [`Literal`][typing.Literal] [special form][].
|
| 138 |
+
|
| 139 |
+
```pycon
|
| 140 |
+
>>> is_literal(Literal)
|
| 141 |
+
True
|
| 142 |
+
>>> is_literal(Literal["a"])
|
| 143 |
+
False
|
| 144 |
+
```
|
| 145 |
+
"""
|
| 146 |
+
|
| 147 |
+
def is_paramspec(obj: Any, /) -> TypeIs[ParamSpec]:
|
| 148 |
+
"""
|
| 149 |
+
Return whether the argument is an instance of [`ParamSpec`][typing.ParamSpec].
|
| 150 |
+
|
| 151 |
+
```pycon
|
| 152 |
+
>>> P = ParamSpec('P')
|
| 153 |
+
>>> is_paramspec(P)
|
| 154 |
+
True
|
| 155 |
+
```
|
| 156 |
+
"""
|
| 157 |
+
|
| 158 |
+
def is_typevar(obj: Any, /) -> TypeIs[TypeVar]:
|
| 159 |
+
"""
|
| 160 |
+
Return whether the argument is an instance of [`TypeVar`][typing.TypeVar].
|
| 161 |
+
|
| 162 |
+
```pycon
|
| 163 |
+
>>> T = TypeVar('T')
|
| 164 |
+
>>> is_typevar(T)
|
| 165 |
+
True
|
| 166 |
+
```
|
| 167 |
+
"""
|
| 168 |
+
|
| 169 |
+
def is_typevartuple(obj: Any, /) -> TypeIs[TypeVarTuple]:
|
| 170 |
+
"""
|
| 171 |
+
Return whether the argument is an instance of [`TypeVarTuple`][typing.TypeVarTuple].
|
| 172 |
+
|
| 173 |
+
```pycon
|
| 174 |
+
>>> Ts = TypeVarTuple('Ts')
|
| 175 |
+
>>> is_typevartuple(Ts)
|
| 176 |
+
True
|
| 177 |
+
```
|
| 178 |
+
"""
|
| 179 |
+
|
| 180 |
+
def is_union(obj: Any, /) -> bool:
|
| 181 |
+
"""
|
| 182 |
+
Return whether the argument is the [`Union`][typing.Union] [special form][].
|
| 183 |
+
|
| 184 |
+
This function can also be used to check for the [`Optional`][typing.Optional] [special form][],
|
| 185 |
+
as at runtime, `Optional[int]` is equivalent to `Union[int, None]`.
|
| 186 |
+
|
| 187 |
+
```pycon
|
| 188 |
+
>>> is_union(Union)
|
| 189 |
+
True
|
| 190 |
+
>>> is_union(Union[int, str])
|
| 191 |
+
False
|
| 192 |
+
```
|
| 193 |
+
|
| 194 |
+
!!! warning
|
| 195 |
+
This does not check for unions using the [new syntax][types-union] (e.g. `int | str`).
|
| 196 |
+
"""
|
| 197 |
+
|
| 198 |
+
def is_namedtuple(obj: Any, /) -> bool:
|
| 199 |
+
"""Return whether the argument is a named tuple type.
|
| 200 |
+
|
| 201 |
+
This includes [`NamedTuple`][typing.NamedTuple] subclasses and classes created from the
|
| 202 |
+
[`collections.namedtuple`][] factory function.
|
| 203 |
+
|
| 204 |
+
```pycon
|
| 205 |
+
>>> class User(NamedTuple):
|
| 206 |
+
... name: str
|
| 207 |
+
...
|
| 208 |
+
>>> is_namedtuple(User)
|
| 209 |
+
True
|
| 210 |
+
>>> City = collections.namedtuple('City', [])
|
| 211 |
+
>>> is_namedtuple(City)
|
| 212 |
+
True
|
| 213 |
+
>>> is_namedtuple(NamedTuple)
|
| 214 |
+
False
|
| 215 |
+
```
|
| 216 |
+
"""
|
| 217 |
+
|
| 218 |
+
def is_literalstring(obj: Any, /) -> bool:
|
| 219 |
+
"""
|
| 220 |
+
Return whether the argument is the [`LiteralString`][typing.LiteralString] [special form][].
|
| 221 |
+
|
| 222 |
+
```pycon
|
| 223 |
+
>>> is_literalstring(LiteralString)
|
| 224 |
+
True
|
| 225 |
+
```
|
| 226 |
+
"""
|
| 227 |
+
|
| 228 |
+
def is_never(obj: Any, /) -> bool:
|
| 229 |
+
"""
|
| 230 |
+
Return whether the argument is the [`Never`][typing.Never] [special form][].
|
| 231 |
+
|
| 232 |
+
```pycon
|
| 233 |
+
>>> is_never(Never)
|
| 234 |
+
True
|
| 235 |
+
```
|
| 236 |
+
"""
|
| 237 |
+
|
| 238 |
+
def is_newtype(obj: Any, /) -> TypeIs[NewType]:
|
| 239 |
+
"""
|
| 240 |
+
Return whether the argument is a [`NewType`][typing.NewType].
|
| 241 |
+
|
| 242 |
+
```pycon
|
| 243 |
+
>>> UserId = NewType("UserId", int)
|
| 244 |
+
>>> is_newtype(UserId)
|
| 245 |
+
True
|
| 246 |
+
```
|
| 247 |
+
"""
|
| 248 |
+
|
| 249 |
+
def is_nodefault(obj: Any, /) -> bool:
|
| 250 |
+
"""
|
| 251 |
+
Return whether the argument is the [`NoDefault`][typing.NoDefault] sentinel object.
|
| 252 |
+
|
| 253 |
+
```pycon
|
| 254 |
+
>>> is_nodefault(NoDefault)
|
| 255 |
+
True
|
| 256 |
+
```
|
| 257 |
+
"""
|
| 258 |
+
|
| 259 |
+
def is_noextraitems(obj: Any, /) -> bool:
|
| 260 |
+
"""
|
| 261 |
+
Return whether the argument is the `NoExtraItems` sentinel object.
|
| 262 |
+
|
| 263 |
+
```pycon
|
| 264 |
+
>>> is_noextraitems(NoExtraItems)
|
| 265 |
+
True
|
| 266 |
+
```
|
| 267 |
+
"""
|
| 268 |
+
|
| 269 |
+
def is_noreturn(obj: Any, /) -> bool:
|
| 270 |
+
"""
|
| 271 |
+
Return whether the argument is the [`NoReturn`][typing.NoReturn] [special form][].
|
| 272 |
+
|
| 273 |
+
```pycon
|
| 274 |
+
>>> is_noreturn(NoReturn)
|
| 275 |
+
True
|
| 276 |
+
>>> is_noreturn(Never)
|
| 277 |
+
False
|
| 278 |
+
```
|
| 279 |
+
"""
|
| 280 |
+
|
| 281 |
+
def is_notrequired(obj: Any, /) -> bool:
|
| 282 |
+
"""
|
| 283 |
+
Return whether the argument is the [`NotRequired`][typing.NotRequired] [special form][].
|
| 284 |
+
|
| 285 |
+
```pycon
|
| 286 |
+
>>> is_notrequired(NotRequired)
|
| 287 |
+
True
|
| 288 |
+
```
|
| 289 |
+
"""
|
| 290 |
+
|
| 291 |
+
def is_paramspecargs(obj: Any, /) -> TypeIs[ParamSpecArgs]:
|
| 292 |
+
"""
|
| 293 |
+
Return whether the argument is an instance of [`ParamSpecArgs`][typing.ParamSpecArgs].
|
| 294 |
+
|
| 295 |
+
```pycon
|
| 296 |
+
>>> P = ParamSpec('P')
|
| 297 |
+
>>> is_paramspecargs(P.args)
|
| 298 |
+
True
|
| 299 |
+
```
|
| 300 |
+
"""
|
| 301 |
+
|
| 302 |
+
def is_paramspeckwargs(obj: Any, /) -> TypeIs[ParamSpecKwargs]:
|
| 303 |
+
"""
|
| 304 |
+
Return whether the argument is an instance of [`ParamSpecKwargs`][typing.ParamSpecKwargs].
|
| 305 |
+
|
| 306 |
+
```pycon
|
| 307 |
+
>>> P = ParamSpec('P')
|
| 308 |
+
>>> is_paramspeckwargs(P.kwargs)
|
| 309 |
+
True
|
| 310 |
+
```
|
| 311 |
+
"""
|
| 312 |
+
|
| 313 |
+
def is_readonly(obj: Any, /) -> bool:
|
| 314 |
+
"""
|
| 315 |
+
Return whether the argument is the [`ReadOnly`][typing.ReadOnly] [special form][].
|
| 316 |
+
|
| 317 |
+
```pycon
|
| 318 |
+
>>> is_readonly(ReadOnly)
|
| 319 |
+
True
|
| 320 |
+
```
|
| 321 |
+
"""
|
| 322 |
+
|
| 323 |
+
def is_required(obj: Any, /) -> bool:
|
| 324 |
+
"""
|
| 325 |
+
Return whether the argument is the [`Required`][typing.Required] [special form][].
|
| 326 |
+
|
| 327 |
+
```pycon
|
| 328 |
+
>>> is_required(Required)
|
| 329 |
+
True
|
| 330 |
+
```
|
| 331 |
+
"""
|
| 332 |
+
|
| 333 |
+
def is_self(obj: Any, /) -> bool:
|
| 334 |
+
"""
|
| 335 |
+
Return whether the argument is the [`Self`][typing.Self] [special form][].
|
| 336 |
+
|
| 337 |
+
```pycon
|
| 338 |
+
>>> is_self(Self)
|
| 339 |
+
True
|
| 340 |
+
```
|
| 341 |
+
"""
|
| 342 |
+
|
| 343 |
+
def is_typealias(obj: Any, /) -> bool:
|
| 344 |
+
"""
|
| 345 |
+
Return whether the argument is the [`TypeAlias`][typing.TypeAlias] [special form][].
|
| 346 |
+
|
| 347 |
+
```pycon
|
| 348 |
+
>>> is_typealias(TypeAlias)
|
| 349 |
+
True
|
| 350 |
+
```
|
| 351 |
+
"""
|
| 352 |
+
|
| 353 |
+
def is_typeguard(obj: Any, /) -> bool:
|
| 354 |
+
"""
|
| 355 |
+
Return whether the argument is the [`TypeGuard`][typing.TypeGuard] [special form][].
|
| 356 |
+
|
| 357 |
+
```pycon
|
| 358 |
+
>>> is_typeguard(TypeGuard)
|
| 359 |
+
True
|
| 360 |
+
```
|
| 361 |
+
"""
|
| 362 |
+
|
| 363 |
+
def is_typeis(obj: Any, /) -> bool:
|
| 364 |
+
"""
|
| 365 |
+
Return whether the argument is the [`TypeIs`][typing.TypeIs] [special form][].
|
| 366 |
+
|
| 367 |
+
```pycon
|
| 368 |
+
>>> is_typeis(TypeIs)
|
| 369 |
+
True
|
| 370 |
+
```
|
| 371 |
+
"""
|
| 372 |
+
|
| 373 |
+
def is_typealiastype(obj: Any, /) -> TypeIs[TypeAliasType]:
|
| 374 |
+
"""
|
| 375 |
+
Return whether the argument is a [`TypeAliasType`][typing.TypeAliasType] instance.
|
| 376 |
+
|
| 377 |
+
```pycon
|
| 378 |
+
>>> type MyInt = int
|
| 379 |
+
>>> is_typealiastype(MyInt)
|
| 380 |
+
True
|
| 381 |
+
>>> MyStr = TypeAliasType("MyStr", str)
|
| 382 |
+
>>> is_typealiastype(MyStr):
|
| 383 |
+
True
|
| 384 |
+
>>> type MyList[T] = list[T]
|
| 385 |
+
>>> is_typealiastype(MyList[int])
|
| 386 |
+
False
|
| 387 |
+
```
|
| 388 |
+
"""
|
| 389 |
+
|
| 390 |
+
def is_unpack(obj: Any, /) -> bool:
|
| 391 |
+
"""
|
| 392 |
+
Return whether the argument is the [`Unpack`][typing.Unpack] [special form][].
|
| 393 |
+
|
| 394 |
+
```pycon
|
| 395 |
+
>>> is_unpack(Unpack)
|
| 396 |
+
True
|
| 397 |
+
>>> is_unpack(Unpack[Ts])
|
| 398 |
+
False
|
| 399 |
+
```
|
| 400 |
+
"""
|
| 401 |
+
|
| 402 |
+
def is_deprecated(obj: Any, /) -> TypeIs[deprecated]:
|
| 403 |
+
"""
|
| 404 |
+
Return whether the argument is a [`deprecated`][warnings.deprecated] instance.
|
| 405 |
+
|
| 406 |
+
This also includes the [`typing_extensions` backport][typing_extensions.deprecated].
|
| 407 |
+
|
| 408 |
+
```pycon
|
| 409 |
+
>>> is_deprecated(warnings.deprecated('message'))
|
| 410 |
+
True
|
| 411 |
+
>>> is_deprecated(typing_extensions.deprecated('deprecated'))
|
| 412 |
+
True
|
| 413 |
+
```
|
| 414 |
+
"""
|
| 415 |
+
|
| 416 |
+
DEPRECATED_ALIASES: Final[dict[Any, type[Any]]]
|
| 417 |
+
"""A mapping between the deprecated typing aliases to their replacement, as per [PEP 585](https://peps.python.org/pep-0585/)."""
|