]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Fix return type hint for `Query.get()`
authorSéamus Ó Ceanainn <seamusoceanainn@gmail.com>
Wed, 10 Dec 2025 17:14:25 +0000 (12:14 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 10 Dec 2025 18:28:08 +0000 (13:28 -0500)
<!-- Provide a general summary of your proposed changes in the Title field above -->

<!-- Describe your changes in detail -->

`Query[_T].get(...)` should return `Optional[_T]` instead of `Optional[Any]`. This is typed correctly when migrating to  `Session.get(_T, ...)`. By typing the legacy `Query.get(...)` call first, it should make migrations easier on developers, as it splits up the type checking improvements (and subsequent errors which may be discovered) from the `Query.get()`
 to `Session.get()` migration.

<!-- 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:

- [x] A documentation / typographical / small typing error fix
- Good to go, no issue or tests are needed
- [ ] 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: #13028
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/13028
Pull-request-sha: 38ec0c3ae072f8c7b62134dd017579a6a249219f

Change-Id: I7ae8e59bc0bd0a25c65de3804d60f6c2b6f50151

lib/sqlalchemy/orm/query.py
test/typing/plain_files/orm/typed_queries.py

index 1000b1991fce60974dccf4d678513eca9697f151..e07d7fc778f5f1ad0676f75998773380b74fbc2e 100644 (file)
@@ -1060,7 +1060,7 @@ class Query(
         ":meth:`_orm.Query.get`",
         alternative="The method is now available as :meth:`_orm.Session.get`",
     )
-    def get(self, ident: _PKIdentityArgument) -> Optional[Any]:
+    def get(self, ident: _PKIdentityArgument) -> Optional[_T]:
         """Return an instance based on the given primary key identifier,
         or ``None`` if not found.
 
index 8460dafcb9a7b08f2a69e3372acad388355926cb..da901ab752cdb9fe736ee941dce7aafc62410f66 100644 (file)
@@ -3,6 +3,7 @@ from __future__ import annotations
 from string.templatelib import Template
 from typing import Any
 from typing import assert_type
+from typing import Optional
 from typing import Unpack
 
 from sqlalchemy import Column
@@ -135,6 +136,8 @@ def t_legacy_query_single_entity() -> None:
 
     assert_type(q1, Query[User])
 
+    assert_type(q1.get(5), Optional[User])
+
     assert_type(q1.one(), User)
 
     assert_type(q1.all(), list[User])