8000 Error when assigning a ternary with an empty collection to a union type variable · Issue #7780 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content
Error when assigning a ternary with an empty collection to a union type variable #7780
Closed
@raphj

Description

@raphj

Hello,

the following code:

from typing import List, Union
import sys
p : Union[int, List[int]] = 1 if len(sys.argv) else []

incorrectly generates the following error:

t.py:3: error: Incompatible types in assignment (expression has type "Union[int, List[<nothing>]]", variable has type "Union[int, List[int]]")

Adding an integer in the list makes the error disappear. I first observed this issue with a dictionary:

from typing import Dict, Union
import sys
p : Union[int, Dict[int, int]] = 1 if len(sys.argv) else {}

Error:

t.py:3: error: Incompatible types in assignment (expression has type "Union[int, Dict[<nothing>, <nothing>]]", variable has type "Union[int, Dict[int, int]]")

It seems <nothing> should be matched with any type in type parameters in unions.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0