Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
zzzeek opened this issue Apr 17, 2022 · 5 comments · Fixed by #12627
Closed

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

zzzeek opened this issue Apr 17, 2022 · 5 comments · Fixed by #12627
Labels
bug mypy got something wrong topic-tests

Comments

@zzzeek
Copy link

zzzeek commented Apr 17, 2022

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

@hauntsaninja
Copy link
Collaborator

Thanks! This bisects to the recent typeshed sync: #12321

Ran mypy_primer -p test.py --bisect --new origin/master --old v0.942 --debug

@hauntsaninja
Copy link
Collaborator

Looks like the following diff "fixes" it:

diff --git a/mypy/typeshed/stdlib/builtins.pyi b/mypy/typeshed/stdlib/builtins.pyi
index 9c5dfcfef..870ed693d 100644
--- a/mypy/typeshed/stdlib/builtins.pyi
+++ b/mypy/typeshed/stdlib/builtins.pyi
@@ -995,7 +995,7 @@ class frozenset(AbstractSet[_T_co], Generic[_T_co]):
 
 class enumerate(Iterator[tuple[int, _T]], Generic[_T]):
     def __init__(self, iterable: Iterable[_T], start: int = ...) -> None: ...
-    def __iter__(self: Self) -> Self: ...
+    def __iter__(self) -> Iterator[tuple[int, _T]]: ...
     def __next__(self) -> tuple[int, _T]: ...
     if sys.version_info >= (3, 9):
         def __class_getitem__(cls, __item: Any) -> GenericAlias: ...

@hauntsaninja
Copy link
Collaborator

Looks like I'm treading ground already covered in #12588

@AlexWaygood
Copy link
Member

This was fixed by #12590, so I'm closing this (cc. @JukkaL)

@JelleZijlstra
Copy link
Member

Could we add a test case specifically for @zzzeek's example? #12590 is pretty remote from its effect, and it would be good to get new regression tests for each bug we find.

@JelleZijlstra JelleZijlstra reopened this Apr 19, 2022
@JelleZijlstra JelleZijlstra changed the title regression w/ mypy mis-interpreting generator comprehension add test case: regression w/ mypy mis-interpreting generator comprehension Apr 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-tests
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants