mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 06:11:43 +00:00
[red-knot] Fix is_disjoint_from
for class literals (#14210)
## Summary `Ty::BuiltinClassLiteral(…)` is a sub~~class~~type of `Ty::BuiltinInstance("type")`, so it can't be disjoint from it. ## Test Plan New `is_not_disjoint_from` test case
This commit is contained in:
parent
953e862aca
commit
1430f21283
1 changed files with 15 additions and 8 deletions
|
@ -823,14 +823,20 @@ impl<'db> Type<'db> {
|
||||||
),
|
),
|
||||||
(Type::SliceLiteral(..), _) | (_, Type::SliceLiteral(..)) => true,
|
(Type::SliceLiteral(..), _) | (_, Type::SliceLiteral(..)) => true,
|
||||||
|
|
||||||
(
|
(Type::ClassLiteral(..), Type::Instance(InstanceType { class }))
|
||||||
Type::FunctionLiteral(..) | Type::ModuleLiteral(..) | Type::ClassLiteral(..),
|
| (Type::Instance(InstanceType { class }), Type::ClassLiteral(..)) => {
|
||||||
Type::Instance(InstanceType { class }),
|
!matches!(class.known(db), Some(KnownClass::Type | KnownClass::Object))
|
||||||
)
|
}
|
||||||
| (
|
(Type::FunctionLiteral(..), Type::Instance(InstanceType { class }))
|
||||||
Type::Instance(InstanceType { class }),
|
| (Type::Instance(InstanceType { class }), Type::FunctionLiteral(..)) => !matches!(
|
||||||
Type::FunctionLiteral(..) | Type::ModuleLiteral(..) | Type::ClassLiteral(..),
|
class.known(db),
|
||||||
) => !class.is_known(db, KnownClass::Object),
|
Some(KnownClass::FunctionType | KnownClass::Object)
|
||||||
|
),
|
||||||
|
(Type::ModuleLiteral(..), Type::Instance(InstanceType { class }))
|
||||||
|
| (Type::Instance(InstanceType { class }), Type::ModuleLiteral(..)) => !matches!(
|
||||||
|
class.known(db),
|
||||||
|
Some(KnownClass::ModuleType | KnownClass::Object)
|
||||||
|
),
|
||||||
|
|
||||||
(Type::Instance(..), Type::Instance(..)) => {
|
(Type::Instance(..), Type::Instance(..)) => {
|
||||||
// TODO: once we have support for `final`, there might be some cases where
|
// TODO: once we have support for `final`, there might be some cases where
|
||||||
|
@ -3048,6 +3054,7 @@ mod tests {
|
||||||
#[test_case(Ty::Union(vec![Ty::IntLiteral(1), Ty::IntLiteral(2)]), Ty::Union(vec![Ty::IntLiteral(2), Ty::IntLiteral(3)]))]
|
#[test_case(Ty::Union(vec![Ty::IntLiteral(1), Ty::IntLiteral(2)]), Ty::Union(vec![Ty::IntLiteral(2), Ty::IntLiteral(3)]))]
|
||||||
#[test_case(Ty::Intersection{pos: vec![Ty::BuiltinInstance("int"), Ty::IntLiteral(2)], neg: vec![]}, Ty::IntLiteral(2))]
|
#[test_case(Ty::Intersection{pos: vec![Ty::BuiltinInstance("int"), Ty::IntLiteral(2)], neg: vec![]}, Ty::IntLiteral(2))]
|
||||||
#[test_case(Ty::Tuple(vec![Ty::IntLiteral(1), Ty::IntLiteral(2)]), Ty::Tuple(vec![Ty::IntLiteral(1), Ty::BuiltinInstance("int")]))]
|
#[test_case(Ty::Tuple(vec![Ty::IntLiteral(1), Ty::IntLiteral(2)]), Ty::Tuple(vec![Ty::IntLiteral(1), Ty::BuiltinInstance("int")]))]
|
||||||
|
#[test_case(Ty::BuiltinClassLiteral("str"), Ty::BuiltinInstance("type"))]
|
||||||
fn is_not_disjoint_from(a: Ty, b: Ty) {
|
fn is_not_disjoint_from(a: Ty, b: Ty) {
|
||||||
let db = setup_db();
|
let db = setup_db();
|
||||||
let a = a.into_type(&db);
|
let a = a.into_type(&db);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue