summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/orm
diff options
context:
space:
mode:
authormike bayer <mike_mp@zzzcomputing.com>2023-02-01 15:55:16 +0000
committerGerrit Code Review <gerrit@ci3.zzzcomputing.com>2023-02-01 15:55:16 +0000
commit74774c6318f04c5178acd2a334ad403dcd8eaf34 (patch)
tree2a700dc4114bcd3a5b4ea23b6453444126d5f3e9 /lib/sqlalchemy/orm
parentf8e4ed046dac730096034476218896046cb81b56 (diff)
parent8e890609eb47f5a273e695154cf143af56807921 (diff)
downloadsqlalchemy-74774c6318f04c5178acd2a334ad403dcd8eaf34.tar.gz
Merge "Add support for typing.Literal in Mapped" into main
Diffstat (limited to 'lib/sqlalchemy/orm')
-rw-r--r--lib/sqlalchemy/orm/decl_api.py14
-rw-r--r--lib/sqlalchemy/orm/decl_base.py3
2 files changed, 14 insertions, 3 deletions
diff --git a/lib/sqlalchemy/orm/decl_api.py b/lib/sqlalchemy/orm/decl_api.py
index c36089fde..e3e2611da 100644
--- a/lib/sqlalchemy/orm/decl_api.py
+++ b/lib/sqlalchemy/orm/decl_api.py
@@ -77,6 +77,7 @@ from ..util import typing as compat_typing
from ..util.typing import CallableReference
from ..util.typing import flatten_newtype
from ..util.typing import is_generic
+from ..util.typing import is_literal
from ..util.typing import is_newtype
from ..util.typing import Literal
@@ -1218,10 +1219,19 @@ class registry:
) -> Optional[sqltypes.TypeEngine[Any]]:
search: Iterable[Tuple[_MatchedOnType, Type[Any]]]
+ python_type_type: Type[Any]
if is_generic(python_type):
- python_type_type: Type[Any] = python_type.__origin__
- search = ((python_type, python_type_type),)
+ if is_literal(python_type):
+ python_type_type = cast("Type[Any]", python_type)
+
+ search = ( # type: ignore[assignment]
+ (python_type, python_type_type),
+ (Literal, python_type_type),
+ )
+ else:
+ python_type_type = python_type.__origin__
+ search = ((python_type, python_type_type),)
elif is_newtype(python_type):
python_type_type = flatten_newtype(python_type)
search = ((python_type, python_type_type),)
diff --git a/lib/sqlalchemy/orm/decl_base.py b/lib/sqlalchemy/orm/decl_base.py
index 0462a8945..a858f12cb 100644
--- a/lib/sqlalchemy/orm/decl_base.py
+++ b/lib/sqlalchemy/orm/decl_base.py
@@ -66,6 +66,7 @@ from ..util import topological
from ..util.typing import _AnnotationScanType
from ..util.typing import de_stringify_annotation
from ..util.typing import is_fwd_ref
+from ..util.typing import is_literal
from ..util.typing import Protocol
from ..util.typing import TypedDict
from ..util.typing import typing_get_args
@@ -1165,7 +1166,7 @@ class _ClassScanMapperConfig(_MapperConfig):
extracted_mapped_annotation, mapped_container = extracted
- if attr_value is None:
+ if attr_value is None and not is_literal(extracted_mapped_annotation):
for elem in typing_get_args(extracted_mapped_annotation):
if isinstance(elem, str) or is_fwd_ref(
elem, check_generic=True