-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[red-knot] Add type inference for basic for
loops
#13195
Merged
Merged
+331
−131
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
AlexWaygood
force-pushed
the
alex/for-loop-types
branch
from
September 1, 2024 16:36
b46aecf
to
b513607
Compare
AlexWaygood
commented
Sep 1, 2024
AlexWaygood
force-pushed
the
alex/for-loop-types
branch
from
September 1, 2024 19:49
a45317c
to
6ab8617
Compare
MichaReiser
reviewed
Sep 2, 2024
AlexWaygood
force-pushed
the
alex/for-loop-types
branch
2 times, most recently
from
September 2, 2024 13:21
9fdeb3e
to
52941b9
Compare
All changes look good to me but I prefer for @carljm to take a look |
AlexWaygood
force-pushed
the
alex/for-loop-types
branch
from
September 3, 2024 11:31
52941b9
to
fae2d21
Compare
carljm
approved these changes
Sep 3, 2024
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.
This is great!! So neat to see things coming together such that we can now do real inference based on dunder protocols :)
dhruvmanila
reviewed
Sep 4, 2024
AlexWaygood
force-pushed
the
alex/for-loop-types
branch
from
September 4, 2024 10:12
fae2d21
to
a5fbf74
Compare
AlexWaygood
force-pushed
the
alex/for-loop-types
branch
from
September 4, 2024 10:14
a5fbf74
to
1c68317
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds type inference for basic
for
loop variables to red-knot.Inferring the type of a loop var involves either the new-style iteration protocol (which invokes
__iter__
to get an iterator, and then__next__
on that iterator), or the old-style iteration protocol (which passes incrementally largerint
s to a__getitem__
method). If__iter__
is defined on the class,__iter__
always takes precedence, even if__iter__
is e.g. set toNone
: this is a technique that can be used to make classes that define__getitem__
not-iterable.Whichever protocol is used to make a class's instances iterable, the dunder methods are always looked up on the class of an instance rather than the instance itself. I.e., the interpreter won't consider
foo
in this snippet to be iterable, because__iter__
only exists on the instance, not the class:To emulate this runtime behaviour, I added a
Type::to_type_of_class
method that allows us to go from a type representing<instance of int>
to a type representing<the int class itself>
.Test Plan
cargo test