Skip to content

Check for Deprecated SQLA Query Class Usage in Prevent Deprecated SQLA Hook - #55274

Merged
potiuk merged 7 commits into
apache:mainfrom
Prab-27:detect-deprecated-query-class
Dec 11, 2025
Merged

potiuk merged 7 commits into
apache:mainfrom
Prab-27:detect-deprecated-query-class

Conversation

@Prab-27

@Prab-27 Prab-27 commented Sep 4, 2025

Copy link
Copy Markdown
Contributor

This PR is part of the migration to SQLAlchemy 2.0

Previously, the hook only detected usage of .query() which returns a Query object.
This update extends the check to also flag direct usage of the deprecated Query class itself


^ Add meaningful description above
Read the Pull Request Guidelines for more information.
In case of fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in a newsfragment file, named {pr_number}.significant.rst or {issue_number}.significant.rst, in airflow-core/newsfragments.

@Prab-27

Prab-27 commented Sep 4, 2025 •

Copy link
Copy Markdown
Contributor Author

I noticed the Query class usage in airflow-core/src/airflow/models/dag.py yesterday while browsing the code—it wasn’t flagged by the hook. I’ve added support for detecting direct Query usage now. Let’s see where else it shows up so we can clean it up during the SQLAlchemy 2.0 migration.

I’ll be more careful next time—sorry about that!

@Prab-27

Prab-27 commented Sep 6, 2025 •

Copy link
Copy Markdown
Contributor Author

@potiuk , Could you please let me know if there are any other deprecated query forms I should add?
1 query attribute session.query() - added
2 class ofsession.orm-> Query - implementing here

This seems to be another deprecated pattern, since it's using query.filter() in this form . : here -
This function appears frequently throughout these files and seems to be a common pattern.

@potiuk

potiuk commented Sep 6, 2025

Copy link
Copy Markdown
Member

@potiuk , Could you please let me know if there are any other deprecated query forms I should add?

Hard to say - I am not well versed in SQLAlchemy 2 deprecations - but maybe @Dev-iL have more to say about it :)

@Dev-iL

Dev-iL commented Sep 7, 2025 •

Copy link
Copy Markdown
Collaborator

Hmm, I don't think there's anything I can add beyond what's written in the SQLA migration docs on this subject.

Perhaps try feeding the current hook implementation along with the full migration doc into a large-context LLM (like gemini) and asking for suggestions?

@Dev-iL

Dev-iL commented Sep 7, 2025

Copy link
Copy Markdown
Collaborator

Here's what Gemini had to say:

Based on the SQLAlchemy 1.4 to 2.0 migration documentation, here are some additional checks to include in the pre-commit hook to detect deprecated patterns:

Deprecated Query Object and its Usage

The provided script already checks for session.query() and from sqlalchemy.orm import Query, which is excellent. Other checks could include looking for the direct use of the Query object in other contexts.

Deprecated declarative Module

The sqlalchemy.ext.declarative package has been integrated into sqlalchemy.orm. While the old names still exist, they issue deprecation warnings. The pre-commit hook could check for the following:

  • Imports: Flag any imports from sqlalchemy.ext.declarative.
  • Function Calls: Look for calls to sqlalchemy.ext.declarative.declarative_base() and sqlalchemy.ext.declarative.declared_attr().

Deprecated Mapping Functions

Direct calls to mapper() and instrument_declarative() are deprecated. The hook can be extended to find and flag these function calls, suggesting a move to the new registry methods.

  • Classical Mapping: Look for direct calls to sqlalchemy.orm.mapper().
  • Imperative Mapping: Check for calls to sqlalchemy.orm.instrument_declarative().

Deprecated RowProxy and KeyedTuple

While these are less about code syntax and more about return types, a static analysis tool could potentially detect their usage if they are explicitly typed or a comment refers to them. A more practical check for a pre-commit hook would be to focus on the function calls that produce these deprecated types.

Other Deprecated Features

The hook can also be expanded to detect other deprecated behaviors mentioned in the documentation, such as cascade_backrefs. This would involve looking for cascade arguments that include backrefs within relationship() calls.

@Prab-27

Prab-27 commented Sep 8, 2025

Copy link
Copy Markdown
Contributor Author

It seems like these are also deprecated query patterns, and I need to add filters for them

@Prab-27
Prab-27 force-pushed the detect-deprecated-query-class branch 2 times, most recently from 8d71248 to 090779a Compare September 18, 2025 10:02
@Prab-27
Prab-27 marked this pull request as ready for review September 18, 2025 10:59
@Prab-27

Prab-27 commented Sep 18, 2025

Copy link
Copy Markdown
Contributor Author

I think these query patterns are used by the standard library, so we can't remove them
here , here and many more

@vincbeck ,Happy to adjust if you have other ideas.

@vincbeck

Copy link
Copy Markdown
Contributor

I think these query patterns are used by the standard library, so we can't remove them here , here and many more

@vincbeck ,Happy to adjust if you have other ideas.

These have existed for a while so we definitely need them (I double checked and we use them). We are currently in the process of upgrading Flask-appbuilder to 5 in #50960. If you look at the PR, we no longer need CustomSQLAInterface that is the only class that uses all these filters so we will most likely able to remove these filters after migration.

@Prab-27
Prab-27 force-pushed the detect-deprecated-query-class branch from 090779a to 57e55de Compare October 3, 2025 06:07
@Prab-27
Prab-27 force-pushed the detect-deprecated-query-class branch 2 times, most recently from 11c7802 to 2375285 Compare October 17, 2025 09:40
@Prab-27

Prab-27 commented Nov 3, 2025

Copy link
Copy Markdown
Contributor Author

Oops, I'm resolving the conflicts.

Comment thread airflow-core/src/airflow/models/dag.py Resolved
@Prab-27

Prab-27 commented Nov 10, 2025

Copy link
Copy Markdown
Contributor Author

Oops! A few changes need fixing — I’ll sort them out soon

@Prab-27 Prab-27 mentioned this pull request Nov 11, 2025
@Dev-iL Dev-iL mentioned this pull request Nov 12, 2025
@Prab-27
Prab-27 force-pushed the detect-deprecated-query-class branch from 9dce01e to 84f249c Compare December 10, 2025 05:05
@Prab-27

Prab-27 commented Dec 11, 2025

Copy link
Copy Markdown
Contributor Author

Thanks @Dev-iL !

@potiuk
potiuk merged commit 3966d0a into apache:main Dec 11, 2025
jhgoebbert pushed a commit to jhgoebbert/airflow_Owen-CH-Leung that referenced this pull request Feb 8, 2026
…A Hook (apache#55274)

* add conditions for query class in ast.tree

* Remove Query class

* remove Query obj from dagrun.py

* resolve conflicts

* re-added import that was mistakenly deleted

* remove Query from type-hint

* fixing static checks
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.

6 participants