Skip to content

Inconsistent behavior depending on cache -- dict.get returning Union of key and value types #6509

Closed
@pdeblanc

Description

@pdeblanc

Python 3.7.2, mypy 0.670 (I also tried 70565cd, which was the most recent commit on master at the time of my testing).

File foo.py:

from typing import Dict, Generic, TypeVar

Key = TypeVar('Key')

class A(Generic[Key]):
    @staticmethod
    def lookup(d: Dict[Key, int], key: Key) -> None:
        x = d.get(key, 0) + 1

x = 1 + "a"

The first time running mypy foo.py produces the expected output:

foo.py:10: error: Unsupported operand types for + ("int" and "str")

Now running mypy foo.py a second time produces a spurious error:

foo.py:8: error: Unsupported operand types for + ("Key" and "int")
foo.py:8: note: Left operand is of type "Union[int, Key]"
foo.py:10: error: Unsupported operand types for + ("int" and "str")

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions