]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Make ARRAY generic on the item_type
authorDenis Laxalde <denis@laxalde.org>
Tue, 18 Mar 2025 16:23:01 +0000 (12:23 -0400)
committersqla-tester <sqla-tester@sqlalchemy.org>
Tue, 18 Mar 2025 16:23:01 +0000 (12:23 -0400)
Now `Column(type_=ARRAY(Integer)` is inferred as `Column[Sequence[int]]` instead as `Column[Sequence[Any]]` previously. This only works with the `type_` argument to Column, but that's not new.

This follows from a suggestion at
https://github.com/sqlalchemy/sqlalchemy/pull/12386#issuecomment-2694056069.

Related to #6810.

Closes: #12443
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12443
Pull-request-sha: 2fff4e89cd0b72d9444ce3f3d845b152770fc55d

Change-Id: I87b828fd82d10fbf157141db3c31f0ec8149caad

lib/sqlalchemy/dialects/postgresql/array.py
lib/sqlalchemy/sql/sqltypes.py
test/typing/plain_files/dialects/postgresql/pg_stuff.py

index f32f146664209f7d0f6cb5f3234c60d0e6e8edaa..af026fb6ba8a9f29ccc14b19c8b82d9a7689051b 100644 (file)
@@ -197,7 +197,7 @@ class array(expression.ExpressionClauseList[_T]):
             return self
 
 
-class ARRAY(sqltypes.ARRAY):
+class ARRAY(sqltypes.ARRAY[_T]):
     """PostgreSQL ARRAY type.
 
     The :class:`_postgresql.ARRAY` type is constructed in the same way
@@ -271,7 +271,7 @@ class ARRAY(sqltypes.ARRAY):
 
     def __init__(
         self,
-        item_type: _TypeEngineArgument[typing_Any],
+        item_type: _TypeEngineArgument[_T],
         as_tuple: bool = False,
         dimensions: Optional[int] = None,
         zero_indexes: bool = False,
@@ -320,7 +320,7 @@ class ARRAY(sqltypes.ARRAY):
         self.dimensions = dimensions
         self.zero_indexes = zero_indexes
 
-    class Comparator(sqltypes.ARRAY.Comparator):
+    class Comparator(sqltypes.ARRAY.Comparator[_T]):
         """Define comparison operations for :class:`_types.ARRAY`.
 
         Note that these operations are in addition to those provided
@@ -361,7 +361,7 @@ class ARRAY(sqltypes.ARRAY):
     def _against_native_enum(self) -> bool:
         return (
             isinstance(self.item_type, sqltypes.Enum)
-            and self.item_type.native_enum
+            and self.item_type.native_enum  # type: ignore[attr-defined]
         )
 
     def literal_processor(
index 58af4cc0af276def8d5c7045208e89c95eac0f65..f71678a4ab41b4b6767fb3f50c7bec1754a8d6d0 100644 (file)
@@ -2813,7 +2813,7 @@ class JSON(Indexable, TypeEngine[Any]):
 
 
 class ARRAY(
-    SchemaEventTarget, Indexable, Concatenable, TypeEngine[Sequence[Any]]
+    SchemaEventTarget, Indexable, Concatenable, TypeEngine[Sequence[_T]]
 ):
     """Represent a SQL Array type.
 
@@ -2936,7 +2936,7 @@ class ARRAY(
 
     def __init__(
         self,
-        item_type: _TypeEngineArgument[Any],
+        item_type: _TypeEngineArgument[_T],
         as_tuple: bool = False,
         dimensions: Optional[int] = None,
         zero_indexes: bool = False,
@@ -2985,8 +2985,8 @@ class ARRAY(
         self.zero_indexes = zero_indexes
 
     class Comparator(
-        Indexable.Comparator[Sequence[Any]],
-        Concatenable.Comparator[Sequence[Any]],
+        Indexable.Comparator[Sequence[_T]],
+        Concatenable.Comparator[Sequence[_T]],
     ):
         """Define comparison operations for :class:`_types.ARRAY`.
 
@@ -2997,7 +2997,7 @@ class ARRAY(
 
         __slots__ = ()
 
-        type: ARRAY
+        type: ARRAY[_T]
 
         @overload
         def _setup_getitem(
index 9981e4a4fc1a00ba0071c187036d72fdb69e6636..b74ea53082c0cfc31efc5431b48bc29a1fd4a325 100644 (file)
@@ -117,3 +117,9 @@ reveal_type(array_of_ints)
 
 # EXPECTED_MYPY: Cannot infer type argument 1 of "array"
 array([0], type_=Text)
+
+# EXPECTED_TYPE: ARRAY[str]
+reveal_type(ARRAY(Text))
+
+# EXPECTED_TYPE: Column[Sequence[int]]
+reveal_type(Column(type_=ARRAY(Integer)))