[Python] mypy: Collection[str] replaces nested structure typing?

Discussão em 'Python' iniciado por Stack, Julho 13, 2025.

  1. Stack

    Stack Membro Participativo

    I have a data variable that, if typed properly, should look like this:

    data: List[Dict[str, Union[str, Dict[str, List[Dict[str, str]]]]]] = []


    But when I try to append to this list with the following code:

    info = {"spaghetti": [{"value": "foo", "test": "bar"}]}
    my_data = {
    "Name": name,
    "_info": info,
    }
    data.append(my_data)


    Mypy tells me:

    Argument 1 to "append" of "list" has incompatible type "Dict[str, Collection[str]]"; expected "Dict[str, Union[str, Dict[str, List[Dict[str, str]]]]]"


    Now, how is that possible that Union[str, Dict[str, List[Dict[str, str]]]] is equivalent to a Collection[str] ?

    Continue reading...

Compartilhe esta Página