Skip to content

Fix MyPy type errors in airflow/models(serialized_dag.py, connection.py) - #58062

Closed
KoviAnusha wants to merge 24 commits into
apache:mainfrom
KoviAnusha:mypy-1
Closed

KoviAnusha wants to merge 24 commits into
apache:mainfrom
KoviAnusha:mypy-1

Conversation

@KoviAnusha

@KoviAnusha KoviAnusha commented Nov 7, 2025 •

Copy link
Copy Markdown
Contributor

related: #56735

  1. airflow-core/src/airflow/models/serialized_dag.py
  2. airflow-core/src/airflow/models/connection.py

airflow-core/src/airflow/models/serialized_dag.py:451: error: "Result[Any]" has
no attribute "rowcount" [attr-defined]
if result.rowcount == 0:
^~~~~~~~~~~~~~~
airflow-core/src/airflow/models/serialized_dag.py:517: error: Incompatible
return value type (got "Sequence[SerializedDagModel]", expected
"list[SerializedDagModel]") [return-value]
return latest_serdags or []
^~~~~~~~~~~~~~~~~~~~
airflow-core/src/airflow/models/serialized_dag.py:562: error: Incompatible
types in assignment (expression has type "dict[Any, Any] | None", variable has
type "dict[Any, Any]") [assignment]
self.__data_cache = self._data
^~~~~~~~~~
airflow-core/src/airflow/models/serialized_dag.py:627: error: Incompatible
types in assignment (expression has type "BinaryExpression[Any]", variable has
type "Function[Any]") [assignment]
data_col_to_select = cls._data.op("#>")(literal('{"dag...
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~...
airflow-core/src/airflow/models/serialized_dag.py:633: error: Incompatible
types in assignment (expression has type "InstrumentedAttribute[bytes | None]",
variable has type "Function[Any]") [assignment]
data_col_to_select = cls._data_compressed
^~~~~~~~~~~~~~~~~~~~
airflow-core/src/airflow/models/serialized_dag.py:656: error: Argument
"dag_id_dependencies" to "_DagDependenciesResolver" has incompatible type
"list[tuple[Any, Any]] | Sequence[Row[Any]]"; expected
"Sequence[tuple[str, dict[Any, Any]]]" [arg-type]
...r = _DagDependenciesResolver(dag_id_dependencies=iterator, session=ses...
^~~~~~~~
airflow-core/src/airflow/models/connection.py:377: error: Incompatible types in
assignment (expression has type "str | None", variable has type "str")
[assignment]
extra_val = self._extra
^~~~~~~~~~~


^ 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 November 11, 2025 18:21
@KoviAnusha KoviAnusha changed the title Fix MyPy type errors in airflow/models(serialized_dag.py) Fix MyPy type errors in airflow/models(serialized_dag.py, connection.py) Nov 16, 2025
try:
as_bytes = bytes(fld, "utf-8")
except (TypeError, UnicodeEncodeError):
return fld

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.

The input shouldn’t be returned without at least a log. Maybe let’s not do this here, but in a separate PR? This PR is for type changes, but this function changes logic.

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.

This is suggested here #58062 (comment). So I have implemented this.

@Dev-iL Dev-iL Nov 18, 2025 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Context:

This is the sort of issue in question:

airflow-core/src/airflow/models/connection.py:377: error: Incompatible types in assignment (expression has type "str | None", variable
has type "str")  [assignment]
                extra_val = self._extra
                            ^~~~~~~~~~~

The solution proposed before my suggestion was this:

            try:
                return fernet.decrypt(bytes(self._password, "utf-8")).decode()
            except Exception as e:
                from cryptography.fernet import InvalidToken

                if isinstance(e, InvalidToken):
                    return self._password
                raise

I suggested the helper method for two reasons:

  1. We have the fernet.decrypt(bytes(self.*, "utf-8")).decode() patterns in more than one place.
  2. except Exception is too broad

Therefore, it made sense to me to handle the exceptions of this fairly complex expression in one place, while improving the exception handling resolution.

Adding logs sounds useful and is exactly the sort of benefit we get from this helper function.


HOWEVER, the mypy violation mentioned above is currently the only one in this file (i.e. raised for _extra but not for _password) - so perhaps it can/should be handled at the model level instead?

@vincbeck

Copy link
Copy Markdown
Contributor

Closing this as already fixed

@vincbeck vincbeck closed this Nov 27, 2025
@KoviAnusha
KoviAnusha deleted the mypy-1 branch November 27, 2025 17:31
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