Skip to content

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

Description

@zzzeek

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

fromtypingimportDictfromtypingimportGenericfromtypingimportListfromtypingimportTuplefromtypingimportTypeVarfromtypingimportUnionclassThing:
pass_THING=TypeVar("_THING", bound="Thing")
classThingCollection(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= {}
defdo_thing(self) ->None:
self._index.update(
(idx, c) foridx, (k, c) inenumerate(self._collection)
)
defdo_thing_workaround(self) ->None:
self._index.update(
(idx, c) foridx, cinenumerate(cfor (k, c) inself._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