Skip to content

[red-knot] Type inference for comparisons involving intersection types#14138

Merged
sharkdp merged 2 commits intomainfrom
david/intersection-types-compare-expressions
Nov 7, 2024
Merged

[red-knot] Type inference for comparisons involving intersection types#14138
sharkdp merged 2 commits intomainfrom
david/intersection-types-compare-expressions

Conversation

@sharkdp
Copy link
Contributor

@sharkdp sharkdp commented Nov 6, 2024

Summary

This adds type inference for comparison expressions involving intersection types.

closes #13854

Eye candy

x = get_random_int()

if x != 42:
    reveal_type(x == 42)  # revealed: Literal[False]
    reveal_type(x == 43)  # bool

Test Plan

New Markdown-based tests.

@sharkdp sharkdp force-pushed the david/intersection-types-compare-expressions branch from c5b3c7f to 4039492 Compare November 6, 2024 20:26
@github-actions
Copy link
Contributor

github-actions bot commented Nov 6, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@sharkdp sharkdp force-pushed the david/intersection-types-compare-expressions branch 5 times, most recently from 6d3b87b to 9a62e8d Compare November 6, 2024 21:36
@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label Nov 6, 2024
@sharkdp sharkdp force-pushed the david/intersection-types-compare-expressions branch from 6927577 to 955d321 Compare November 7, 2024 09:26
@sharkdp sharkdp force-pushed the david/intersection-types-compare-expressions branch from 955d321 to 55b2b93 Compare November 7, 2024 17:56
@sharkdp sharkdp force-pushed the david/intersection-types-compare-expressions branch from 55b2b93 to a44c08e Compare November 7, 2024 17:58
@sharkdp sharkdp marked this pull request as ready for review November 7, 2024 18:01
Copy link
Contributor

@carljm carljm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great write-up! Very clear.

Co-authored-by: Carl Meyer <carl@astral.sh>
@sharkdp sharkdp merged commit 57ba25c into main Nov 7, 2024
@sharkdp sharkdp deleted the david/intersection-types-compare-expressions branch November 7, 2024 19:51
sharkdp added a commit that referenced this pull request Nov 7, 2024
## Summary

Minor fix in intersection type comment introduced in #14138
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[red-knot] Compare expression inference - Intersection

4 participants