-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
[1.14 regression] Unpacking an iterator converts the inner type to Any
#18320
Comments
Any
Any
Bisects to this typeshed sync: #18057 |
Looks like this is python/typeshed#12851 ? cc @tungol That change probably makes type checkers a little slower too |
I'll take a look |
Using the reproduction above, I stepped through. The problem comes when we get into Stack starting at the reveal expression:
Where Up the stack a couple places, at So we reach The iterable builtins are the most prominent and most important, but I'd expect that this issue would come up for any iterator type that doesn't explicitly inherit from |
I can make a point release to fix this since it seems pretty bad. If it's easier/faster we could just revert the typeshed change (python/typeshed#12851 -- but maybe the others mentioned in the description have similar bugs?) in mypy for 1.14.1 while we make a proper fix for the 1.15 release. |
Yes, there's a similar bug for the others mentioned in the description, along with anything that has an import itertools
from typing import reveal_type
x = [1, 2]
y = [3, 4]
reveal_type([*enumerate(x)])
reveal_type([*filter(lambda x: True, x)])
reveal_type([*map(str, x)])
reveal_type([*reversed(x)])
reveal_type([*zip(x, y)])
reveal_type([*itertools.cycle(x)])
reveal_type([*itertools.permutations(x)]) 1.13.0 says:
1.14.0 says:
This code reproduces the issue on any version of mypy, not just 1.14.0: from typing import reveal_type, Iterable, Iterator, TypeVar, Generic
_T = TypeVar("_T")
class Iter1(Iterable[_T]):
def __init__(self, value: list[_T]) -> None:
self.value = value
def __iter__(self) -> Iterator[_T]:
return iter(self.value)
class Iter2(Generic[_T]):
def __init__(self, value: list[_T]) -> None:
self.value = value
def __iter__(self) -> Iterator[_T]:
return iter(self.value)
x = [1, 2]
reveal_type([*Iter1(x)]) # Revealed type is "builtins.list[builtins.int]"
reveal_type([*Iter2(x)]) # Revealed type is "builtins.list[Any]" If there's an equivalent of Reverting the typeshed change(s) as a quick fix probably makes sense. I'd probably go with at least the builtins and the itertools, but at the high end, really covering everything would mean making sure that everything in typeshed with an |
I looked at everything in
|
Unfortunately, there is no such thing (commenting since I wanted something like this myself couple times), so I would suggest to aim for a revert. (Implementing such a helper is possible, but not super easy, so I don't want to do it in a hurry.) |
Bug Report
Unpacking an iterator into a list produces a
list[Any]
instead of the more specific type. This worked correctly in mypy 1.13.0 but no longer works in 1.14.0.To Reproduce
Expected Behavior
In mypy 1.13.0:
Actual Behavior
In mypy 1.14.0:
Your Environment
mypy.ini
(and other config files): noneThe text was updated successfully, but these errors were encountered: