Skip to content

Fix MyPy type errors in airflow-core/src/airflow/models/dag.py for Sqlalchemy 2 migration - #57532

Closed
KoviAnusha wants to merge 19 commits into
apache:mainfrom
KoviAnusha:models
Closed

KoviAnusha wants to merge 19 commits into
apache:mainfrom
KoviAnusha:models

Conversation

@KoviAnusha

@KoviAnusha KoviAnusha commented Oct 30, 2025 •

Copy link
Copy Markdown
Contributor

related: #56735

  1. airflow-core/src/airflow/models/dagbag.py
  2. airflow-core/src/airflow/models/dag.py

mypy errors:

airflow-core/src/airflow/models/dagbag.py:75: error: Need type annotation for
"info" [var-annotated]
info = inspect(dag_run)
^~~~~~~~~~~~~~~~
airflow-core/src/airflow/models/dagbag.py:81: error: Incompatible return value
type (got "DagVersion | None", expected "DagVersion") [return-value]
return session.get(DagVersion, dag_run.created_dag_version_id)
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
airflow-core/src/airflow/models/dag.py:139: error: Argument 2 to
"infer_automated_data_interval" has incompatible type "datetime | None";
expected "datetime" [arg-type]
return infer_automated_data_interval(timetable, run.logical_date)
^~~~~~~~~~~~~~~~
airflow-core/src/airflow/models/dag.py:525: error: Incompatible types in
assignment (expression has type "set[Any]", variable has type
"Result[tuple[str]]") [assignment]
paused_dag_ids = {paused_dag_id for (paused_dag_id,) in paused...
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~...
airflow-core/src/airflow/models/dag.py:526: error: Incompatible return value
type (got "Result[tuple[str]]", expected "set[str]") [return-value]
return paused_dag_ids
^~~~~~~~~~~~~~


^ 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.

@KoviAnusha
KoviAnusha marked this pull request as ready for review October 30, 2025 15:41
Comment thread airflow-core/src/airflow/models/xcom.py Resolved
Comment thread airflow-core/src/airflow/models/dag.py Resolved
@KoviAnusha KoviAnusha changed the title Fix MyPy type errors in airflow-core/src/airflow/models/ (dagbag,xcom,dag,variable) for Sqlalchemy 2 migration Fix MyPy type errors in airflow-core/src/airflow/models/ (dagbag,xcom,dag) for Sqlalchemy 2 migration Nov 15, 2025
@KoviAnusha

Copy link
Copy Markdown
Contributor Author

@uranusjr @Dev-iL could you please review this PR when you have time?

Comment thread airflow-core/src/airflow/models/xcom.py Resolved
Comment thread airflow-core/src/airflow/models/xcom.py Resolved
Comment thread airflow-core/src/airflow/models/xcom.py Resolved
Comment thread airflow-core/src/airflow/utils/db.py Resolved
@KoviAnusha KoviAnusha changed the title Fix MyPy type errors in airflow-core/src/airflow/models/ (dagbag,xcom,dag) for Sqlalchemy 2 migration Fix MyPy type errors in airflow-core/src/airflow/models/ (dagbag,dag) for Sqlalchemy 2 migration Nov 18, 2025
Comment thread airflow-core/src/airflow/models/xcom.py Outdated
log.warning(
warning_message,
"return value" if key == XCOM_RETURN_KEY else f"value {key}",
"return value" if key == XCOM_RETURN_KEY else "value %s",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t think this would work. This is the second argument and putting the template placeholder would not work as you expect.

>>> import logging, sys
>>> logging.basicConfig(stream=sys.stdout, level=logging.INFO)
>>> logging.getLogger('').info('foo %s', 'bar %s', 1)
--- Logging error ---
Traceback (most recent call last):
  File "logging/__init__.py", line 1100, in emit
    msg = self.format(record)
  File "logging/__init__.py", line 943, in format
    return fmt.format(record)
  File "logging/__init__.py", line 678, in format
    record.message = record.getMessage()
  File "logging/__init__.py", line 368, in getMessage
    msg = msg % self.args
TypeError: not all arguments converted during string formatting
Call stack:
  File "<stdin>", line 1, in <module>
Message: 'foo %s'
Arguments: ('bar %s', 1)

It’s probably a good idea to switch this to use structlog instead.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for suggesting this approach. I have addressed this. Please review.

@KoviAnusha KoviAnusha changed the title Fix MyPy type errors in airflow-core/src/airflow/models/ (dagbag,dag) for Sqlalchemy 2 migration Fix MyPy type errors in airflow-core/src/airflow/models/ (dag) for Sqlalchemy 2 migration Nov 23, 2025
@KoviAnusha KoviAnusha changed the title Fix MyPy type errors in airflow-core/src/airflow/models/ (dag) for Sqlalchemy 2 migration Fix MyPy type errors in airflow-core/src/airflow/models/dag.py for Sqlalchemy 2 migration Nov 23, 2025
@vincbeck
vincbeck requested a review from uranusjr November 25, 2025 15:13
@vincbeck

vincbeck commented Dec 1, 2025

Copy link
Copy Markdown
Contributor

All mypy errors have been fixed. Closing it

@vincbeck vincbeck closed this Dec 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants