-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[red-knot] Binary operator inference for union types #16601
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
Conversation
|
This comment was marked as resolved.
This comment was marked as resolved.
0303719 to
2273374
Compare
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
## Summary Strip ANSI codes in the mypy_primer diff before uploading. ## Test Plan Successful run here: #16601
2273374 to
86933b9
Compare
This comment was marked as resolved.
This comment was marked as resolved.
## Summary Strip ANSI codes in the mypy_primer diff before uploading. ## Test Plan Successful run here: #16601
86933b9 to
e06ac75
Compare
e06ac75 to
760bbd4
Compare
| (Type::Union(lhs_union), rhs, _) => { | ||
| let mut union = UnionBuilder::new(self.db()); | ||
| for lhs in lhs_union.elements(self.db()) { | ||
| let result = self.infer_binary_expression_type(*lhs, rhs, op)?; |
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.
Instead of short-circuiting here, we could probably also build a better diagnostic, telling you which exact element-combination didn't work. But it seems low priority and doesn't necessarily have to be included here, I think.
carljm
left a comment
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.
Thank you!
Summary
This fixes a bug I noticed while looking at ecosystem results. The MRE version of it is this:
It's yet another instance of the general problem that our way of representing union types does not prevent us from handling unions (and intersections) incorrectly. The pattern is usually this: we implement operation X by explicitly matching on some cases that we handle explicitly (e.g. literals). For all other types, we fall back to some generic behavior (calling some functions that work for every
Type). That generic behavior also compiles forType::Union/Type::Intersection, but what we really want is to recursively call X for each element, which also gives us that special behavior for elements of the union.For intersections, I'll open a ticket.
Test Plan