Skip to content

add test case: regression w/ mypy mis-interpreting generator comprehension #12610

Closed
@zzzeek

Description

@zzzeek

this seems to be a regression which I am getting with mypy master @ 0df8cf5, havent bisected to find the exact commit:

from typing import Dict
from typing import Generic
from typing import List
from typing import Tuple
from typing import TypeVar
from typing import Union


class Thing:
    pass


_THING = TypeVar("_THING", bound="Thing")


class ThingCollection(Generic[_THING]):
    _collection: List[Tuple[str, _THING]]

    _index: Dict[Union[None, str, int], _THING]

    def __init__(self, t: _THING):
        self._collection = [("x", t)]
        self._index = {}

    def do_thing(self) -> None:
        self._index.update(
            (idx, c) for idx, (k, c) in enumerate(self._collection)
        )

    def do_thing_workaround(self) -> None:
        self._index.update(
            (idx, c) for idx, c in enumerate(c for (k, c) in self._collection)
        )

output:

$ mypy test3.py 
test3.py:27: error: Generator has incompatible item type "Tuple[int, Tuple[str, _THING]]"; expected "Tuple[Union[None, str, int], _THING]"  [misc]
Found 1 error in 1 file (checked 1 source file)

mypy seems to be mis-interpreting the first generator. works in mypy 0.942 and pyright

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions