]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commit
Fixes: #10673: make declared_attr covariant
authorLuiz Felipe Neves <luizfneves@proton.me>
Thu, 30 Apr 2026 17:06:25 +0000 (13:06 -0400)
committerFederico Caselli <cfederico87@gmail.com>
Thu, 30 Apr 2026 19:12:43 +0000 (21:12 +0200)
commitd611921fb6046e4bd3245d792de0d15757aa53f0
tree2843e0799deb88f61bedc935050cd721d1745ae9
parent6e9352815c93b936f3424a01fdd4d955516d2c5c
Fixes: #10673: make declared_attr covariant
<!-- Provide a general summary of your proposed changes in the Title field above -->

### Description
<!-- Describe your changes in detail -->
I made declared_attr covariant as suggested in #10673. mypy didn't seem to complain. Added a regression test for the use case that was asked for. Unfortunately, it seems like using `Mapped[int | UUID]` directly in the Protocol won't work:

```python
class CompareProtocol(Protocol):
    id: Mapped[int | UUID]
```

Because mypy will see this as a settable variable and not as a SQLAlchemy descriptor. Using `@property` instead seems to work and it's what I used in the test (perhaps it should be documented as the way to achieve this?):

```python
class CompareProtocol(Protocol):
    @property
    def id(self) -> Mapped[int | UUID]: ...
```

### 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 / small typing 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.

Closes: #13266
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/13266
Pull-request-sha: 8edd2841f4bbe61f8bb9bc15a7a57e0560698779

Change-Id: I7d63ad43df0ab34ee7c7389a007191be91efa574
(cherry picked from commit f1dfe10237d754ae25b9790c9a9e4d9defccb52b)
lib/sqlalchemy/orm/decl_api.py
test/typing/plain_files/orm/declared_attr_three.py [new file with mode: 0644]