Skip to content

Commit

Permalink
[red-knot] Fix intersection simplification for ~Any/~Unknown (#14195
Browse files Browse the repository at this point in the history
)

## Summary

Another bug found using [property
testing](#14178).

## Test Plan

New unit test
  • Loading branch information
sharkdp authored Nov 8, 2024
1 parent fed35a2 commit 670f958
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion crates/red_knot_python_semantic/src/types/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ impl<'db> InnerIntersectionBuilder<'db> {
// Adding any of these types to the negative side of an intersection
// is equivalent to adding it to the positive side. We do this to
// simplify the representation.
self.positive.insert(ty);
self.add_positive(db, ty);
}
// ~Literal[True] & bool = Literal[False]
Type::BooleanLiteral(bool)
Expand Down Expand Up @@ -592,6 +592,22 @@ mod tests {
assert_eq!(ta_not_i0.display(&db).to_string(), "int & Any | Literal[1]");
}

#[test]
fn build_intersection_simplify_negative_any() {
let db = setup_db();

let ty = IntersectionBuilder::new(&db)
.add_negative(Type::Any)
.build();
assert_eq!(ty, Type::Any);

let ty = IntersectionBuilder::new(&db)
.add_positive(Type::Never)
.add_negative(Type::Any)
.build();
assert_eq!(ty, Type::Never);
}

#[test]
fn intersection_distributes_over_union() {
let db = setup_db();
Expand Down

0 comments on commit 670f958

Please sign in to comment.