Skip to content

Support SQLAlchemy 2.1 type hints - #77494

Merged
uranusjr merged 2 commits into
apache:mainfrom
astronomer:claude/mypy-sqlalchemy-handoff-6fd4d2
Nov 25, 2026
Merged

uranusjr merged 2 commits into
apache:mainfrom
astronomer:claude/mypy-sqlalchemy-handoff-6fd4d2

Conversation

@uranusjr

@uranusjr uranusjr commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

SQLAlchemy 2.1 changed how Select is parameterised: 2.0 takes the row tuple (Select[tuple[X, Y]]), while 2.1 takes the column types (Select[X, Y]). Runtime behaviour is unaffected, but each spelling is wrong for mypy under the other version. Since #76705, uv.lock resolves SQLAlchemy 2.1 only on Python 3.14+ (the Snowflake provider still caps it below 2.1 elsewhere, #75118), so local mypy runs on Python 3.14 failed while CI stayed green.

To work around this, a small Select shim is added for type checking only, together with a mypy plugin that tells mypy which SQLAlchemy is installed. Annotations are written the 2.1 way and type-check precisely on both versions. Once the lock resolves 2.1 everywhere, the plugin can be removed and the shim replaced with a plain sqlalchemy.sql.Select import, without touching the annotations.

While applying the shim, issues were found in LazySelectSequence. Its pickling support was left over from Airflow 2 (#28191, for @task.virtualenv) and has been unused for a while. It also didn't work: an unpickled sequence never got its session back, and its rebuilt statement couldn't be indexed or sliced. So it's removed rather than fixed, and pickling now raises TypeError. The class is also unhashable now, like a list. Pool.slots_stats now skips pool rows with no name, which SQLAlchemy 2.1's stricter types surfaced.

related: #76705, #75118

Comment thread airflow-core/src/airflow/utils/db.py Resolved
@Dev-iL

Dev-iL commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

SQLAlchemy 2.1 changed the type definition in a backward incompatible
way, causing Mypy issues with the existing code base (runtime support
does not break, only type hints). The new types should be the way going
forward, but unfortunately 2.1 does not support Python 3.13 and lower,
and some of the Airflow providers do not yet support 3.14.

To work around the issue, a small shim is implemented in Airflow with a
small plugin to switch behavior between the two distinct combinations.
Once Python 3.14 is supported across the board, we can simply remove the
plugin and change the type imports without needing to fix any of the
type hints.

While applying the shim, issues were discovered in LazySelectSequence.
After further inspection, the offending code was leftover from Airflow 2
that has been dead for quite a while and no longer needed. It doesn't
work if explicitly called anyway (reflected by the now-correct type
hints), and therefore has been removed entirely.
@uranusjr
uranusjr force-pushed the claude/mypy-sqlalchemy-handoff-6fd4d2 branch from 8f483c6 to 14a196a Compare November 23, 2026 22:17
@uranusjr
uranusjr enabled auto-merge (squash) November 23, 2026 22:17
@uranusjr

Copy link
Copy Markdown
Member Author

CI failures being fixed in #77535

@uranusjr
uranusjr merged commit c70a5ed into apache:main Nov 25, 2026
285 checks passed
@uranusjr
uranusjr deleted the claude/mypy-sqlalchemy-handoff-6fd4d2 branch November 25, 2026 00:00
@github-actions

Copy link
Copy Markdown
Contributor

Backport failed to create: v3-4-test. View the failure log Run details

Note: As of Merging PRs targeted for Airflow 3.X
the committer who merges the PR is responsible for backporting the PRs that are bug fixes (generally speaking) to the maintenance branches.

In matter of doubt please ask in #release-management Slack channel.

Status Branch Result
❌ v3-4-test Commit Link

You can attempt to backport this manually by running:

cherry_picker c70a5ed v3-4-test

This should apply the commit to the v3-4-test branch and leave the commit in conflict state marking
the files that need manual conflict resolution.

After you have resolved the conflicts, you can continue the backport process by running:

cherry_picker --continue

If you don't have cherry-picker installed, see the installation guide.

Lee-W added a commit to astronomer/airflow that referenced this pull request Nov 25, 2026
Since apache#77494 the type checker sees a single-column select as Select[int],
so the Select[tuple[int]] annotations no longer matched what the callers
pass.
Lee-W added a commit to viiccwen/airflow that referenced this pull request Nov 26, 2026
Since apache#77494 the type checker sees a single-column select as Select[int],
so the Select[tuple[int]] annotations no longer matched what the callers
pass.
Lee-W added a commit to viiccwen/airflow that referenced this pull request Nov 27, 2026
Since apache#77494 the type checker sees a single-column select as Select[int],
so the Select[tuple[int]] annotations no longer matched what the callers
pass.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants