You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Regression in determining return type of zip() on tuples #4226
Revealed type is 'typing.Iterator[Tuple[builtins.int*, builtins.str*]]
while mypy 0.780 will output:
Revealed type is 'Tuple[builtins.tuple[builtins.object*], builtins.tuple[builtins.object*], builtins.tuple[builtins.object*]]'
The output from 0.770 is both more correct (since the zip object is not a tuple) and more useful (since the types of the elements are known).
The fix for python/mypy#8454 adds additional overloaded definitions for zip(*...) in typeshed, which in itself is useful. However, for some reason mypy seems to prefer the new 'star' definitions to the older and more correct 'non-star' definitions, which causes this regression.
We should probably move this to typeshed, I think mypy is doing the right thing here. mypy will usually pick the first matching variant and complicating that logic too much will make mypy unpredictable in ways that are bad for users.
We could patch the situation by adding even more overloads (and removing a few of the longer heterogeneous overloads added in #3830). I think the following could actually work well in practice (it should make both the above and the original python/mypy#8454 work):
Reverting would fix my use case, but people using zip() on heterogeneous tuples would then have to add extra annotations or casts to their code.
Tweaking overlapping rules, as @hauntsaninja proposed above, might work for small inputs, but I think any star rule will be matching too much and forcing the types to object. And then there is also the point that those rules are technically lies that might hide bugs, for example the returned zip object does not support len(), while Tuple does.
Having more advanced rules for handling overlapping overload declarations is likely to indeed get unpredictable if it ends up being some kind of scoring system. And I can't think of anything simpler that still solves the issue.
A special case or plugin in mypy specifically for zip would work, but then someone would have to be willing to invest time into that.
The zip change also caused a performance regression in mypy (python/mypy#9016). Maybe reverting is the right call? The old behavior maybe caused fewer issues.
We reverted #3830 in #4254, so mthuurne's issue should be fixed. If more people report issues like python/mypy#8454, we could reconsider the overloads or maybe preferably write a mypy plugin.
Activity
hauntsaninja commentedon Jun 11, 2020
We should probably move this to typeshed, I think mypy is doing the right thing here. mypy will usually pick the first matching variant and complicating that logic too much will make mypy unpredictable in ways that are bad for users.
We could patch the situation by adding even more overloads (and removing a few of the longer heterogeneous overloads added in #3830). I think the following could actually work well in practice (it should make both the above and the original python/mypy#8454 work):
But given that the return type is a lie and that the original patch evidently caused issues I didn't foresee, maybe we should just revert #3830
Would appreciate opinions!
ilevkivskyi commentedon Jun 12, 2020
OK, I am moving this issue to typeshed now.
mthuurne commentedon Jun 13, 2020
I don't think there is an easy solution.
Reverting would fix my use case, but people using
zip()on heterogeneous tuples would then have to add extra annotations or casts to their code.Tweaking overlapping rules, as @hauntsaninja proposed above, might work for small inputs, but I think any star rule will be matching too much and forcing the types to
object. And then there is also the point that those rules are technically lies that might hide bugs, for example the returnedzipobject does not supportlen(), whileTupledoes.Having more advanced rules for handling overlapping overload declarations is likely to indeed get unpredictable if it ends up being some kind of scoring system. And I can't think of anything simpler that still solves the issue.
A special case or plugin in mypy specifically for
zipwould work, but then someone would have to be willing to invest time into that.JelleZijlstra commentedon Jun 21, 2020
The zip change also caused a performance regression in mypy (python/mypy#9016). Maybe reverting is the right call? The old behavior maybe caused fewer issues.
staticdev commentedon Jun 25, 2020
I also got a problem with that and opened a question on SO.
hauntsaninja commentedon Jun 26, 2020
staticdev's issue is unrelated, see python/mypy#9048
We reverted #3830 in #4254, so mthuurne's issue should be fixed. If more people report issues like python/mypy#8454, we could reconsider the overloads or maybe preferably write a mypy plugin.