From: Mike Bayer Date: Thu, 10 Oct 2024 02:05:05 +0000 (-0400) Subject: _Binary as generic to LargeBinary X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=858eba6156f210e24d39cc066069a3dac700e33a;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git _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 --- 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 145fce2fb4..bc2d898ab9 100644 --- a/lib/sqlalchemy/sql/sqltypes.py +++ b/lib/sqlalchemy/sql/sqltypes.py @@ -871,6 +871,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 8f18c47796..e47b85029a 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_):