-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Fix Any
inference when unpacking iterators that don't directly inherit from typing.Iterator
#14821
Conversation
…on't directly inherit from `typing.Iterator`
Any
inference when unpacking iterators that return self
and don't directly inherit from typing.Iterator
Any
inference when unpacking iterators that don't directly inherit from typing.Iterator
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice to see this ad-hoc code removed!
@@ -380,6 +380,8 @@ class Nums(Iterable[int]): | |||
def __iter__(self): pass | |||
def __next__(self): pass | |||
a, b = Nums() | |||
reveal_type(a) # N: Revealed type is "builtins.int" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The other option here is to infer Any
, because of def __iter__(self): pass
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, but as discussed in the issue, two long-standing tests fail if I make that change, so I'd rather not make that behaviour change in this PR 👍
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Fixes #14819.
Mypy currently silently infers an
Any
type when unpacking an iterator that doesn't explicitly inherit fromtyping.Iterator
(i.e., an iterator that's a structural subtype oftyping.Iterator
, but not a nominal subtype):However, we have enough information here to infer that the type of
a
should really beint
. This PR fixes that bug.There's discussion on the issue thread about an alternative solution that would involve changing some mypy behaviour that's been established for around 10 years. For now, I haven't gone for that solution.