From a1cf687380f99d20ffa13dd41771937e084e54b6 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Wed, 9 Oct 2024 22:05:05 -0400 Subject: [PATCH] _Binary as generic to LargeBinary 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 | 7 +++++++ lib/sqlalchemy/sql/sqltypes.py | 6 ++++++ test/sql/test_types.py | 9 +++++++++ 3 files changed, 22 insertions(+) create mode 100644 doc/build/changelog/unreleased_20/11978.rst diff --git a/doc/build/changelog/unreleased_20/11978.rst b/doc/build/changelog/unreleased_20/11978.rst new file mode 100644 index 0000000000..a8a9cdaf57 --- /dev/null +++ b/doc/build/changelog/unreleased_20/11978.rst @@ -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. diff --git a/lib/sqlalchemy/sql/sqltypes.py b/lib/sqlalchemy/sql/sqltypes.py index ad9a696ee8..dd7110e880 100644 --- a/lib/sqlalchemy/sql/sqltypes.py +++ b/lib/sqlalchemy/sql/sqltypes.py @@ -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 diff --git a/test/sql/test_types.py b/test/sql/test_types.py index 999919c5f5..702f70edc7 100644 --- a/test/sql/test_types.py +++ b/test/sql/test_types.py @@ -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_): -- 2.47.3