]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
_Binary as generic to LargeBinary
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 10 Oct 2024 02:05:05 +0000 (22:05 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 10 Oct 2024 15:52:45 +0000 (11:52 -0400)
Datatypes that are binary based such as :class:`.VARBINARY` will resolve to
:class:`.LargeBinary` when the :meth:`.TypeEngine.as_generic()` method is
called.

Fixes: #11978
Change-Id: I2e0586324fb0f1c367da61f0074b35c96fbe2fd0
(cherry picked from commit 858eba6156f210e24d39cc066069a3dac700e33a)

doc/build/changelog/unreleased_20/11978.rst [new file with mode: 0644]
lib/sqlalchemy/sql/sqltypes.py
test/sql/test_types.py

diff --git a/doc/build/changelog/unreleased_20/11978.rst b/doc/build/changelog/unreleased_20/11978.rst
new file mode 100644 (file)
index 0000000..a8a9cda
--- /dev/null
@@ -0,0 +1,7 @@
+.. change::
+    :tags: usecase, sql
+    :tickets: 11978
+
+    Datatypes that are binary based such as :class:`.VARBINARY` will resolve to
+    :class:`.LargeBinary` when the :meth:`.TypeEngine.as_generic()` method is
+    called.
index ad9a696ee8262639ba5e65859d28f53aa4c7a59c..dd7110e8801895ae7fd537f33156dc2422a35597 100644 (file)
@@ -870,6 +870,12 @@ class _Binary(TypeEngine[bytes]):
     def __init__(self, length: Optional[int] = None):
         self.length = length
 
+    @util.ro_memoized_property
+    def _generic_type_affinity(
+        self,
+    ) -> Type[TypeEngine[bytes]]:
+        return LargeBinary
+
     def literal_processor(self, dialect):
         def process(value):
             # TODO: this is useless for real world scenarios; implement
index 999919c5f5127da777dc6aefcac4b219656161c3..702f70edc78d54268039b8969f5a47f55ae0f360 100644 (file)
@@ -62,6 +62,7 @@ from sqlalchemy import TypeDecorator
 from sqlalchemy import types
 from sqlalchemy import Unicode
 from sqlalchemy import util
+from sqlalchemy import VARBINARY
 from sqlalchemy import VARCHAR
 import sqlalchemy.dialects.mysql as mysql
 import sqlalchemy.dialects.oracle as oracle
@@ -450,6 +451,11 @@ class TypeAffinityTest(fixtures.TestBase):
 class AsGenericTest(fixtures.TestBase):
     @testing.combinations(
         (String(), String()),
+        (VARBINARY(), LargeBinary()),
+        (mysql.BINARY(), LargeBinary()),
+        (mysql.MEDIUMBLOB(), LargeBinary()),
+        (oracle.RAW(), LargeBinary()),
+        (pg.BYTEA(), LargeBinary()),
         (VARCHAR(length=100), String(length=100)),
         (NVARCHAR(length=100), Unicode(length=100)),
         (DATE(), Date()),
@@ -472,6 +478,9 @@ class AsGenericTest(fixtures.TestBase):
             (t,)
             for t in _all_types(omit_special_types=True)
             if not util.method_is_overridden(t, TypeEngine.as_generic)
+            and not util.method_is_overridden(
+                t, TypeEngine._generic_type_affinity
+            )
         ]
     )
     def test_as_generic_all_types_heuristic(self, type_):