Skip to content

Commit

Permalink
Simplify per review
Browse files Browse the repository at this point in the history
  • Loading branch information
InSyncWithFoo committed Jan 8, 2025
1 parent d82e757 commit d6fa64f
Showing 1 changed file with 32 additions and 39 deletions.
71 changes: 32 additions & 39 deletions crates/red_knot_python_semantic/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use rustc_hash::FxHasher;
use std::collections::HashSet;
use std::hash::{BuildHasherDefault, Hash};
use std::hash::Hash;
use std::iter;

use context::InferContext;
Expand Down Expand Up @@ -558,8 +556,6 @@ pub enum Type<'db> {
// TODO protocols, callable types, overloads, generics, type vars
}

type OrderedTypeSet<'a, 'db> = HashSet<&'a Type<'db>, BuildHasherDefault<FxHasher>>;

impl<'db> Type<'db> {
pub const fn is_unknown(&self) -> bool {
matches!(self, Type::Unknown)
Expand Down Expand Up @@ -1490,9 +1486,6 @@ impl<'db> Type<'db> {
return false;
}

let first_elements = first_elements.iter().collect::<OrderedTypeSet>();
let second_elements = second_elements.iter().collect::<OrderedTypeSet>();

iter::zip(first_elements, second_elements).all(equivalent)
}

Expand All @@ -1509,12 +1502,6 @@ impl<'db> Type<'db> {
return false;
}

let first_positive = first_positive.iter().collect::<OrderedTypeSet>();
let first_negative = first_negative.iter().collect::<OrderedTypeSet>();

let second_positive = second_positive.iter().collect::<OrderedTypeSet>();
let second_negative = second_negative.iter().collect::<OrderedTypeSet>();

iter::zip(first_positive, second_positive).all(equivalent)
&& iter::zip(first_negative, second_negative).all(equivalent)
}
Expand Down Expand Up @@ -4746,6 +4733,7 @@ pub(crate) mod tests {
assert!(!from.into_type(&db).is_fully_static(&db));
}

#[test_case(Ty::Todo, Ty::Todo)]
#[test_case(Ty::Any, Ty::Any)]
#[test_case(Ty::Unknown, Ty::Unknown)]
#[test_case(Ty::Any, Ty::Unknown)]
Expand All @@ -4758,30 +4746,32 @@ pub(crate) mod tests {
#[test_case(Ty::SliceLiteral(0, 1, 2), Ty::SliceLiteral(0, 1, 2))]
#[test_case(Ty::BuiltinClassLiteral("str"), Ty::BuiltinClassLiteral("str"))]
#[test_case(Ty::SubclassOfAny, Ty::SubclassOfUnknown)]
#[test_case(
Ty::Union(vec![Ty::BuiltinInstance("str"), Ty::BuiltinInstance("int")]),
Ty::Union(vec![Ty::BuiltinInstance("int"), Ty::BuiltinInstance("str")])
)]
#[test_case(
Ty::Intersection {
pos: vec![Ty::BuiltinInstance("str"), Ty::BuiltinInstance("int")],
neg: vec![Ty::BuiltinInstance("bytes"), Ty::None]
},
Ty::Intersection {
pos: vec![Ty::BuiltinInstance("int"), Ty::BuiltinInstance("str")],
neg: vec![Ty::None, Ty::BuiltinInstance("bytes")]
}
)]
#[test_case(
Ty::Intersection {
pos: vec![Ty::Union(vec![Ty::BuiltinInstance("str"), Ty::BuiltinInstance("int")])],
neg: vec![Ty::SubclassOfAny]
},
Ty::Intersection {
pos: vec![Ty::Union(vec![Ty::BuiltinInstance("int"), Ty::BuiltinInstance("str")])],
neg: vec![Ty::SubclassOfUnknown]
}
)]
#[test_case(Ty::BuiltinInstance("type"), Ty::SubclassOfBuiltinClass("object"))]
// TODO: Compare unions/intersections with different orders
// #[test_case(
// Ty::Union(vec![Ty::BuiltinInstance("str"), Ty::BuiltinInstance("int")]),
// Ty::Union(vec![Ty::BuiltinInstance("int"), Ty::BuiltinInstance("str")])
// )]
// #[test_case(
// Ty::Intersection {
// pos: vec![Ty::BuiltinInstance("str"), Ty::BuiltinInstance("int")],
// neg: vec![Ty::BuiltinInstance("bytes"), Ty::None]
// },
// Ty::Intersection {
// pos: vec![Ty::BuiltinInstance("int"), Ty::BuiltinInstance("str")],
// neg: vec![Ty::None, Ty::BuiltinInstance("bytes")]
// }
// )]
// #[test_case(
// Ty::Intersection {
// pos: vec![Ty::Union(vec![Ty::BuiltinInstance("str"), Ty::BuiltinInstance("int")])],
// neg: vec![Ty::SubclassOfAny]
// },
// Ty::Intersection {
// pos: vec![Ty::Union(vec![Ty::BuiltinInstance("int"), Ty::BuiltinInstance("str")])],
// neg: vec![Ty::SubclassOfUnknown]
// }
// )]
fn is_gradual_equivalent_to(a: Ty, b: Ty) {
let db = setup_db();
let a = a.into_type(&db);
Expand All @@ -4791,7 +4781,10 @@ pub(crate) mod tests {
assert!(b.is_gradual_equivalent_to(&db, a));
}

#[test_case(Ty::Todo, Ty::Todo)]
#[test_case(Ty::BuiltinInstance("type"), Ty::SubclassOfAny)]
#[test_case(Ty::BuiltinInstance("type"), Ty::SubclassOfUnknown)]
#[test_case(Ty::SubclassOfBuiltinClass("object"), Ty::SubclassOfAny)]
#[test_case(Ty::SubclassOfBuiltinClass("object"), Ty::SubclassOfUnknown)]
#[test_case(
Ty::Union(vec![Ty::BuiltinInstance("str"), Ty::BuiltinInstance("int")]),
Ty::Union(vec![Ty::BuiltinInstance("int"), Ty::BuiltinInstance("str"), Ty::BuiltinInstance("bytes")])
Expand Down

0 comments on commit d6fa64f

Please sign in to comment.