[red-knot] Type inference for comparisons involving intersection types#14138
Merged
[red-knot] Type inference for comparisons involving intersection types#14138
Conversation
c5b3c7f to
4039492
Compare
Contributor
|
6d3b87b to
9a62e8d
Compare
T-256
reviewed
Nov 7, 2024
crates/red_knot_python_semantic/resources/mdtest/comparison/intersections.md
Outdated
Show resolved
Hide resolved
6927577 to
955d321
Compare
955d321 to
55b2b93
Compare
55b2b93 to
a44c08e
Compare
carljm
approved these changes
Nov 7, 2024
Comment on lines
+3198
to
+3221
| // If non of the simplifications above apply, we still need to return *some* | ||
| // result type for the comparison 'T_inter `op` T_other' (or reversed), where | ||
| // | ||
| // T_inter = P1 & P2 & ... & Pn & ~N1 & ~N2 & ... & ~Nm | ||
| // | ||
| // is the intersection type. If f(T) is the function that computes the result | ||
| // type of a `op`-comparison with `T_other`, we are interested in f(T_inter). | ||
| // Since we can't compute it exactly, we return the following approximation: | ||
| // | ||
| // f(T_inter) = f(P1) & f(P2) & ... & f(Pn) | ||
| // | ||
| // The reason for this is the following: In general, for any function 'f', the | ||
| // set f(A) & f(B) can be *larger than* the set f(A & B). This means that we | ||
| // will return a type that is too wide, which is not necessarily problematic. | ||
| // | ||
| // However, we do have to leave out the negative contributions. If we were to | ||
| // add a contribution like ~f(N1), we would potentially infer result types | ||
| // that are too narrow, since ~f(A) can be larger than f(~A). | ||
| // | ||
| // As an example for this, consider the intersection type `int & ~Literal[1]`. | ||
| // If 'f' would be the `==`-comparison with 2, we obviously can't tell if that | ||
| // answer would be true or false, so we need to return `bool`. However, if we | ||
| // compute f(int) & ~f(Literal[1]), we get `bool & ~Literal[False]`, which can | ||
| // be simplified to `Literal[True]` -- a type that is too narrow. |
sharkdp
added a commit
that referenced
this pull request
Nov 7, 2024
## Summary Minor fix in intersection type comment introduced in #14138
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 adds type inference for comparison expressions involving intersection types.
closes #13854
Eye candy
Test Plan
New Markdown-based tests.