summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/util/typing.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2022-06-14 17:05:44 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2022-06-15 09:04:51 -0400
commit514f2a8b4c6de8c033496543e9aaf2a0a4eb599d (patch)
treea18461c3168a9e49803b41a0492d33fc000aef15 /lib/sqlalchemy/util/typing.py
parentaa8ddf7d356e7ea8c100cca18bb49e3bd820ca31 (diff)
downloadsqlalchemy-514f2a8b4c6de8c033496543e9aaf2a0a4eb599d.tar.gz
new features for pep 593 Annotated
* extract the inner type from Annotated when the outer type isn't present in the type map, to allow for arbitrary Annotated * allow _IntrospectsAnnotations objects to be directly present in an Annotated and resolve the mapper property from that. Currently implemented for mapped_column(), with message for others. Can work for composite() and likely some relationship() as well at some point References: https://twitter.com/zzzeek/status/1536693554621341697 and replies Change-Id: I04657050a8785f194bf8f63291faf3475af88781
Diffstat (limited to 'lib/sqlalchemy/util/typing.py')
-rw-r--r--lib/sqlalchemy/util/typing.py16
1 files changed, 16 insertions, 0 deletions
diff --git a/lib/sqlalchemy/util/typing.py b/lib/sqlalchemy/util/typing.py
index 454de100b..eb625e06e 100644
--- a/lib/sqlalchemy/util/typing.py
+++ b/lib/sqlalchemy/util/typing.py
@@ -61,9 +61,18 @@ else:
if typing.TYPE_CHECKING or compat.py310:
from typing import Annotated as Annotated
+ from typing import get_args as get_args
+ from typing import get_origin as get_origin
else:
from typing_extensions import Annotated as Annotated # noqa: F401
+ # these are in py38 but don't work with Annotated correctly, so
+ # for 3.8 / 3.9 we use the typing extensions version
+ from typing_extensions import get_args as get_args # noqa: F401
+ from typing_extensions import (
+ get_origin as get_origin, # noqa: F401,
+ )
+
if typing.TYPE_CHECKING or compat.py38:
from typing import Literal as Literal
from typing import Protocol as Protocol
@@ -75,6 +84,9 @@ else:
from typing_extensions import TypedDict as TypedDict # noqa: F401
from typing_extensions import Final as Final # noqa: F401
+typing_get_args = get_args
+typing_get_origin = get_origin
+
# copied from TypeShed, required in order to implement
# MutableMapping.update()
@@ -140,6 +152,10 @@ def de_stringify_annotation(
return annotation # type: ignore
+def is_pep593(type_: Optional[_AnnotationScanType]) -> bool:
+ return type_ is not None and typing_get_origin(type_) is Annotated
+
+
def is_fwd_ref(type_: _AnnotationScanType) -> bool:
return isinstance(type_, ForwardRef)