]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Use independent TypeVar for ColumnElement.cast
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 8 Mar 2023 17:35:27 +0000 (12:35 -0500)
committermike bayer <mike_mp@zzzcomputing.com>
Wed, 8 Mar 2023 21:33:12 +0000 (21:33 +0000)
Fixed typing issue where :meth:`.ColumnElement.cast` did not allow a
:class:`.TypeEngine` argument independent of the type of the
:class:`.ColumnElement` itself, which is the purpose of
:meth:`.ColumnElement.cast`.

Fixes: #9451
Change-Id: I68119c6a9e8bf896715eea79be2b4f36b1c141de

doc/build/changelog/unreleased_20/9451.rst [new file with mode: 0644]
lib/sqlalchemy/sql/elements.py
test/ext/mypy/plain_files/sql_operations.py

diff --git a/doc/build/changelog/unreleased_20/9451.rst b/doc/build/changelog/unreleased_20/9451.rst
new file mode 100644 (file)
index 0000000..5ed6b7e
--- /dev/null
@@ -0,0 +1,8 @@
+.. change::
+    :tags: bug, typing
+    :tickets: 9451
+
+    Fixed typing issue where :meth:`.ColumnElement.cast` did not allow a
+    :class:`.TypeEngine` argument independent of the type of the
+    :class:`.ColumnElement` itself, which is the purpose of
+    :meth:`.ColumnElement.cast`.
index a416b6ac09682e41c609b8fea809667772e54d02..ef4587e1874e6b7ea4aa8f7b0ead9d8e00b6e745 100644 (file)
@@ -1635,7 +1635,7 @@ class ColumnElement(
             co._is_clone_of = selectable._is_clone_of.columns.get(key)
         return key, co
 
-    def cast(self, type_: _TypeEngineArgument[_T]) -> Cast[_T]:
+    def cast(self, type_: _TypeEngineArgument[_OPT]) -> Cast[_OPT]:
         """Produce a type cast, i.e. ``CAST(<expression> AS <type>)``.
 
         This is a shortcut to the :func:`_expression.cast` function.
index c55442be998d2738dc2808438c1d53d8f65258f2..d658f3d50fd202d7775179f319e43c3ae2bf9385 100644 (file)
@@ -2,8 +2,11 @@ import typing
 
 from sqlalchemy import and_
 from sqlalchemy import Boolean
+from sqlalchemy import cast
 from sqlalchemy import column
+from sqlalchemy import DateTime
 from sqlalchemy import false
+from sqlalchemy import Float
 from sqlalchemy import func
 from sqlalchemy import Integer
 from sqlalchemy import or_
@@ -75,6 +78,13 @@ and_(c1.notlike("x"))
 and_(c1.not_ilike("x"))
 and_(c1.notilike("x"))
 
+# issue #9451
+s1 = c1.cast(Integer)
+s2 = c1.cast(Float)
+s3 = c1.op("foobar")("operand").cast(DateTime)
+s4 = cast(c1, Float)
+s5 = cast(c1.op("foobar")("operand"), DateTime)
+
 
 if typing.TYPE_CHECKING: