Skip to content
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

[red-knot] add IntersectionBuilder #12791

Merged
merged 3 commits into from
Aug 12, 2024
Merged

[red-knot] add IntersectionBuilder #12791

merged 3 commits into from
Aug 12, 2024

Conversation

carljm
Copy link
Contributor

@carljm carljm commented Aug 9, 2024

For type narrowing, we'll need intersections (since applying type narrowing is just a type intersection.)

Add IntersectionBuilder, along with some tests for it and UnionBuilder (renamed from UnionTypeBuilder).

We use smart builders to ensure that we always keep these types in disjunctive normal form (DNF). That means that we never have deeply nested trees of unions and intersections: unions flatten into unions, intersections flatten into intersections, and intersections distribute over unions, so the most complex tree we can ever have is a union of intersections. We also never have a single-element union or a single-positive-element intersection; these both just simplify to the contained type.

Maintaining these invariants means that UnionBuilder doesn't necessarily end up building a Type::Union (e.g. if you only add a single type to the union, it'll just return that type instead), and IntersectionBuilder doesn't necessarily build a Type::Intersection (if you add a union to the intersection, we distribute the intersection over that union, and IntersectionBuilder will end up returning a Type::Union of intersections).

We also simplify intersections by ensuring that if a type and its negation are both in an intersection, they simplify out. (In future this should also respect subtyping, not just type identity, but we don't have subtyping yet.) We do implement subtyping of Never as a special case for now.

Most of this PR is unused for now until type narrowing lands; I'm just breaking it out to reduce the review fatigue of a single massive PR.

@carljm carljm added the red-knot Multi-file analysis & type inference label Aug 9, 2024
Copy link
Contributor

github-actions bot commented Aug 9, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

crates/red_knot_python_semantic/src/types.rs Outdated Show resolved Hide resolved
crates/red_knot_python_semantic/src/types.rs Outdated Show resolved Hide resolved
crates/red_knot_python_semantic/src/types.rs Outdated Show resolved Hide resolved
crates/red_knot_python_semantic/src/types.rs Outdated Show resolved Hide resolved
@AlexWaygood
Copy link
Member

(Have only skimmed so far; will look in more depth over the weekend or next week :-)

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

Overall this looks great. I love the builder interfaces.

crates/red_knot_python_semantic/src/types.rs Outdated Show resolved Hide resolved
crates/red_knot_python_semantic/src/types.rs Outdated Show resolved Hide resolved
crates/red_knot_python_semantic/src/types.rs Outdated Show resolved Hide resolved
crates/red_knot_python_semantic/src/types.rs Outdated Show resolved Hide resolved
crates/red_knot_python_semantic/src/types.rs Outdated Show resolved Hide resolved
crates/red_knot_python_semantic/src/types.rs Outdated Show resolved Hide resolved
@carljm
Copy link
Contributor Author

carljm commented Aug 11, 2024

Thanks for the great review!

Copy link

codspeed-hq bot commented Aug 11, 2024

CodSpeed Performance Report

Merging #12791 will not alter performance

Comparing cjm/union-intersection (106e2d8) with main (4b9ddc4)

Summary

✅ 32 untouched benchmarks

@AlexWaygood AlexWaygood changed the title [red-knot] add IntersectionTypeBuilder [red-knot] add IntersectionBuilder Aug 11, 2024
crates/red_knot_python_semantic/src/types.rs Outdated Show resolved Hide resolved
crates/red_knot_python_semantic/src/types.rs Outdated Show resolved Hide resolved
@carljm carljm merged commit 75131c6 into main Aug 12, 2024
20 checks passed
@carljm carljm deleted the cjm/union-intersection branch August 12, 2024 18:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
red-knot Multi-file analysis & type inference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants