summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYurii Karabas <1998uriyyo@gmail.com>2023-01-28 09:27:35 -0500
committersqla-tester <sqla-tester@sqlalchemy.org>2023-01-28 09:27:35 -0500
commit9c4267f2442145378922498ca735d96c8f4bca42 (patch)
treec95ff3c8ce6119c40e8d1479bf427b439b6940f0
parentecdaf44711f1feb002ad98b6e917cc575a1bd801 (diff)
downloadsqlalchemy-9c4267f2442145378922498ca735d96c8f4bca42.tar.gz
Set correct type annotations for ColumnElement.cast
<!-- Provide a general summary of your proposed changes in the Title field above --> Fixes: #9156 ### Description <!-- Describe your changes in detail --> ### Checklist <!-- go over following points. check them with an `x` if they do apply, (they turn into clickable checkboxes once the PR is submitted, so no need to do everything at once) --> This pull request is: - [ ] A documentation / typographical error fix - Good to go, no issue or tests are needed - [x] A short code fix - please include the issue number, and create an issue if none exists, which must include a complete example of the issue. one line code fixes without an issue and demonstration will not be accepted. - Please include: `Fixes: #<issue number>` in the commit message - please include tests. one line code fixes without tests will not be accepted. - [ ] A new feature implementation - please include the issue number, and create an issue if none exists, which must include a complete example of how the feature would look. - Please include: `Fixes: #<issue number>` in the commit message - please include tests. **Have a nice day!** Closes: #9157 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/9157 Pull-request-sha: 321dac184ee0d317296a689d7c8e47bf1464bcce Change-Id: I99fe759a21de910f34bae3bb919e82cd08969e81
-rw-r--r--doc/build/changelog/unreleased_20/9156.rst7
-rw-r--r--lib/sqlalchemy/sql/elements.py2
-rw-r--r--test/ext/mypy/plugin_files/issue_9156.py11
3 files changed, 19 insertions, 1 deletions
diff --git a/doc/build/changelog/unreleased_20/9156.rst b/doc/build/changelog/unreleased_20/9156.rst
new file mode 100644
index 000000000..9fbeb44eb
--- /dev/null
+++ b/doc/build/changelog/unreleased_20/9156.rst
@@ -0,0 +1,7 @@
+.. change::
+ :tags: bug, typing
+ :tickets: 9156
+
+ The :meth:`_expression.ColumnElement.cast` is typed to accept
+ both ``Type[TypeEngine[T]]`` and ``TypeEngine[T]`` rather only
+ ``TypeEngine[T]``. \ No newline at end of file
diff --git a/lib/sqlalchemy/sql/elements.py b/lib/sqlalchemy/sql/elements.py
index 71d8cb2d3..c3f7b884c 100644
--- a/lib/sqlalchemy/sql/elements.py
+++ b/lib/sqlalchemy/sql/elements.py
@@ -1641,7 +1641,7 @@ class ColumnElement(
co._is_clone_of = selectable._is_clone_of.columns.get(key)
return key, co
- def cast(self, type_: TypeEngine[_T]) -> Cast[_T]:
+ def cast(self, type_: _TypeEngineArgument[_T]) -> Cast[_T]:
"""Produce a type cast, i.e. ``CAST(<expression> AS <type>)``.
This is a shortcut to the :func:`_expression.cast` function.
diff --git a/test/ext/mypy/plugin_files/issue_9156.py b/test/ext/mypy/plugin_files/issue_9156.py
new file mode 100644
index 000000000..46e5e9570
--- /dev/null
+++ b/test/ext/mypy/plugin_files/issue_9156.py
@@ -0,0 +1,11 @@
+from typing import Any, Type
+
+from sqlalchemy.sql.elements import ColumnElement
+from sqlalchemy.sql.type_api import TypeEngine
+
+col: ColumnElement[Any]
+type_: Type[TypeEngine[Any]]
+obj: TypeEngine[Any]
+
+col.cast(type_)
+col.cast(obj)