Skip to content

Comments

[red-knot] Simplify object out of intersections#15511

Merged
AlexWaygood merged 2 commits intomainfrom
alex/object-intersections
Jan 15, 2025
Merged

[red-knot] Simplify object out of intersections#15511
AlexWaygood merged 2 commits intomainfrom
alex/object-intersections

Conversation

@AlexWaygood
Copy link
Member

Summary

object & int is equivalent to int, object & Unknown is equivalent to Unknown, object & ~int is equivalent to ~int... I could go on ;)

object is the top type, the supertype of everything, so it is always redundant in an intersection. Eagerly simplifying it out of intersections is easy, and fixes some property-test failures in another branch of mine.

Test Plan

  • `cargo test -p red_knot_python_semantic --test mdtest
  • QUICKCHECK_TESTS=200000 cargo test --release -p red_knot_python_semantic -- --ignored types::property_tests::stable
  • uvx pre-commit run -a

@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label Jan 15, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Jan 15, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@sharkdp
Copy link
Contributor

sharkdp commented Jan 15, 2025

I recently wrote this down (in an unpublished branch):

object describes the set of all possible values, while Never describes the empty set. The two types are complements of each other:

from knot_extensions import static_assert, is_equivalent_to, Not
from typing_extensions import Never

static_assert(is_equivalent_to(Not[object], Never))
static_assert(is_equivalent_to(Not[Never], object))

This duality is also reflected in other facts:

  • Never is a subtype of every type, while object is a supertype of every type.
  • Never is assignable to every type, while object is assignable from every type.
  • Never is disjoint from every type, while object overlaps with every type.
  • Building a union with Never is a no-op, intersecting with object is a no-op.
  • Interecting with Never results in Never, building a union with object results in object.

Thanks for explicitly implementing the "intersecting with object is a no-op" part of this 👍

Copy link
Contributor

@sharkdp sharkdp left a comment

Choose a reason for hiding this comment

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

Thank you!

@AlexWaygood AlexWaygood enabled auto-merge (squash) January 15, 2025 20:02
@AlexWaygood AlexWaygood merged commit 49557a9 into main Jan 15, 2025
20 checks passed
@AlexWaygood AlexWaygood deleted the alex/object-intersections branch January 15, 2025 20:06
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.

3 participants