]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
handle builtins types in annotations with __allow_unmapped__ 10390/head
authorPascal Corpet <pascal@co2ai.com>
Wed, 27 Sep 2023 22:34:15 +0000 (00:34 +0200)
committerPascal Corpet <pascal@co2ai.com>
Wed, 27 Sep 2023 22:34:15 +0000 (00:34 +0200)
Fixes #10385

lib/sqlalchemy/util/typing.py
test/orm/declarative/test_tm_future_annotations_sync.py
test/orm/declarative/test_typed_mapping.py

index 597549ce77d390df40d5957dbaf2122bd9b337a9..4f051a1e3ca5ec7c0267b5d5b4d9051b0032382a 100644 (file)
@@ -8,6 +8,7 @@
 
 from __future__ import annotations
 
+import builtins
 import re
 import sys
 import typing
@@ -235,6 +236,12 @@ def eval_name_only(
     if "." in name:
         return eval_expression(name, module_name, locals_=locals_)
 
+    # Try builtins first, to handle `list`, `set` or `dict`, etc.
+    try:
+        return builtins.__dict__[name]
+    except KeyError:
+        pass
+
     try:
         base_globals: Dict[str, Any] = sys.modules[module_name].__dict__
     except KeyError as ke:
index bb392ba3144ae7c3b5fd3cf09a0ffab5219710f0..57ae5a206446fdba741b8e64cea7e31197d9ffb6 100644 (file)
@@ -2167,8 +2167,15 @@ class RelationshipLHSTest(fixtures.TestBase, testing.AssertsCompiledSQL):
 
                 decl_base.registry.configure()
 
-    def test_14_style_anno_accepted_w_allow_unmapped(self):
-        """test for #8692"""
+    @testing.combinations(
+        ("list", testing.requires.python310),
+        ("List",),
+        ("set", testing.requires.python310),
+        ("Set",),
+        argnames="collection_type",
+    )
+    def test_14_style_anno_accepted_w_allow_unmapped(self, collection_type):
+        """test for #8692 and #10385"""
 
         class Base(DeclarativeBase):
             __allow_unmapped__ = True
@@ -2178,7 +2185,29 @@ class RelationshipLHSTest(fixtures.TestBase, testing.AssertsCompiledSQL):
 
             id: Mapped[int] = mapped_column(primary_key=True)
             data: str = Column(String)
-            bs: List["B"] = relationship("B", back_populates="a")  # noqa: F821
+
+            if collection_type == "list":
+                bs: list["B"] = relationship(  # noqa: F821
+                    "B",
+                    back_populates="a",
+                )
+            elif collection_type == "List":
+                bs: List["B"] = relationship(  # noqa: F821
+                    "B",
+                    back_populates="a",
+                )
+            elif collection_type == "set":
+                bs: set["B"] = relationship(  # noqa: F821
+                    "B",
+                    back_populates="a",
+                )
+            elif collection_type == "Set":
+                bs: Set["B"] = relationship(  # noqa: F821
+                    "B",
+                    back_populates="a",
+                )
+            else:
+                collection_type.fail()
 
         class B(Base):
             __tablename__ = "b"
index de2f6f94a73a69bec401cc6003f7f4188e674fc6..2a4b378c5198f0171ef031ecea818ecdb1586708 100644 (file)
@@ -2158,8 +2158,15 @@ class RelationshipLHSTest(fixtures.TestBase, testing.AssertsCompiledSQL):
 
                 decl_base.registry.configure()
 
-    def test_14_style_anno_accepted_w_allow_unmapped(self):
-        """test for #8692"""
+    @testing.combinations(
+        ("list", testing.requires.python310),
+        ("List",),
+        ("set", testing.requires.python310),
+        ("Set",),
+        argnames="collection_type",
+    )
+    def test_14_style_anno_accepted_w_allow_unmapped(self, collection_type):
+        """test for #8692 and #10385"""
 
         class Base(DeclarativeBase):
             __allow_unmapped__ = True
@@ -2169,7 +2176,29 @@ class RelationshipLHSTest(fixtures.TestBase, testing.AssertsCompiledSQL):
 
             id: Mapped[int] = mapped_column(primary_key=True)
             data: str = Column(String)
-            bs: List["B"] = relationship("B", back_populates="a")  # noqa: F821
+
+            if collection_type == "list":
+                bs: list["B"] = relationship(  # noqa: F821
+                    "B",
+                    back_populates="a",
+                )
+            elif collection_type == "List":
+                bs: List["B"] = relationship(  # noqa: F821
+                    "B",
+                    back_populates="a",
+                )
+            elif collection_type == "set":
+                bs: set["B"] = relationship(  # noqa: F821
+                    "B",
+                    back_populates="a",
+                )
+            elif collection_type == "Set":
+                bs: Set["B"] = relationship(  # noqa: F821
+                    "B",
+                    back_populates="a",
+                )
+            else:
+                collection_type.fail()
 
         class B(Base):
             __tablename__ = "b"