]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Implement TypeEngine.as_generic() and tests 5713/head
authorAndrew Hannigan <andrewhannigan@Andrews-MacBook-Pro.local>
Wed, 18 Nov 2020 15:55:01 +0000 (09:55 -0600)
committerAndrew Hannigan <andrewhannigan@Andrews-MacBook-Pro.local>
Wed, 18 Nov 2020 16:45:30 +0000 (10:45 -0600)
Fixes: #5659
lib/sqlalchemy/sql/type_api.py
test/sql/test_types.py

index bca6e9020e44b816db0ad4c221d1e076e26351f4..e4d66c54f7254809206eed6b578ff3334e03d071 100644 (file)
@@ -470,7 +470,7 @@ class TypeEngine(Traversible):
                     "sqlalchemy.sql.sqltypes",
                     "sqlalchemy.sql.type_api",
                 )
-                and t._is_generic_type()
+                and hasattr(t, '_is_generic_type') and t._is_generic_type()
             ):
                 if t in (TypeEngine, UserDefinedType):
                     return NULLTYPE.__class__
@@ -478,6 +478,11 @@ class TypeEngine(Traversible):
         else:
             return self.__class__
 
+    def as_generic(self):
+        """Return an instance of the generic type corresponding to this type"""
+
+        return util.constructor_copy(self, self._generic_type_affinity())
+
     def dialect_impl(self, dialect):
         """Return a dialect-specific implementation for this
         :class:`.TypeEngine`.
index fd1783e09806177acb953da3225526f493e20ec4..570383dfedb838d2e428bea48b2f0e0ca1253049 100644 (file)
@@ -383,6 +383,26 @@ class TypeAffinityTest(fixtures.TestBase):
         assert t1.dialect_impl(d)._type_affinity is postgresql.UUID
 
 
+class AsGenericTest(fixtures.TestBase):
+    @testing.combinations(
+        (String(), String()),
+        (VARCHAR(length=100), String(length=100)),
+        (NVARCHAR(length=100), Unicode(length=100)),
+        (DATE(), Date()),
+    )
+    def test_as_generic(self, t1, t2):
+        assert repr(t1.as_generic()) == repr(t2)
+
+    @testing.combinations(*[(t,) for t in _all_types(omit_special_types=True)])
+    def test_as_generic_all_types(self, type_):
+        if issubclass(type_, ARRAY):
+            t1 = type_(String)
+        else:
+            t1 = type_()
+
+        t1.as_generic()
+
+
 class PickleTypesTest(fixtures.TestBase):
     @testing.combinations(
         ("Boo", Boolean()),