Skip to content

Type annotation for @task errors when called with parentheses/arguments #32384

@benbuckman

Description

@benbuckman

Apache Airflow version

2.6.2

What happened

👋 If I define a task with @task (via from airflow.decorators import task) then mypy (type checker) is happy. If I add add parentheses to add arguments, like @task() or @task(queue='foo'), then mypy complains,

Untyped decorator makes function [name] untyped.

I see in airflow src, there are example DAGs with @task() (using parentheses). So I also wonder how this works internally.

Thank you!

What you think should happen instead

Mypy should understand any of,

@task
def foo():
    ...
@task()
def foo():
    ...
@task(queue='secondary')
def foo():
    ...

How to reproduce

  • Python 3.10.8
  • Airflow 2.6.2
  • Mypy 1.0.0 (as defined by constraints file)
    • (Note: upgrading mypy to the latest 1.4.1 results in the same behavior.)

Place somewhere in <dags_folder>:

from datetime import datetime
from airflow.decorators import dag, task


@task
def task_without_parens_is_fine() -> None:
    ...


@task()
# ^ type error on this line: "Untyped decorator makes function "demo_type_error" untyped"
def demo_type_error() -> None:
    ...


@dag(schedule=None, start_date=datetime(2021, 1, 1), catchup=False)
def demo_dag():
    demo_type_error()


demo_dag()

Run mypy --strict . in the parent or application-root folder.
(edit: I initially omitted --strict from this snippet, but it's required to repro.)

Operating System

MacOS Ventura 13.4

Versions of Apache Airflow Providers

apache-airflow-providers-common-sql==1.5.1
apache-airflow-providers-ftp==3.4.1
apache-airflow-providers-google==10.1.1
apache-airflow-providers-http==4.4.1
apache-airflow-providers-imap==3.2.1
apache-airflow-providers-postgres==5.5.0
apache-airflow-providers-sendgrid==3.2.0
apache-airflow-providers-slack==7.3.0
apache-airflow-providers-sqlite==3.4.1

Deployment

Official Apache Airflow Helm Chart

Deployment details

The deployment is not relevant to this issue, as this can be reproduced locally in a mypy-enabled IDE or in a terminal running mypy <dir>.

Anything else

I tried to copy/use the dev/mypy/plugins/decorators.py mypy plugin in my application, but this did not help. (And anyway that file is not exported in the public apache-airflow package.)

Airflow Slack thread about this: https://apache-airflow.slack.com/archives/CCPRP7943/p1688571561287409

Tzu-ping Chung:
Sounds like our type annotation to @task is inaccurate. File an issue with a repro and I can try to fix that.

Are you willing to submit PR?

  • Yes I am willing to submit a PR!

Code of Conduct

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions