Skip to content

Commit

Permalink
final fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood committed Jan 18, 2025
1 parent 14cd669 commit 6496d9a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,11 @@ from knot_extensions import Intersection, Not, Unknown, is_gradual_equivalent_to

static_assert(is_gradual_equivalent_to(str | int, str | int))
static_assert(is_gradual_equivalent_to(str | int | Any, str | int | Unknown))

# TODO: These should pass
static_assert(is_gradual_equivalent_to(str | int, int | str)) # error: [static-assert-error]
# error: [static-assert-error]
static_assert(is_gradual_equivalent_to(str | int, int | str))
static_assert(
is_gradual_equivalent_to(Intersection[str, int, Not[bytes], Not[None]], Intersection[int, str, Not[None], Not[bytes]])
)
# TODO: `~type[Any]` shoudld be gradually equivalent to `~type[Unknown]`
# error: [static-assert-error]
static_assert(is_gradual_equivalent_to(Intersection[str | int, Not[type[Any]]], Intersection[int | str, Not[type[Unknown]]]))

Expand Down
12 changes: 10 additions & 2 deletions crates/red_knot_python_semantic/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1087,10 +1087,10 @@ impl<'db> Type<'db> {

match (self, other) {
(Type::Union(left), Type::Union(right)) => {
left.to_sorted_union(db) == right.to_sorted_union(db)
left == right || left.to_sorted_union(db) == right.to_sorted_union(db)
}
(Type::Intersection(left), Type::Intersection(right)) => {
left.to_sorted_intersection(db) == right.to_sorted_intersection(db)
left == right || left.to_sorted_intersection(db) == right.to_sorted_intersection(db)
}
_ => self == other,
}
Expand Down Expand Up @@ -1152,6 +1152,10 @@ impl<'db> Type<'db> {
}

(Type::Union(first), Type::Union(second)) => {
if first == second {
return true;
}

let first = first.to_sorted_union(db);
let second = second.to_sorted_union(db);

Expand All @@ -1169,6 +1173,10 @@ impl<'db> Type<'db> {
}

(Type::Intersection(first), Type::Intersection(second)) => {
if first == second {
return true;
}

let first = first.to_sorted_intersection(db);
let second = second.to_sorted_intersection(db);

Expand Down
14 changes: 7 additions & 7 deletions crates/red_knot_python_semantic/src/types/property_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,12 @@ mod stable {
forall types s, t, u. s.is_equivalent_to(db, t) && t.is_equivalent_to(db, u) => s.is_equivalent_to(db, u)
);

// Symmetry: If `S` is gradual equivalent to `T`, `T` is gradual equivalent to `S`.
type_property_test!(
gradual_equivalent_to_is_symmetric, db,
forall types s, t. s.is_gradual_equivalent_to(db, t) => t.is_gradual_equivalent_to(db, s)
);

// A fully static type `T` is a subtype of itself.
type_property_test!(
subtype_of_is_reflexive, db,
Expand Down Expand Up @@ -408,7 +414,7 @@ mod flaky {
);

// Equal element sets of intersections implies equivalence
// flaky at least in part because of https://github.com/astral-sh/ruff/issues/15508
// flaky at least in part because of https://github.com/astral-sh/ruff/issues/15513
type_property_test!(
intersection_equivalence_not_order_dependent, db,
forall types s, t, u.
Expand All @@ -435,12 +441,6 @@ mod flaky {
.all(|vec_of_unions| vec_of_unions[0].is_equivalent_to(db, vec_of_unions[1]))
);

// Symmetry: If `S` is gradual equivalent to `T`, `T` is gradual equivalent to `S`.
type_property_test!(
gradual_equivalent_to_is_symmetric, db,
forall types s, t. s.is_gradual_equivalent_to(db, t) => t.is_gradual_equivalent_to(db, s)
);

// A fully static type does not have any materializations.
// Thus, two equivalent (fully static) types are also gradual equivalent.
type_property_test!(
Expand Down
2 changes: 1 addition & 1 deletion crates/red_knot_python_semantic/src/types/type_ordering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,5 +271,5 @@ pub(crate) fn sequence_is_sorted<'db>(sequence: impl IntoIterator<Item = &'db Ty
sequence
.into_iter()
.tuple_windows()
.all(|(left, right)| union_elements_ordering(left, right) != Ordering::Greater)
.all(|(left, right)| !union_elements_ordering(left, right).is_gt())
}

0 comments on commit 6496d9a

Please sign in to comment.