-
Notifications
You must be signed in to change notification settings - Fork 147
Bug Fix 708 - Union Iterability Checker #710
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
base: main
Are you sure you want to change the base?
Conversation
Bug Fix 708 - Iterability Checker
pyrefly/lib/alt/solve.rs
Outdated
@@ -584,7 +584,18 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> { | |||
} | |||
Type::Union(ts) => ts | |||
.iter() | |||
.flat_map(|t| self.iterate(t, range, errors)) | |||
.filter_map(|t| { | |||
let is_iterable = self.unwrap_iterable(t).is_some() |
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.
can we add is_iterable be added to types.rs and invoked from there instead?
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.
Moving the function out is a good idea, but I think placing the function types.rs won't work since pyrefly_types is in a different crate. From the way it's defined right now we'd end up with circular dependencies?
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.
I think that's correct, it needs access to self
.
Let's just pull it into a helper method?
We probably should make an AnswersSolver
module for "type utilities" that require access to a solver and therefore cannot be in types.rs
at some point.
Merging upstream Main
This looks great, thanks for the contribution! I think it would be nice to extract the helper method, after that I could work on getting it merged. |
Merging Upstrem Main
Done! Let me know if there's any other changes needed. |
LGTM, I'll aim to get it merged soon |
I think this is wrong, as given the example:
Before this diff we get an error |
Oh good point - we probably need something like this logic, but only used in match. The idea is that if we see a tuple form in a match, we should take all the iterable cases of a union as potential hits (so if we have It might actually need to be done as a narrowing operation cc @yangdanny97 who has the most context on matching |
@stroxler We do have narrowing that checks sequence length, but it doesn't exclude types that can't be sequences. I don't think we can modify it directly because it's used for stuff like |
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.
@Tejas-Ketkar there was a bit of back and forth discussion on this PR earlier, but it sounds like the next step is to update the PR it s.t. the checker only works when pattern matching.
Thanks, I'll look into making the fix. |
This PR tries to solve bug #708. The problem with the current implementation was that given a union type, solve.rs would iterate over each of the two types without considering whether both types were iterable. The fix tries to check if each option in the union is iterable, checking for explicit
__iter__
or seeing if it behaves like a tuple or is a tuple. The original bug was added as a test to pattern_match.rs.