Repository navigation
Fix MyPy type errors in airflow/models(serialized_dag.py, connection.py) - #58062
KoviAnusha wants to merge 24 commits into
Conversation
| try: | ||
| as_bytes = bytes(fld, "utf-8") | ||
| except (TypeError, UnicodeEncodeError): | ||
| return fld |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
This is suggested here #58062 (comment). So I have implemented this.
There was a problem hiding this comment.
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
raiseI suggested the helper method for two reasons:
- We have the
fernet.decrypt(bytes(self.*, "utf-8")).decode()patterns in more than one place. except Exceptionis 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?
|
Closing this as already fixed |
related: #56735
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.rstor{issue_number}.significant.rst, in airflow-core/newsfragments.