Skip to content

Cannot use TypedDict object when defining params #29358

Closed

Description

@jcobb-healx

Apache Airflow version

2.5.1

What happened

Context: I am attempting to use TypedDict objects to maintain the keys used in DAG params in a single place, and check for key names across multiple DAGs that use the params.
This raises an error with mypy as params expects an Optional[Dict]. Due to the invariance of Dict, this does not accept TypedDict objects.

What happened: I passed a TypedDict to the params arg of DAG and got a TypeError.

What you think should happen instead

TypedDict objects should be accepted by DAG, which should accept Optional[Mapping[str, Any]].

Unless I'm mistaken, params are converted to a ParamsDict class and therefore the appropriate type hint is a generic Mapping type.

How to reproduce

Steps to reproduce

from typing import TypedDict

from airflow import DAG
from airflow.models import Param

class ParamsTypedDict(TypedDict):
    str_param: Param

params: ParamsTypedDict = {
    "str_param": Param("", type="str")
}

with DAG(
    dag_id="mypy-error-dag",
    # The line below raises a mypy error
    # Argument "params" to "DAG" has incompatible type "ParamsTypedDict"; expected "Optional[Dict[Any, Any]]"  [arg-type]
    params=params,  
) as dag:
    pass

Operating System

Amazon Linux

Versions of Apache Airflow Providers

No response

Deployment

Docker-Compose

Deployment details

No response

Anything else

No response

Are you willing to submit PR?

  • Yes I am willing to submit a PR!

Code of Conduct

Activity

  1. boring-cyborg commented on Feb 3, 2023

    @boring-cyborg

    Thanks for opening your first issue here! Be sure to follow the issue template!

  2. jcobb-healx commented on Feb 3, 2023

    @jcobb-healx
    ContributorAuthor

    Relevant mypy discussion on passing TypedDict as args annotated with Dict: python/mypy#4976

  3. potiuk commented on Feb 3, 2023

    @potiuk
    Member

    Feel free to work on it.

  4. pierrejeambrun commented on Feb 25, 2023

    @pierrejeambrun
    Member

    @jcobb-healx Are you still working on this ? Don't hesitate to reach out if you need some help :)

  5. jcobb-healx commented on Feb 27, 2023

    @jcobb-healx
    ContributorAuthor

    @pierrejeambrun Sorry, I hadn't gotten around to looking at this. Spent a bit of time on this today, and it looks like converting the type hint to Mapping isn't right, as the params are merged with the default params in an update call, which isn't consistent with TypedDict behaviour.
    Given the earlier mypy discussion, it probably makes sense to use MutableMapping to allow generic inputs, and let users perform casts, i.e. not accept TypedDict objects.

  6. jcobb-healx commented on Feb 27, 2023

    @jcobb-healx
    ContributorAuthor

    @pierrejeambrun I've made the changes to use MutableMapping so that passing in a ParamsDict object works as expected. I've not changed anything that would allow my original case to work, so I've made no mention of TypedDict in the PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions