]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
use proper functions to get typing origin, args
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 4 Sep 2024 03:08:21 +0000 (23:08 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 4 Sep 2024 03:08:21 +0000 (23:08 -0400)
Fixed regression caused by issue :ticket:`11814` which broke support for
certain flavors of :pep:`593` ``Annotated`` in the type_annotation_map when
builtin types such as ``list``, ``dict`` were used without an element type.
While this is an incomplete style of typing, these types nonetheless
previously would be located in the type_annotation_map correctly.

Fixes: #11831
Change-Id: I6ea7fc1bce462d44ffcf67ef18b60050dfc2c91e

doc/build/changelog/unreleased_20/11831.rst [new file with mode: 0644]
lib/sqlalchemy/util/typing.py
test/orm/declarative/test_tm_future_annotations_sync.py
test/orm/declarative/test_typed_mapping.py

diff --git a/doc/build/changelog/unreleased_20/11831.rst b/doc/build/changelog/unreleased_20/11831.rst
new file mode 100644 (file)
index 0000000..65699bf
--- /dev/null
@@ -0,0 +1,9 @@
+.. change::
+    :tags: bug, orm
+    :tickets: 11831
+
+    Fixed regression caused by issue :ticket:`11814` which broke support for
+    certain flavors of :pep:`593` ``Annotated`` in the type_annotation_map when
+    builtin types such as ``list``, ``dict`` were used without an element type.
+    While this is an incomplete style of typing, these types nonetheless
+    previously would be located in the type_annotation_map correctly.
index f4f14e1b56d83d1e330f64d621be50cfa7f750ba..7be6589e03d78c55d9249886aee2f8f37faa6878 100644 (file)
@@ -201,9 +201,10 @@ def fixup_container_fwd_refs(
     and similar for list, set
 
     """
+
     if (
         is_generic(type_)
-        and type_.__origin__
+        and typing_get_origin(type_)
         in (
             dict,
             set,
@@ -223,11 +224,11 @@ def fixup_container_fwd_refs(
         )
     ):
         # compat with py3.10 and earlier
-        return type_.__origin__.__class_getitem__(  # type: ignore
+        return typing_get_origin(type_).__class_getitem__(  # type: ignore
             tuple(
                 [
                     ForwardRef(elem) if isinstance(elem, str) else elem
-                    for elem in type_.__args__
+                    for elem in typing_get_args(type_)
                 ]
             )
         )
index e9b74b0d93f33cdf1c1160a86077d75bbbdfdf6d..eb1e605d10e0af094f0312463b05f1545026f558 100644 (file)
@@ -1419,6 +1419,16 @@ class MappedColumnTest(fixtures.TestBase, testing.AssertsCompiledSQL):
             Dict,
             (str, str),
         ),
+        (list, None, testing.requires.python310),
+        (
+            List,
+            None,
+        ),
+        (dict, None, testing.requires.python310),
+        (
+            Dict,
+            None,
+        ),
         id_="sa",
         argnames="container_typ,args",
     )
@@ -1428,22 +1438,30 @@ class MappedColumnTest(fixtures.TestBase, testing.AssertsCompiledSQL):
 
         test #11814
 
+        test #11831, regression from #11814
         """
 
         global TestType
 
         if style.pep593:
-            TestType = Annotated[container_typ[args], 0]
+            if args is None:
+                TestType = Annotated[container_typ, 0]
+            else:
+                TestType = Annotated[container_typ[args], 0]
         elif style.alias:
-            TestType = container_typ[args]
+            if args is None:
+                TestType = container_typ
+            else:
+                TestType = container_typ[args]
         elif style.direct:
             TestType = container_typ
-            double_strings = args == (str, str)
 
         class Base(DeclarativeBase):
             if style.direct:
-                if double_strings:
+                if args == (str, str):
                     type_annotation_map = {TestType[str, str]: JSON()}
+                elif args is None:
+                    type_annotation_map = {TestType: JSON()}
                 else:
                     type_annotation_map = {TestType[str]: JSON()}
             else:
@@ -1455,8 +1473,10 @@ class MappedColumnTest(fixtures.TestBase, testing.AssertsCompiledSQL):
             id: Mapped[int] = mapped_column(primary_key=True)
 
             if style.direct:
-                if double_strings:
+                if args == (str, str):
                     data: Mapped[TestType[str, str]] = mapped_column()
+                elif args is None:
+                    data: Mapped[TestType] = mapped_column()
                 else:
                     data: Mapped[TestType[str]] = mapped_column()
             else:
index 5060ac613161565780a3aed48610b54f25258891..c9eacbae7da81343ee048dee23ddff1238a9c7c9 100644 (file)
@@ -1410,6 +1410,16 @@ class MappedColumnTest(fixtures.TestBase, testing.AssertsCompiledSQL):
             Dict,
             (str, str),
         ),
+        (list, None, testing.requires.python310),
+        (
+            List,
+            None,
+        ),
+        (dict, None, testing.requires.python310),
+        (
+            Dict,
+            None,
+        ),
         id_="sa",
         argnames="container_typ,args",
     )
@@ -1419,22 +1429,30 @@ class MappedColumnTest(fixtures.TestBase, testing.AssertsCompiledSQL):
 
         test #11814
 
+        test #11831, regression from #11814
         """
 
         global TestType
 
         if style.pep593:
-            TestType = Annotated[container_typ[args], 0]
+            if args is None:
+                TestType = Annotated[container_typ, 0]
+            else:
+                TestType = Annotated[container_typ[args], 0]
         elif style.alias:
-            TestType = container_typ[args]
+            if args is None:
+                TestType = container_typ
+            else:
+                TestType = container_typ[args]
         elif style.direct:
             TestType = container_typ
-            double_strings = args == (str, str)
 
         class Base(DeclarativeBase):
             if style.direct:
-                if double_strings:
+                if args == (str, str):
                     type_annotation_map = {TestType[str, str]: JSON()}
+                elif args is None:
+                    type_annotation_map = {TestType: JSON()}
                 else:
                     type_annotation_map = {TestType[str]: JSON()}
             else:
@@ -1446,8 +1464,10 @@ class MappedColumnTest(fixtures.TestBase, testing.AssertsCompiledSQL):
             id: Mapped[int] = mapped_column(primary_key=True)
 
             if style.direct:
-                if double_strings:
+                if args == (str, str):
                     data: Mapped[TestType[str, str]] = mapped_column()
+                elif args is None:
+                    data: Mapped[TestType] = mapped_column()
                 else:
                     data: Mapped[TestType[str]] = mapped_column()
             else: