Skip to content

Validate operator arg types at decoration time in @task decorator#65041

Merged
kaxil merged 1 commit intoapache:mainfrom
astronomer:worktree-foamy-dazzling-boole
Apr 11, 2026
Merged

Validate operator arg types at decoration time in @task decorator#65041
kaxil merged 1 commit intoapache:mainfrom
astronomer:worktree-foamy-dazzling-boole

Conversation

@kaxil
Copy link
Copy Markdown
Member

@kaxil kaxil commented Apr 11, 2026

Fixes #65028

Passing wrong types to @task kwargs (e.g. @task(task_id="fetch_{}".format) or @task(retries="three")) was silently accepted at decoration time. Type errors only surfaced later when the task was invoked inside a DAG context via validate_key() in BaseOperator.__init__, or not at all if the task was never called in a DAG.

Now validates all kwargs against BASEOPERATOR_ARGS_EXPECTED_TYPES in _TaskDecorator.__attrs_post_init__, catching type mismatches immediately when the decorator is applied.

Design

The validation loop runs after setdefault populates the default task_id from the function name (always a string), so only explicitly passed non-string values trigger it. None values are skipped (matching the existing validate_instance_args behavior).

All @task.* variants and .override(task_id=...) go through _TaskDecorator / attr.evolve() which re-runs __attrs_post_init__, so a single check covers all paths.

Reuses the existing BASEOPERATOR_ARGS_EXPECTED_TYPES dict rather than duplicating type definitions.

Previously, passing wrong types to @task kwargs (e.g.
`@task(task_id="fetch_{}".format)` or `@task(retries="three")`)
was silently accepted at decoration time. Type errors only surfaced
later when the task was invoked inside a DAG context, or not at all
if the task was never used.

Now validates all kwargs against BASEOPERATOR_ARGS_EXPECTED_TYPES
in _TaskDecorator.__attrs_post_init__, catching type mismatches
immediately at decoration time.

Closes apache#65028
@kaxil kaxil added this to the Airflow 3.2.1 milestone Apr 11, 2026
@kaxil kaxil requested a review from RNHTTR April 11, 2026 01:11
@kaxil kaxil requested review from amoghrajesh and ashb as code owners April 11, 2026 01:11
@eladkal eladkal added type:bug-fix Changelog: Bug Fixes backport-to-v3-2-test Mark PR with this label to backport to v3-2-test branch labels Apr 11, 2026
@kaxil kaxil merged commit 7d3364b into apache:main Apr 11, 2026
112 checks passed
@kaxil kaxil deleted the worktree-foamy-dazzling-boole branch April 11, 2026 10:57
github-actions bot pushed a commit that referenced this pull request Apr 11, 2026
…ecorator (#65041)

(cherry picked from commit 7d3364b)

Co-authored-by: Kaxil Naik <kaxilnaik@gmail.com>
@github-actions
Copy link
Copy Markdown

Backport successfully created: v3-2-test

Note: As of Merging PRs targeted for Airflow 3.X
the committer who merges the PR is responsible for backporting the PRs that are bug fixes (generally speaking) to the maintenance branches.

In matter of doubt please ask in #release-management Slack channel.

Status Branch Result
v3-2-test PR Link

eladkal pushed a commit that referenced this pull request Apr 13, 2026
…ecorator (#65041) (#65050)

(cherry picked from commit 7d3364b)

Co-authored-by: Kaxil Naik <kaxilnaik@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:task-sdk backport-to-v3-2-test Mark PR with this label to backport to v3-2-test branch type:bug-fix Changelog: Bug Fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

broken string format in task decorator definition doesn't properly result in a dag import error

2 participants