[red-knot] Add type inference for basic for loops#13195
Merged
AlexWaygood merged 5 commits intomainfrom Sep 4, 2024
Merged
Conversation
Contributor
|
b46aecf to
b513607
Compare
AlexWaygood
commented
Sep 1, 2024
a45317c to
6ab8617
Compare
MichaReiser
reviewed
Sep 2, 2024
9fdeb3e to
52941b9
Compare
Member
|
All changes look good to me but I prefer for @carljm to take a look |
52941b9 to
fae2d21
Compare
carljm
approved these changes
Sep 3, 2024
Contributor
carljm
left a comment
There was a problem hiding this comment.
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
fae2d21 to
a5fbf74
Compare
a5fbf74 to
1c68317
Compare
This file contains hidden or 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
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
forloop 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 largerints 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
fooin 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_classmethod that allows us to go from a type representing<instance of int>to a type representing<the int class itself>.Test Plan
cargo test