mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 05:15:12 +00:00
[red-knot] Understand typing.Tuple
(#14927)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
parent
a7e5e42b88
commit
e4885a2fb2
4 changed files with 66 additions and 2 deletions
|
@ -1758,6 +1758,7 @@ impl<'db> Type<'db> {
|
|||
Type::ClassLiteral(_) | Type::SubclassOf(_) => self.to_instance(db),
|
||||
// We treat `typing.Type` exactly the same as `builtins.type`:
|
||||
Type::KnownInstance(KnownInstanceType::Type) => KnownClass::Type.to_instance(db),
|
||||
Type::KnownInstance(KnownInstanceType::Tuple) => KnownClass::Tuple.to_instance(db),
|
||||
Type::Union(union) => union.map(db, |element| element.in_type_expression(db)),
|
||||
Type::Unknown => Type::Unknown,
|
||||
// TODO map this to a new `Type::TypeVar` variant
|
||||
|
@ -2137,6 +2138,8 @@ pub enum KnownInstanceType<'db> {
|
|||
Never,
|
||||
/// The symbol `typing.Any` (which can also be found as `typing_extensions.Any`)
|
||||
Any,
|
||||
/// The symbol `typing.Tuple` (which can also be found as `typing_extensions.Tuple`)
|
||||
Tuple,
|
||||
/// The symbol `typing.Type` (which can also be found as `typing_extensions.Type`)
|
||||
Type,
|
||||
/// A single instance of `typing.TypeVar`
|
||||
|
@ -2157,6 +2160,7 @@ impl<'db> KnownInstanceType<'db> {
|
|||
Self::NoReturn => "NoReturn",
|
||||
Self::Never => "Never",
|
||||
Self::Any => "Any",
|
||||
Self::Tuple => "Tuple",
|
||||
Self::Type => "Type",
|
||||
Self::TypeAliasType(_) => "TypeAliasType",
|
||||
}
|
||||
|
@ -2173,6 +2177,7 @@ impl<'db> KnownInstanceType<'db> {
|
|||
| Self::NoReturn
|
||||
| Self::Never
|
||||
| Self::Any
|
||||
| Self::Tuple
|
||||
| Self::Type
|
||||
| Self::TypeAliasType(_) => Truthiness::AlwaysTrue,
|
||||
}
|
||||
|
@ -2188,6 +2193,7 @@ impl<'db> KnownInstanceType<'db> {
|
|||
Self::NoReturn => "typing.NoReturn",
|
||||
Self::Never => "typing.Never",
|
||||
Self::Any => "typing.Any",
|
||||
Self::Tuple => "typing.Tuple",
|
||||
Self::Type => "typing.Type",
|
||||
Self::TypeVar(typevar) => typevar.name(db),
|
||||
Self::TypeAliasType(_) => "typing.TypeAliasType",
|
||||
|
@ -2204,7 +2210,8 @@ impl<'db> KnownInstanceType<'db> {
|
|||
Self::NoReturn => KnownClass::SpecialForm,
|
||||
Self::Never => KnownClass::SpecialForm,
|
||||
Self::Any => KnownClass::Object,
|
||||
Self::Type => KnownClass::Object,
|
||||
Self::Tuple => KnownClass::SpecialForm,
|
||||
Self::Type => KnownClass::SpecialForm,
|
||||
Self::TypeVar(_) => KnownClass::TypeVar,
|
||||
Self::TypeAliasType(_) => KnownClass::TypeAliasType,
|
||||
}
|
||||
|
@ -2231,6 +2238,7 @@ impl<'db> KnownInstanceType<'db> {
|
|||
("typing" | "typing_extensions", "Union") => Some(Self::Union),
|
||||
("typing" | "typing_extensions", "NoReturn") => Some(Self::NoReturn),
|
||||
("typing" | "typing_extensions", "Never") => Some(Self::Never),
|
||||
("typing" | "typing_extensions", "Tuple") => Some(Self::Tuple),
|
||||
("typing" | "typing_extensions", "Type") => Some(Self::Type),
|
||||
_ => None,
|
||||
}
|
||||
|
|
|
@ -4862,6 +4862,7 @@ impl<'db> TypeInferenceBuilder<'db> {
|
|||
Type::Unknown
|
||||
}
|
||||
KnownInstanceType::Type => self.infer_subclass_of_type_expression(parameters),
|
||||
KnownInstanceType::Tuple => self.infer_tuple_type_expression(parameters),
|
||||
KnownInstanceType::Any => Type::Any,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -364,10 +364,13 @@ impl<'db> ClassBase<'db> {
|
|||
| KnownInstanceType::Never
|
||||
| KnownInstanceType::Optional => None,
|
||||
KnownInstanceType::Any => Some(Self::Any),
|
||||
// TODO: classes inheriting from `typing.Type` also have `Generic` in their MRO
|
||||
// TODO: Classes inheriting from `typing.Type` et al. also have `Generic` in their MRO
|
||||
KnownInstanceType::Type => {
|
||||
ClassBase::try_from_ty(db, KnownClass::Type.to_class_literal(db))
|
||||
}
|
||||
KnownInstanceType::Tuple => {
|
||||
ClassBase::try_from_ty(db, KnownClass::Tuple.to_class_literal(db))
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue