Skip to content

Conversation

@Pei-Cheng-Yu
Copy link
Contributor

related #56735

fix mypy error in airflow-core/src/airflow/models/trigger.py


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

Lee-W
Lee-W previously approved these changes Nov 27, 2025

# Get the next kwargs of the task instance, or an empty dictionary if it doesn't exist
next_kwargs = task_instance.next_kwargs or {}
next_kwargs = cast("dict[str, Any]", task_instance.next_kwargs or {})
Copy link
Member

Choose a reason for hiding this comment

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

What is the error here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here, this error generated after running
prek --color always --verbose --hook-stage manual "mypy-airflow-core" --all-files following the instruction from the issue
image

Copy link
Member

Choose a reason for hiding this comment

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

Looks like there’s a potential bug; task_instance.next_kwargs can be a str; see discussion here #58728 (comment)

This shouldn’t be ignored with a cast.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the explanations above!

I just want to double-check the current situation:
From the discussion, it seems that task_instance.next_kwargs can be either a dict or a serialized/encrypted str, and the correct handling is still unclear.

Given this, should I put this PR aside for now and wait for a decision on how next_kwargs is expected to behave?
Thanks!

Copy link

@Tcc0403 Tcc0403 Nov 27, 2025

Choose a reason for hiding this comment

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

There is a similar case where next_kwargs is deserialized first if it's a str.

# This is slightly inefficient as we deserialize it to then right again serialize it in the sqla
# TypeAdapter.
next_kwargs = None
if ti_patch_payload.next_kwargs:
from airflow.serialization.serialized_objects import BaseSerialization
next_kwargs = BaseSerialization.deserialize(ti_patch_payload.next_kwargs)

Perhaps the solution would be something like this?

Suggested change
next_kwargs = cast("dict[str, Any]", task_instance.next_kwargs or {})
next_kwargs = task_instance.next_kwargs or {}
if isinstance(next_kwargs, str):
from airflow.serialization.serialized_objects import BaseSerialization
next_kwargs = BaseSerialization.deserialize(next_kwargs)

Copy link
Collaborator

Choose a reason for hiding this comment

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

See some additional suggestions in this discussion (incl. the collapsed session in my last comment).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestions from @Tcc0403 and @Dev-iL!
I’ve updated this PR to fix the mypy error without using a direct cast.

@Lee-W Lee-W dismissed their stale review November 27, 2025 07:30

potential error found

@vincbeck
Copy link
Contributor

Let's wait #58728 to be merged. If I am not mistaken, #58728 solves all the remaining mypy errors

@Pei-Cheng-Yu
Copy link
Contributor Author

No problem, thanks for the clarification!

@vincbeck
Copy link
Contributor

vincbeck commented Dec 1, 2025

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants