SQLAlchemy 2.0.36__cp313-cp313-win32.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- SQLAlchemy-2.0.36.dist-info/LICENSE +19 -0
- SQLAlchemy-2.0.36.dist-info/METADATA +243 -0
- SQLAlchemy-2.0.36.dist-info/RECORD +273 -0
- SQLAlchemy-2.0.36.dist-info/WHEEL +5 -0
- SQLAlchemy-2.0.36.dist-info/top_level.txt +1 -0
- sqlalchemy/__init__.py +294 -0
- sqlalchemy/connectors/__init__.py +18 -0
- sqlalchemy/connectors/aioodbc.py +174 -0
- sqlalchemy/connectors/asyncio.py +213 -0
- sqlalchemy/connectors/pyodbc.py +249 -0
- sqlalchemy/cyextension/__init__.py +6 -0
- sqlalchemy/cyextension/collections.cp313-win32.pyd +0 -0
- sqlalchemy/cyextension/collections.pyx +409 -0
- sqlalchemy/cyextension/immutabledict.cp313-win32.pyd +0 -0
- sqlalchemy/cyextension/immutabledict.pxd +8 -0
- sqlalchemy/cyextension/immutabledict.pyx +133 -0
- sqlalchemy/cyextension/processors.cp313-win32.pyd +0 -0
- sqlalchemy/cyextension/processors.pyx +68 -0
- sqlalchemy/cyextension/resultproxy.cp313-win32.pyd +0 -0
- sqlalchemy/cyextension/resultproxy.pyx +102 -0
- sqlalchemy/cyextension/util.cp313-win32.pyd +0 -0
- sqlalchemy/cyextension/util.pyx +91 -0
- sqlalchemy/dialects/__init__.py +61 -0
- sqlalchemy/dialects/_typing.py +25 -0
- sqlalchemy/dialects/mssql/__init__.py +88 -0
- sqlalchemy/dialects/mssql/aioodbc.py +64 -0
- sqlalchemy/dialects/mssql/base.py +4010 -0
- sqlalchemy/dialects/mssql/information_schema.py +254 -0
- sqlalchemy/dialects/mssql/json.py +133 -0
- sqlalchemy/dialects/mssql/provision.py +162 -0
- sqlalchemy/dialects/mssql/pymssql.py +126 -0
- sqlalchemy/dialects/mssql/pyodbc.py +745 -0
- sqlalchemy/dialects/mysql/__init__.py +101 -0
- sqlalchemy/dialects/mysql/aiomysql.py +333 -0
- sqlalchemy/dialects/mysql/asyncmy.py +337 -0
- sqlalchemy/dialects/mysql/base.py +3494 -0
- sqlalchemy/dialects/mysql/cymysql.py +84 -0
- sqlalchemy/dialects/mysql/dml.py +219 -0
- sqlalchemy/dialects/mysql/enumerated.py +244 -0
- sqlalchemy/dialects/mysql/expression.py +141 -0
- sqlalchemy/dialects/mysql/json.py +81 -0
- sqlalchemy/dialects/mysql/mariadb.py +32 -0
- sqlalchemy/dialects/mysql/mariadbconnector.py +277 -0
- sqlalchemy/dialects/mysql/mysqlconnector.py +180 -0
- sqlalchemy/dialects/mysql/mysqldb.py +303 -0
- sqlalchemy/dialects/mysql/provision.py +110 -0
- sqlalchemy/dialects/mysql/pymysql.py +137 -0
- sqlalchemy/dialects/mysql/pyodbc.py +138 -0
- sqlalchemy/dialects/mysql/reflection.py +677 -0
- sqlalchemy/dialects/mysql/reserved_words.py +571 -0
- sqlalchemy/dialects/mysql/types.py +774 -0
- sqlalchemy/dialects/oracle/__init__.py +67 -0
- sqlalchemy/dialects/oracle/base.py +3271 -0
- sqlalchemy/dialects/oracle/cx_oracle.py +1483 -0
- sqlalchemy/dialects/oracle/dictionary.py +507 -0
- sqlalchemy/dialects/oracle/oracledb.py +431 -0
- sqlalchemy/dialects/oracle/provision.py +220 -0
- sqlalchemy/dialects/oracle/types.py +287 -0
- sqlalchemy/dialects/postgresql/__init__.py +167 -0
- sqlalchemy/dialects/postgresql/_psycopg_common.py +187 -0
- sqlalchemy/dialects/postgresql/array.py +425 -0
- sqlalchemy/dialects/postgresql/asyncpg.py +1274 -0
- sqlalchemy/dialects/postgresql/base.py +5008 -0
- sqlalchemy/dialects/postgresql/dml.py +310 -0
- sqlalchemy/dialects/postgresql/ext.py +496 -0
- sqlalchemy/dialects/postgresql/hstore.py +397 -0
- sqlalchemy/dialects/postgresql/json.py +333 -0
- sqlalchemy/dialects/postgresql/named_types.py +509 -0
- sqlalchemy/dialects/postgresql/operators.py +129 -0
- sqlalchemy/dialects/postgresql/pg8000.py +662 -0
- sqlalchemy/dialects/postgresql/pg_catalog.py +300 -0
- sqlalchemy/dialects/postgresql/provision.py +175 -0
- sqlalchemy/dialects/postgresql/psycopg.py +772 -0
- sqlalchemy/dialects/postgresql/psycopg2.py +886 -0
- sqlalchemy/dialects/postgresql/psycopg2cffi.py +61 -0
- sqlalchemy/dialects/postgresql/ranges.py +1029 -0
- sqlalchemy/dialects/postgresql/types.py +303 -0
- sqlalchemy/dialects/sqlite/__init__.py +57 -0
- sqlalchemy/dialects/sqlite/aiosqlite.py +396 -0
- sqlalchemy/dialects/sqlite/base.py +2805 -0
- sqlalchemy/dialects/sqlite/dml.py +240 -0
- sqlalchemy/dialects/sqlite/json.py +92 -0
- sqlalchemy/dialects/sqlite/provision.py +198 -0
- sqlalchemy/dialects/sqlite/pysqlcipher.py +155 -0
- sqlalchemy/dialects/sqlite/pysqlite.py +756 -0
- sqlalchemy/dialects/type_migration_guidelines.txt +145 -0
- sqlalchemy/engine/__init__.py +62 -0
- sqlalchemy/engine/_py_processors.py +136 -0
- sqlalchemy/engine/_py_row.py +128 -0
- sqlalchemy/engine/_py_util.py +74 -0
- sqlalchemy/engine/base.py +3375 -0
- sqlalchemy/engine/characteristics.py +155 -0
- sqlalchemy/engine/create.py +875 -0
- sqlalchemy/engine/cursor.py +2181 -0
- sqlalchemy/engine/default.py +2365 -0
- sqlalchemy/engine/events.py +951 -0
- sqlalchemy/engine/interfaces.py +3403 -0
- sqlalchemy/engine/mock.py +131 -0
- sqlalchemy/engine/processors.py +61 -0
- sqlalchemy/engine/reflection.py +2098 -0
- sqlalchemy/engine/result.py +2382 -0
- sqlalchemy/engine/row.py +401 -0
- sqlalchemy/engine/strategies.py +19 -0
- sqlalchemy/engine/url.py +910 -0
- sqlalchemy/engine/util.py +167 -0
- sqlalchemy/event/__init__.py +25 -0
- sqlalchemy/event/api.py +225 -0
- sqlalchemy/event/attr.py +655 -0
- sqlalchemy/event/base.py +470 -0
- sqlalchemy/event/legacy.py +246 -0
- sqlalchemy/event/registry.py +386 -0
- sqlalchemy/events.py +17 -0
- sqlalchemy/exc.py +830 -0
- sqlalchemy/ext/__init__.py +11 -0
- sqlalchemy/ext/associationproxy.py +2013 -0
- sqlalchemy/ext/asyncio/__init__.py +25 -0
- sqlalchemy/ext/asyncio/base.py +279 -0
- sqlalchemy/ext/asyncio/engine.py +1466 -0
- sqlalchemy/ext/asyncio/exc.py +21 -0
- sqlalchemy/ext/asyncio/result.py +961 -0
- sqlalchemy/ext/asyncio/scoping.py +1614 -0
- sqlalchemy/ext/asyncio/session.py +1936 -0
- sqlalchemy/ext/automap.py +1691 -0
- sqlalchemy/ext/baked.py +574 -0
- sqlalchemy/ext/compiler.py +570 -0
- sqlalchemy/ext/declarative/__init__.py +65 -0
- sqlalchemy/ext/declarative/extensions.py +548 -0
- sqlalchemy/ext/horizontal_shard.py +481 -0
- sqlalchemy/ext/hybrid.py +1514 -0
- sqlalchemy/ext/indexable.py +341 -0
- sqlalchemy/ext/instrumentation.py +450 -0
- sqlalchemy/ext/mutable.py +1073 -0
- sqlalchemy/ext/mypy/__init__.py +6 -0
- sqlalchemy/ext/mypy/apply.py +320 -0
- sqlalchemy/ext/mypy/decl_class.py +515 -0
- sqlalchemy/ext/mypy/infer.py +590 -0
- sqlalchemy/ext/mypy/names.py +335 -0
- sqlalchemy/ext/mypy/plugin.py +303 -0
- sqlalchemy/ext/mypy/util.py +357 -0
- sqlalchemy/ext/orderinglist.py +416 -0
- sqlalchemy/ext/serializer.py +181 -0
- sqlalchemy/future/__init__.py +16 -0
- sqlalchemy/future/engine.py +15 -0
- sqlalchemy/inspection.py +174 -0
- sqlalchemy/log.py +288 -0
- sqlalchemy/orm/__init__.py +170 -0
- sqlalchemy/orm/_orm_constructors.py +2571 -0
- sqlalchemy/orm/_typing.py +179 -0
- sqlalchemy/orm/attributes.py +2835 -0
- sqlalchemy/orm/base.py +973 -0
- sqlalchemy/orm/bulk_persistence.py +2123 -0
- sqlalchemy/orm/clsregistry.py +571 -0
- sqlalchemy/orm/collections.py +1620 -0
- sqlalchemy/orm/context.py +3268 -0
- sqlalchemy/orm/decl_api.py +1883 -0
- sqlalchemy/orm/decl_base.py +2190 -0
- sqlalchemy/orm/dependency.py +1304 -0
- sqlalchemy/orm/descriptor_props.py +1076 -0
- sqlalchemy/orm/dynamic.py +300 -0
- sqlalchemy/orm/evaluator.py +379 -0
- sqlalchemy/orm/events.py +3261 -0
- sqlalchemy/orm/exc.py +228 -0
- sqlalchemy/orm/identity.py +302 -0
- sqlalchemy/orm/instrumentation.py +754 -0
- sqlalchemy/orm/interfaces.py +1474 -0
- sqlalchemy/orm/loading.py +1682 -0
- sqlalchemy/orm/mapped_collection.py +557 -0
- sqlalchemy/orm/mapper.py +4432 -0
- sqlalchemy/orm/path_registry.py +811 -0
- sqlalchemy/orm/persistence.py +1782 -0
- sqlalchemy/orm/properties.py +886 -0
- sqlalchemy/orm/query.py +3396 -0
- sqlalchemy/orm/relationships.py +3500 -0
- sqlalchemy/orm/scoping.py +2165 -0
- sqlalchemy/orm/session.py +5301 -0
- sqlalchemy/orm/state.py +1143 -0
- sqlalchemy/orm/state_changes.py +198 -0
- sqlalchemy/orm/strategies.py +3473 -0
- sqlalchemy/orm/strategy_options.py +2569 -0
- sqlalchemy/orm/sync.py +164 -0
- sqlalchemy/orm/unitofwork.py +796 -0
- sqlalchemy/orm/util.py +2424 -0
- sqlalchemy/orm/writeonly.py +678 -0
- sqlalchemy/pool/__init__.py +44 -0
- sqlalchemy/pool/base.py +1515 -0
- sqlalchemy/pool/events.py +370 -0
- sqlalchemy/pool/impl.py +581 -0
- sqlalchemy/py.typed +0 -0
- sqlalchemy/schema.py +70 -0
- sqlalchemy/sql/__init__.py +145 -0
- sqlalchemy/sql/_dml_constructors.py +140 -0
- sqlalchemy/sql/_elements_constructors.py +1850 -0
- sqlalchemy/sql/_orm_types.py +20 -0
- sqlalchemy/sql/_py_util.py +75 -0
- sqlalchemy/sql/_selectable_constructors.py +635 -0
- sqlalchemy/sql/_typing.py +460 -0
- sqlalchemy/sql/annotation.py +585 -0
- sqlalchemy/sql/base.py +2185 -0
- sqlalchemy/sql/cache_key.py +1057 -0
- sqlalchemy/sql/coercions.py +1405 -0
- sqlalchemy/sql/compiler.py +7818 -0
- sqlalchemy/sql/crud.py +1669 -0
- sqlalchemy/sql/ddl.py +1378 -0
- sqlalchemy/sql/default_comparator.py +552 -0
- sqlalchemy/sql/dml.py +1817 -0
- sqlalchemy/sql/elements.py +5499 -0
- sqlalchemy/sql/events.py +455 -0
- sqlalchemy/sql/expression.py +162 -0
- sqlalchemy/sql/functions.py +2055 -0
- sqlalchemy/sql/lambdas.py +1449 -0
- sqlalchemy/sql/naming.py +212 -0
- sqlalchemy/sql/operators.py +2579 -0
- sqlalchemy/sql/roles.py +323 -0
- sqlalchemy/sql/schema.py +6158 -0
- sqlalchemy/sql/selectable.py +7004 -0
- sqlalchemy/sql/sqltypes.py +3827 -0
- sqlalchemy/sql/traversals.py +1024 -0
- sqlalchemy/sql/type_api.py +2339 -0
- sqlalchemy/sql/util.py +1486 -0
- sqlalchemy/sql/visitors.py +1165 -0
- sqlalchemy/testing/__init__.py +96 -0
- sqlalchemy/testing/assertions.py +989 -0
- sqlalchemy/testing/assertsql.py +516 -0
- sqlalchemy/testing/asyncio.py +135 -0
- sqlalchemy/testing/config.py +427 -0
- sqlalchemy/testing/engines.py +472 -0
- sqlalchemy/testing/entities.py +117 -0
- sqlalchemy/testing/exclusions.py +435 -0
- sqlalchemy/testing/fixtures/__init__.py +28 -0
- sqlalchemy/testing/fixtures/base.py +366 -0
- sqlalchemy/testing/fixtures/mypy.py +312 -0
- sqlalchemy/testing/fixtures/orm.py +227 -0
- sqlalchemy/testing/fixtures/sql.py +503 -0
- sqlalchemy/testing/pickleable.py +155 -0
- sqlalchemy/testing/plugin/__init__.py +6 -0
- sqlalchemy/testing/plugin/bootstrap.py +51 -0
- sqlalchemy/testing/plugin/plugin_base.py +779 -0
- sqlalchemy/testing/plugin/pytestplugin.py +868 -0
- sqlalchemy/testing/profiling.py +324 -0
- sqlalchemy/testing/provision.py +496 -0
- sqlalchemy/testing/requirements.py +1818 -0
- sqlalchemy/testing/schema.py +224 -0
- sqlalchemy/testing/suite/__init__.py +19 -0
- sqlalchemy/testing/suite/test_cte.py +211 -0
- sqlalchemy/testing/suite/test_ddl.py +389 -0
- sqlalchemy/testing/suite/test_deprecations.py +153 -0
- sqlalchemy/testing/suite/test_dialect.py +740 -0
- sqlalchemy/testing/suite/test_insert.py +630 -0
- sqlalchemy/testing/suite/test_reflection.py +3225 -0
- sqlalchemy/testing/suite/test_results.py +502 -0
- sqlalchemy/testing/suite/test_rowcount.py +258 -0
- sqlalchemy/testing/suite/test_select.py +1999 -0
- sqlalchemy/testing/suite/test_sequence.py +317 -0
- sqlalchemy/testing/suite/test_types.py +2141 -0
- sqlalchemy/testing/suite/test_unicode_ddl.py +189 -0
- sqlalchemy/testing/suite/test_update_delete.py +139 -0
- sqlalchemy/testing/util.py +537 -0
- sqlalchemy/testing/warnings.py +52 -0
- sqlalchemy/types.py +76 -0
- sqlalchemy/util/__init__.py +160 -0
- sqlalchemy/util/_collections.py +715 -0
- sqlalchemy/util/_concurrency_py3k.py +288 -0
- sqlalchemy/util/_has_cy.py +40 -0
- sqlalchemy/util/_py_collections.py +541 -0
- sqlalchemy/util/compat.py +301 -0
- sqlalchemy/util/concurrency.py +108 -0
- sqlalchemy/util/deprecations.py +401 -0
- sqlalchemy/util/langhelpers.py +2218 -0
- sqlalchemy/util/preloaded.py +150 -0
- sqlalchemy/util/queue.py +322 -0
- sqlalchemy/util/tool_support.py +201 -0
- sqlalchemy/util/topological.py +120 -0
- sqlalchemy/util/typing.py +629 -0
|
@@ -0,0 +1,629 @@
|
|
|
1
|
+
# util/typing.py
|
|
2
|
+
# Copyright (C) 2022-2024 the SQLAlchemy authors and contributors
|
|
3
|
+
# <see AUTHORS file>
|
|
4
|
+
#
|
|
5
|
+
# This module is part of SQLAlchemy and is released under
|
|
6
|
+
# the MIT License: https://www.opensource.org/licenses/mit-license.php
|
|
7
|
+
# mypy: allow-untyped-defs, allow-untyped-calls
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import builtins
|
|
12
|
+
import collections.abc as collections_abc
|
|
13
|
+
import re
|
|
14
|
+
import sys
|
|
15
|
+
import typing
|
|
16
|
+
from typing import Any
|
|
17
|
+
from typing import Callable
|
|
18
|
+
from typing import cast
|
|
19
|
+
from typing import Dict
|
|
20
|
+
from typing import ForwardRef
|
|
21
|
+
from typing import Generic
|
|
22
|
+
from typing import Iterable
|
|
23
|
+
from typing import Mapping
|
|
24
|
+
from typing import NewType
|
|
25
|
+
from typing import NoReturn
|
|
26
|
+
from typing import Optional
|
|
27
|
+
from typing import overload
|
|
28
|
+
from typing import Set
|
|
29
|
+
from typing import Tuple
|
|
30
|
+
from typing import Type
|
|
31
|
+
from typing import TYPE_CHECKING
|
|
32
|
+
from typing import TypeVar
|
|
33
|
+
from typing import Union
|
|
34
|
+
|
|
35
|
+
from . import compat
|
|
36
|
+
|
|
37
|
+
if True: # zimports removes the tailing comments
|
|
38
|
+
from typing_extensions import Annotated as Annotated # 3.8
|
|
39
|
+
from typing_extensions import Concatenate as Concatenate # 3.10
|
|
40
|
+
from typing_extensions import (
|
|
41
|
+
dataclass_transform as dataclass_transform, # 3.11,
|
|
42
|
+
)
|
|
43
|
+
from typing_extensions import Final as Final # 3.8
|
|
44
|
+
from typing_extensions import final as final # 3.8
|
|
45
|
+
from typing_extensions import get_args as get_args # 3.10
|
|
46
|
+
from typing_extensions import get_origin as get_origin # 3.10
|
|
47
|
+
from typing_extensions import Literal as Literal # 3.8
|
|
48
|
+
from typing_extensions import NotRequired as NotRequired # 3.11
|
|
49
|
+
from typing_extensions import ParamSpec as ParamSpec # 3.10
|
|
50
|
+
from typing_extensions import Protocol as Protocol # 3.8
|
|
51
|
+
from typing_extensions import SupportsIndex as SupportsIndex # 3.8
|
|
52
|
+
from typing_extensions import TypeAlias as TypeAlias # 3.10
|
|
53
|
+
from typing_extensions import TypedDict as TypedDict # 3.8
|
|
54
|
+
from typing_extensions import TypeGuard as TypeGuard # 3.10
|
|
55
|
+
from typing_extensions import Self as Self # 3.11
|
|
56
|
+
from typing_extensions import TypeAliasType as TypeAliasType # 3.12
|
|
57
|
+
|
|
58
|
+
_T = TypeVar("_T", bound=Any)
|
|
59
|
+
_KT = TypeVar("_KT")
|
|
60
|
+
_KT_co = TypeVar("_KT_co", covariant=True)
|
|
61
|
+
_KT_contra = TypeVar("_KT_contra", contravariant=True)
|
|
62
|
+
_VT = TypeVar("_VT")
|
|
63
|
+
_VT_co = TypeVar("_VT_co", covariant=True)
|
|
64
|
+
|
|
65
|
+
if compat.py38:
|
|
66
|
+
# typing_extensions.Literal is different from typing.Literal until
|
|
67
|
+
# Python 3.10.1
|
|
68
|
+
_LITERAL_TYPES = frozenset([typing.Literal, Literal])
|
|
69
|
+
else:
|
|
70
|
+
_LITERAL_TYPES = frozenset([Literal])
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
if compat.py310:
|
|
74
|
+
# why they took until py310 to put this in stdlib is beyond me,
|
|
75
|
+
# I've been wanting it since py27
|
|
76
|
+
from types import NoneType as NoneType
|
|
77
|
+
else:
|
|
78
|
+
NoneType = type(None) # type: ignore
|
|
79
|
+
|
|
80
|
+
NoneFwd = ForwardRef("None")
|
|
81
|
+
|
|
82
|
+
typing_get_args = get_args
|
|
83
|
+
typing_get_origin = get_origin
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
_AnnotationScanType = Union[
|
|
87
|
+
Type[Any], str, ForwardRef, NewType, TypeAliasType, "GenericProtocol[Any]"
|
|
88
|
+
]
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
class ArgsTypeProcotol(Protocol):
|
|
92
|
+
"""protocol for types that have ``__args__``
|
|
93
|
+
|
|
94
|
+
there's no public interface for this AFAIK
|
|
95
|
+
|
|
96
|
+
"""
|
|
97
|
+
|
|
98
|
+
__args__: Tuple[_AnnotationScanType, ...]
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
class GenericProtocol(Protocol[_T]):
|
|
102
|
+
"""protocol for generic types.
|
|
103
|
+
|
|
104
|
+
this since Python.typing _GenericAlias is private
|
|
105
|
+
|
|
106
|
+
"""
|
|
107
|
+
|
|
108
|
+
__args__: Tuple[_AnnotationScanType, ...]
|
|
109
|
+
__origin__: Type[_T]
|
|
110
|
+
|
|
111
|
+
# Python's builtin _GenericAlias has this method, however builtins like
|
|
112
|
+
# list, dict, etc. do not, even though they have ``__origin__`` and
|
|
113
|
+
# ``__args__``
|
|
114
|
+
#
|
|
115
|
+
# def copy_with(self, params: Tuple[_AnnotationScanType, ...]) -> Type[_T]:
|
|
116
|
+
# ...
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
# copied from TypeShed, required in order to implement
|
|
120
|
+
# MutableMapping.update()
|
|
121
|
+
class SupportsKeysAndGetItem(Protocol[_KT, _VT_co]):
|
|
122
|
+
def keys(self) -> Iterable[_KT]: ...
|
|
123
|
+
|
|
124
|
+
def __getitem__(self, __k: _KT) -> _VT_co: ...
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
# work around https://github.com/microsoft/pyright/issues/3025
|
|
128
|
+
_LiteralStar = Literal["*"]
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def de_stringify_annotation(
|
|
132
|
+
cls: Type[Any],
|
|
133
|
+
annotation: _AnnotationScanType,
|
|
134
|
+
originating_module: str,
|
|
135
|
+
locals_: Mapping[str, Any],
|
|
136
|
+
*,
|
|
137
|
+
str_cleanup_fn: Optional[Callable[[str, str], str]] = None,
|
|
138
|
+
include_generic: bool = False,
|
|
139
|
+
_already_seen: Optional[Set[Any]] = None,
|
|
140
|
+
) -> Type[Any]:
|
|
141
|
+
"""Resolve annotations that may be string based into real objects.
|
|
142
|
+
|
|
143
|
+
This is particularly important if a module defines "from __future__ import
|
|
144
|
+
annotations", as everything inside of __annotations__ is a string. We want
|
|
145
|
+
to at least have generic containers like ``Mapped``, ``Union``, ``List``,
|
|
146
|
+
etc.
|
|
147
|
+
|
|
148
|
+
"""
|
|
149
|
+
# looked at typing.get_type_hints(), looked at pydantic. We need much
|
|
150
|
+
# less here, and we here try to not use any private typing internals
|
|
151
|
+
# or construct ForwardRef objects which is documented as something
|
|
152
|
+
# that should be avoided.
|
|
153
|
+
|
|
154
|
+
original_annotation = annotation
|
|
155
|
+
|
|
156
|
+
if is_fwd_ref(annotation):
|
|
157
|
+
annotation = annotation.__forward_arg__
|
|
158
|
+
|
|
159
|
+
if isinstance(annotation, str):
|
|
160
|
+
if str_cleanup_fn:
|
|
161
|
+
annotation = str_cleanup_fn(annotation, originating_module)
|
|
162
|
+
|
|
163
|
+
annotation = eval_expression(
|
|
164
|
+
annotation, originating_module, locals_=locals_, in_class=cls
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
if (
|
|
168
|
+
include_generic
|
|
169
|
+
and is_generic(annotation)
|
|
170
|
+
and not is_literal(annotation)
|
|
171
|
+
):
|
|
172
|
+
if _already_seen is None:
|
|
173
|
+
_already_seen = set()
|
|
174
|
+
|
|
175
|
+
if annotation in _already_seen:
|
|
176
|
+
# only occurs recursively. outermost return type
|
|
177
|
+
# will always be Type.
|
|
178
|
+
# the element here will be either ForwardRef or
|
|
179
|
+
# Optional[ForwardRef]
|
|
180
|
+
return original_annotation # type: ignore
|
|
181
|
+
else:
|
|
182
|
+
_already_seen.add(annotation)
|
|
183
|
+
|
|
184
|
+
elements = tuple(
|
|
185
|
+
de_stringify_annotation(
|
|
186
|
+
cls,
|
|
187
|
+
elem,
|
|
188
|
+
originating_module,
|
|
189
|
+
locals_,
|
|
190
|
+
str_cleanup_fn=str_cleanup_fn,
|
|
191
|
+
include_generic=include_generic,
|
|
192
|
+
_already_seen=_already_seen,
|
|
193
|
+
)
|
|
194
|
+
for elem in annotation.__args__
|
|
195
|
+
)
|
|
196
|
+
|
|
197
|
+
return _copy_generic_annotation_with(annotation, elements)
|
|
198
|
+
|
|
199
|
+
return annotation # type: ignore
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def fixup_container_fwd_refs(
|
|
203
|
+
type_: _AnnotationScanType,
|
|
204
|
+
) -> _AnnotationScanType:
|
|
205
|
+
"""Correct dict['x', 'y'] into dict[ForwardRef('x'), ForwardRef('y')]
|
|
206
|
+
and similar for list, set
|
|
207
|
+
|
|
208
|
+
"""
|
|
209
|
+
|
|
210
|
+
if (
|
|
211
|
+
is_generic(type_)
|
|
212
|
+
and typing_get_origin(type_)
|
|
213
|
+
in (
|
|
214
|
+
dict,
|
|
215
|
+
set,
|
|
216
|
+
list,
|
|
217
|
+
collections_abc.MutableSet,
|
|
218
|
+
collections_abc.MutableMapping,
|
|
219
|
+
collections_abc.MutableSequence,
|
|
220
|
+
collections_abc.Mapping,
|
|
221
|
+
collections_abc.Sequence,
|
|
222
|
+
)
|
|
223
|
+
# fight, kick and scream to struggle to tell the difference between
|
|
224
|
+
# dict[] and typing.Dict[] which DO NOT compare the same and DO NOT
|
|
225
|
+
# behave the same yet there is NO WAY to distinguish between which type
|
|
226
|
+
# it is using public attributes
|
|
227
|
+
and not re.match(
|
|
228
|
+
"typing.(?:Dict|List|Set|.*Mapping|.*Sequence|.*Set)", repr(type_)
|
|
229
|
+
)
|
|
230
|
+
):
|
|
231
|
+
# compat with py3.10 and earlier
|
|
232
|
+
return typing_get_origin(type_).__class_getitem__( # type: ignore
|
|
233
|
+
tuple(
|
|
234
|
+
[
|
|
235
|
+
ForwardRef(elem) if isinstance(elem, str) else elem
|
|
236
|
+
for elem in typing_get_args(type_)
|
|
237
|
+
]
|
|
238
|
+
)
|
|
239
|
+
)
|
|
240
|
+
return type_
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
def _copy_generic_annotation_with(
|
|
244
|
+
annotation: GenericProtocol[_T], elements: Tuple[_AnnotationScanType, ...]
|
|
245
|
+
) -> Type[_T]:
|
|
246
|
+
if hasattr(annotation, "copy_with"):
|
|
247
|
+
# List, Dict, etc. real generics
|
|
248
|
+
return annotation.copy_with(elements) # type: ignore
|
|
249
|
+
else:
|
|
250
|
+
# Python builtins list, dict, etc.
|
|
251
|
+
return annotation.__origin__[elements] # type: ignore
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
def eval_expression(
|
|
255
|
+
expression: str,
|
|
256
|
+
module_name: str,
|
|
257
|
+
*,
|
|
258
|
+
locals_: Optional[Mapping[str, Any]] = None,
|
|
259
|
+
in_class: Optional[Type[Any]] = None,
|
|
260
|
+
) -> Any:
|
|
261
|
+
try:
|
|
262
|
+
base_globals: Dict[str, Any] = sys.modules[module_name].__dict__
|
|
263
|
+
except KeyError as ke:
|
|
264
|
+
raise NameError(
|
|
265
|
+
f"Module {module_name} isn't present in sys.modules; can't "
|
|
266
|
+
f"evaluate expression {expression}"
|
|
267
|
+
) from ke
|
|
268
|
+
|
|
269
|
+
try:
|
|
270
|
+
if in_class is not None:
|
|
271
|
+
cls_namespace = dict(in_class.__dict__)
|
|
272
|
+
cls_namespace.setdefault(in_class.__name__, in_class)
|
|
273
|
+
|
|
274
|
+
# see #10899. We want the locals/globals to take precedence
|
|
275
|
+
# over the class namespace in this context, even though this
|
|
276
|
+
# is not the usual way variables would resolve.
|
|
277
|
+
cls_namespace.update(base_globals)
|
|
278
|
+
|
|
279
|
+
annotation = eval(expression, cls_namespace, locals_)
|
|
280
|
+
else:
|
|
281
|
+
annotation = eval(expression, base_globals, locals_)
|
|
282
|
+
except Exception as err:
|
|
283
|
+
raise NameError(
|
|
284
|
+
f"Could not de-stringify annotation {expression!r}"
|
|
285
|
+
) from err
|
|
286
|
+
else:
|
|
287
|
+
return annotation
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
def eval_name_only(
|
|
291
|
+
name: str,
|
|
292
|
+
module_name: str,
|
|
293
|
+
*,
|
|
294
|
+
locals_: Optional[Mapping[str, Any]] = None,
|
|
295
|
+
) -> Any:
|
|
296
|
+
if "." in name:
|
|
297
|
+
return eval_expression(name, module_name, locals_=locals_)
|
|
298
|
+
|
|
299
|
+
try:
|
|
300
|
+
base_globals: Dict[str, Any] = sys.modules[module_name].__dict__
|
|
301
|
+
except KeyError as ke:
|
|
302
|
+
raise NameError(
|
|
303
|
+
f"Module {module_name} isn't present in sys.modules; can't "
|
|
304
|
+
f"resolve name {name}"
|
|
305
|
+
) from ke
|
|
306
|
+
|
|
307
|
+
# name only, just look in globals. eval() works perfectly fine here,
|
|
308
|
+
# however we are seeking to have this be faster, as this occurs for
|
|
309
|
+
# every Mapper[] keyword, etc. depending on configuration
|
|
310
|
+
try:
|
|
311
|
+
return base_globals[name]
|
|
312
|
+
except KeyError as ke:
|
|
313
|
+
# check in builtins as well to handle `list`, `set` or `dict`, etc.
|
|
314
|
+
try:
|
|
315
|
+
return builtins.__dict__[name]
|
|
316
|
+
except KeyError:
|
|
317
|
+
pass
|
|
318
|
+
|
|
319
|
+
raise NameError(
|
|
320
|
+
f"Could not locate name {name} in module {module_name}"
|
|
321
|
+
) from ke
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
def resolve_name_to_real_class_name(name: str, module_name: str) -> str:
|
|
325
|
+
try:
|
|
326
|
+
obj = eval_name_only(name, module_name)
|
|
327
|
+
except NameError:
|
|
328
|
+
return name
|
|
329
|
+
else:
|
|
330
|
+
return getattr(obj, "__name__", name)
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
def de_stringify_union_elements(
|
|
334
|
+
cls: Type[Any],
|
|
335
|
+
annotation: ArgsTypeProcotol,
|
|
336
|
+
originating_module: str,
|
|
337
|
+
locals_: Mapping[str, Any],
|
|
338
|
+
*,
|
|
339
|
+
str_cleanup_fn: Optional[Callable[[str, str], str]] = None,
|
|
340
|
+
) -> Type[Any]:
|
|
341
|
+
return make_union_type(
|
|
342
|
+
*[
|
|
343
|
+
de_stringify_annotation(
|
|
344
|
+
cls,
|
|
345
|
+
anno,
|
|
346
|
+
originating_module,
|
|
347
|
+
{},
|
|
348
|
+
str_cleanup_fn=str_cleanup_fn,
|
|
349
|
+
)
|
|
350
|
+
for anno in annotation.__args__
|
|
351
|
+
]
|
|
352
|
+
)
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
def is_pep593(type_: Optional[_AnnotationScanType]) -> bool:
|
|
356
|
+
return type_ is not None and typing_get_origin(type_) is Annotated
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
def is_non_string_iterable(obj: Any) -> TypeGuard[Iterable[Any]]:
|
|
360
|
+
return isinstance(obj, collections_abc.Iterable) and not isinstance(
|
|
361
|
+
obj, (str, bytes)
|
|
362
|
+
)
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
def is_literal(type_: _AnnotationScanType) -> bool:
|
|
366
|
+
return get_origin(type_) in _LITERAL_TYPES
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
def is_newtype(type_: Optional[_AnnotationScanType]) -> TypeGuard[NewType]:
|
|
370
|
+
return hasattr(type_, "__supertype__")
|
|
371
|
+
|
|
372
|
+
# doesn't work in 3.8, 3.7 as it passes a closure, not an
|
|
373
|
+
# object instance
|
|
374
|
+
# return isinstance(type_, NewType)
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
def is_generic(type_: _AnnotationScanType) -> TypeGuard[GenericProtocol[Any]]:
|
|
378
|
+
return hasattr(type_, "__args__") and hasattr(type_, "__origin__")
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
def is_pep695(type_: _AnnotationScanType) -> TypeGuard[TypeAliasType]:
|
|
382
|
+
return isinstance(type_, TypeAliasType)
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
def flatten_newtype(type_: NewType) -> Type[Any]:
|
|
386
|
+
super_type = type_.__supertype__
|
|
387
|
+
while is_newtype(super_type):
|
|
388
|
+
super_type = super_type.__supertype__
|
|
389
|
+
return super_type # type: ignore[return-value]
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
def is_fwd_ref(
|
|
393
|
+
type_: _AnnotationScanType, check_generic: bool = False
|
|
394
|
+
) -> TypeGuard[ForwardRef]:
|
|
395
|
+
if isinstance(type_, ForwardRef):
|
|
396
|
+
return True
|
|
397
|
+
elif check_generic and is_generic(type_):
|
|
398
|
+
return any(is_fwd_ref(arg, True) for arg in type_.__args__)
|
|
399
|
+
else:
|
|
400
|
+
return False
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
@overload
|
|
404
|
+
def de_optionalize_union_types(type_: str) -> str: ...
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
@overload
|
|
408
|
+
def de_optionalize_union_types(type_: Type[Any]) -> Type[Any]: ...
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
@overload
|
|
412
|
+
def de_optionalize_union_types(
|
|
413
|
+
type_: _AnnotationScanType,
|
|
414
|
+
) -> _AnnotationScanType: ...
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
def de_optionalize_union_types(
|
|
418
|
+
type_: _AnnotationScanType,
|
|
419
|
+
) -> _AnnotationScanType:
|
|
420
|
+
"""Given a type, filter out ``Union`` types that include ``NoneType``
|
|
421
|
+
to not include the ``NoneType``.
|
|
422
|
+
|
|
423
|
+
"""
|
|
424
|
+
|
|
425
|
+
if is_fwd_ref(type_):
|
|
426
|
+
return de_optionalize_fwd_ref_union_types(type_)
|
|
427
|
+
|
|
428
|
+
elif is_optional(type_):
|
|
429
|
+
typ = set(type_.__args__)
|
|
430
|
+
|
|
431
|
+
typ.discard(NoneType)
|
|
432
|
+
typ.discard(NoneFwd)
|
|
433
|
+
|
|
434
|
+
return make_union_type(*typ)
|
|
435
|
+
|
|
436
|
+
else:
|
|
437
|
+
return type_
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
def de_optionalize_fwd_ref_union_types(
|
|
441
|
+
type_: ForwardRef,
|
|
442
|
+
) -> _AnnotationScanType:
|
|
443
|
+
"""return the non-optional type for Optional[], Union[None, ...], x|None,
|
|
444
|
+
etc. without de-stringifying forward refs.
|
|
445
|
+
|
|
446
|
+
unfortunately this seems to require lots of hardcoded heuristics
|
|
447
|
+
|
|
448
|
+
"""
|
|
449
|
+
|
|
450
|
+
annotation = type_.__forward_arg__
|
|
451
|
+
|
|
452
|
+
mm = re.match(r"^(.+?)\[(.+)\]$", annotation)
|
|
453
|
+
if mm:
|
|
454
|
+
if mm.group(1) == "Optional":
|
|
455
|
+
return ForwardRef(mm.group(2))
|
|
456
|
+
elif mm.group(1) == "Union":
|
|
457
|
+
elements = re.split(r",\s*", mm.group(2))
|
|
458
|
+
return make_union_type(
|
|
459
|
+
*[ForwardRef(elem) for elem in elements if elem != "None"]
|
|
460
|
+
)
|
|
461
|
+
else:
|
|
462
|
+
return type_
|
|
463
|
+
|
|
464
|
+
pipe_tokens = re.split(r"\s*\|\s*", annotation)
|
|
465
|
+
if "None" in pipe_tokens:
|
|
466
|
+
return ForwardRef("|".join(p for p in pipe_tokens if p != "None"))
|
|
467
|
+
|
|
468
|
+
return type_
|
|
469
|
+
|
|
470
|
+
|
|
471
|
+
def make_union_type(*types: _AnnotationScanType) -> Type[Any]:
|
|
472
|
+
"""Make a Union type.
|
|
473
|
+
|
|
474
|
+
This is needed by :func:`.de_optionalize_union_types` which removes
|
|
475
|
+
``NoneType`` from a ``Union``.
|
|
476
|
+
|
|
477
|
+
"""
|
|
478
|
+
return cast(Any, Union).__getitem__(types) # type: ignore
|
|
479
|
+
|
|
480
|
+
|
|
481
|
+
def expand_unions(
|
|
482
|
+
type_: Type[Any], include_union: bool = False, discard_none: bool = False
|
|
483
|
+
) -> Tuple[Type[Any], ...]:
|
|
484
|
+
"""Return a type as a tuple of individual types, expanding for
|
|
485
|
+
``Union`` types."""
|
|
486
|
+
|
|
487
|
+
if is_union(type_):
|
|
488
|
+
typ = set(type_.__args__)
|
|
489
|
+
|
|
490
|
+
if discard_none:
|
|
491
|
+
typ.discard(NoneType)
|
|
492
|
+
|
|
493
|
+
if include_union:
|
|
494
|
+
return (type_,) + tuple(typ) # type: ignore
|
|
495
|
+
else:
|
|
496
|
+
return tuple(typ) # type: ignore
|
|
497
|
+
else:
|
|
498
|
+
return (type_,)
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
def is_optional(type_: Any) -> TypeGuard[ArgsTypeProcotol]:
|
|
502
|
+
return is_origin_of(
|
|
503
|
+
type_,
|
|
504
|
+
"Optional",
|
|
505
|
+
"Union",
|
|
506
|
+
"UnionType",
|
|
507
|
+
)
|
|
508
|
+
|
|
509
|
+
|
|
510
|
+
def is_optional_union(type_: Any) -> bool:
|
|
511
|
+
return is_optional(type_) and NoneType in typing_get_args(type_)
|
|
512
|
+
|
|
513
|
+
|
|
514
|
+
def is_union(type_: Any) -> TypeGuard[ArgsTypeProcotol]:
|
|
515
|
+
return is_origin_of(type_, "Union")
|
|
516
|
+
|
|
517
|
+
|
|
518
|
+
def is_origin_of_cls(
|
|
519
|
+
type_: Any, class_obj: Union[Tuple[Type[Any], ...], Type[Any]]
|
|
520
|
+
) -> bool:
|
|
521
|
+
"""return True if the given type has an __origin__ that shares a base
|
|
522
|
+
with the given class"""
|
|
523
|
+
|
|
524
|
+
origin = typing_get_origin(type_)
|
|
525
|
+
if origin is None:
|
|
526
|
+
return False
|
|
527
|
+
|
|
528
|
+
return isinstance(origin, type) and issubclass(origin, class_obj)
|
|
529
|
+
|
|
530
|
+
|
|
531
|
+
def is_origin_of(
|
|
532
|
+
type_: Any, *names: str, module: Optional[str] = None
|
|
533
|
+
) -> bool:
|
|
534
|
+
"""return True if the given type has an __origin__ with the given name
|
|
535
|
+
and optional module."""
|
|
536
|
+
|
|
537
|
+
origin = typing_get_origin(type_)
|
|
538
|
+
if origin is None:
|
|
539
|
+
return False
|
|
540
|
+
|
|
541
|
+
return _get_type_name(origin) in names and (
|
|
542
|
+
module is None or origin.__module__.startswith(module)
|
|
543
|
+
)
|
|
544
|
+
|
|
545
|
+
|
|
546
|
+
def _get_type_name(type_: Type[Any]) -> str:
|
|
547
|
+
if compat.py310:
|
|
548
|
+
return type_.__name__
|
|
549
|
+
else:
|
|
550
|
+
typ_name = getattr(type_, "__name__", None)
|
|
551
|
+
if typ_name is None:
|
|
552
|
+
typ_name = getattr(type_, "_name", None)
|
|
553
|
+
|
|
554
|
+
return typ_name # type: ignore
|
|
555
|
+
|
|
556
|
+
|
|
557
|
+
class DescriptorProto(Protocol):
|
|
558
|
+
def __get__(self, instance: object, owner: Any) -> Any: ...
|
|
559
|
+
|
|
560
|
+
def __set__(self, instance: Any, value: Any) -> None: ...
|
|
561
|
+
|
|
562
|
+
def __delete__(self, instance: Any) -> None: ...
|
|
563
|
+
|
|
564
|
+
|
|
565
|
+
_DESC = TypeVar("_DESC", bound=DescriptorProto)
|
|
566
|
+
|
|
567
|
+
|
|
568
|
+
class DescriptorReference(Generic[_DESC]):
|
|
569
|
+
"""a descriptor that refers to a descriptor.
|
|
570
|
+
|
|
571
|
+
used for cases where we need to have an instance variable referring to an
|
|
572
|
+
object that is itself a descriptor, which typically confuses typing tools
|
|
573
|
+
as they don't know when they should use ``__get__`` or not when referring
|
|
574
|
+
to the descriptor assignment as an instance variable. See
|
|
575
|
+
sqlalchemy.orm.interfaces.PropComparator.prop
|
|
576
|
+
|
|
577
|
+
"""
|
|
578
|
+
|
|
579
|
+
if TYPE_CHECKING:
|
|
580
|
+
|
|
581
|
+
def __get__(self, instance: object, owner: Any) -> _DESC: ...
|
|
582
|
+
|
|
583
|
+
def __set__(self, instance: Any, value: _DESC) -> None: ...
|
|
584
|
+
|
|
585
|
+
def __delete__(self, instance: Any) -> None: ...
|
|
586
|
+
|
|
587
|
+
|
|
588
|
+
_DESC_co = TypeVar("_DESC_co", bound=DescriptorProto, covariant=True)
|
|
589
|
+
|
|
590
|
+
|
|
591
|
+
class RODescriptorReference(Generic[_DESC_co]):
|
|
592
|
+
"""a descriptor that refers to a descriptor.
|
|
593
|
+
|
|
594
|
+
same as :class:`.DescriptorReference` but is read-only, so that subclasses
|
|
595
|
+
can define a subtype as the generically contained element
|
|
596
|
+
|
|
597
|
+
"""
|
|
598
|
+
|
|
599
|
+
if TYPE_CHECKING:
|
|
600
|
+
|
|
601
|
+
def __get__(self, instance: object, owner: Any) -> _DESC_co: ...
|
|
602
|
+
|
|
603
|
+
def __set__(self, instance: Any, value: Any) -> NoReturn: ...
|
|
604
|
+
|
|
605
|
+
def __delete__(self, instance: Any) -> NoReturn: ...
|
|
606
|
+
|
|
607
|
+
|
|
608
|
+
_FN = TypeVar("_FN", bound=Optional[Callable[..., Any]])
|
|
609
|
+
|
|
610
|
+
|
|
611
|
+
class CallableReference(Generic[_FN]):
|
|
612
|
+
"""a descriptor that refers to a callable.
|
|
613
|
+
|
|
614
|
+
works around mypy's limitation of not allowing callables assigned
|
|
615
|
+
as instance variables
|
|
616
|
+
|
|
617
|
+
|
|
618
|
+
"""
|
|
619
|
+
|
|
620
|
+
if TYPE_CHECKING:
|
|
621
|
+
|
|
622
|
+
def __get__(self, instance: object, owner: Any) -> _FN: ...
|
|
623
|
+
|
|
624
|
+
def __set__(self, instance: Any, value: _FN) -> None: ...
|
|
625
|
+
|
|
626
|
+
def __delete__(self, instance: Any) -> None: ...
|
|
627
|
+
|
|
628
|
+
|
|
629
|
+
# $def ro_descriptor_reference(fn: Callable[])
|