mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-02 22:55:08 +00:00
[ty] Remove unused variants from various Known*
enums (#18015)
## Summary `KnownClass::Range`, `KnownInstanceType::Any` and `ClassBase::any()` are no longer used or useful: all our tests pass with them removed. `KnownModule::Abc` _is_ now used outside of tests, however, so I removed the `#[allow(dead_code)]` branch above that variant. ## Test Plan `cargo test -p ty_python_semantic`
This commit is contained in:
parent
ff7ebecf89
commit
669855d2b5
7 changed files with 2 additions and 73 deletions
|
@ -114,8 +114,7 @@ pub enum KnownModule {
|
||||||
TypingExtensions,
|
TypingExtensions,
|
||||||
Typing,
|
Typing,
|
||||||
Sys,
|
Sys,
|
||||||
#[allow(dead_code)]
|
Abc,
|
||||||
Abc, // currently only used in tests
|
|
||||||
Dataclasses,
|
Dataclasses,
|
||||||
Collections,
|
Collections,
|
||||||
Inspect,
|
Inspect,
|
||||||
|
|
|
@ -2432,25 +2432,6 @@ impl<'db> Type<'db> {
|
||||||
))
|
))
|
||||||
.into(),
|
.into(),
|
||||||
),
|
),
|
||||||
// TODO:
|
|
||||||
// We currently hard-code the knowledge that the following known classes are not
|
|
||||||
// descriptors, i.e. that they have no `__get__` method. This is not wrong and
|
|
||||||
// potentially even beneficial for performance, but it's not very principled.
|
|
||||||
// This case can probably be removed eventually, but we include it at the moment
|
|
||||||
// because we make extensive use of these types in our test suite. Note that some
|
|
||||||
// builtin types are not included here, since they do not have generic bases and
|
|
||||||
// are correctly handled by the `find_name_in_mro` method.
|
|
||||||
(
|
|
||||||
Some(
|
|
||||||
KnownClass::Int
|
|
||||||
| KnownClass::Str
|
|
||||||
| KnownClass::Bytes
|
|
||||||
| KnownClass::Tuple
|
|
||||||
| KnownClass::Slice
|
|
||||||
| KnownClass::Range,
|
|
||||||
),
|
|
||||||
"__get__" | "__set__" | "__delete__",
|
|
||||||
) => Some(Symbol::Unbound.into()),
|
|
||||||
|
|
||||||
_ => Some(class.class_member(db, name, policy)),
|
_ => Some(class.class_member(db, name, policy)),
|
||||||
}
|
}
|
||||||
|
@ -2460,25 +2441,6 @@ impl<'db> Type<'db> {
|
||||||
Some(ClassType::from(*alias).class_member(db, name, policy))
|
Some(ClassType::from(*alias).class_member(db, name, policy))
|
||||||
}
|
}
|
||||||
|
|
||||||
Type::SubclassOf(subclass_of)
|
|
||||||
if name == "__get__"
|
|
||||||
&& matches!(
|
|
||||||
subclass_of
|
|
||||||
.subclass_of()
|
|
||||||
.into_class()
|
|
||||||
.and_then(|c| c.known(db)),
|
|
||||||
Some(
|
|
||||||
KnownClass::Int
|
|
||||||
| KnownClass::Str
|
|
||||||
| KnownClass::Bytes
|
|
||||||
| KnownClass::Tuple
|
|
||||||
| KnownClass::Slice
|
|
||||||
| KnownClass::Range,
|
|
||||||
)
|
|
||||||
) =>
|
|
||||||
{
|
|
||||||
Some(Symbol::Unbound.into())
|
|
||||||
}
|
|
||||||
Type::SubclassOf(subclass_of_ty) => {
|
Type::SubclassOf(subclass_of_ty) => {
|
||||||
subclass_of_ty.find_name_in_mro_with_policy(db, name, policy)
|
subclass_of_ty.find_name_in_mro_with_policy(db, name, policy)
|
||||||
}
|
}
|
||||||
|
@ -4771,7 +4733,6 @@ impl<'db> Type<'db> {
|
||||||
KnownInstanceType::TypeAliasType(alias) => Ok(alias.value_type(db)),
|
KnownInstanceType::TypeAliasType(alias) => Ok(alias.value_type(db)),
|
||||||
KnownInstanceType::Never | KnownInstanceType::NoReturn => Ok(Type::Never),
|
KnownInstanceType::Never | KnownInstanceType::NoReturn => Ok(Type::Never),
|
||||||
KnownInstanceType::LiteralString => Ok(Type::LiteralString),
|
KnownInstanceType::LiteralString => Ok(Type::LiteralString),
|
||||||
KnownInstanceType::Any => Ok(Type::any()),
|
|
||||||
KnownInstanceType::Unknown => Ok(Type::unknown()),
|
KnownInstanceType::Unknown => Ok(Type::unknown()),
|
||||||
KnownInstanceType::AlwaysTruthy => Ok(Type::AlwaysTruthy),
|
KnownInstanceType::AlwaysTruthy => Ok(Type::AlwaysTruthy),
|
||||||
KnownInstanceType::AlwaysFalsy => Ok(Type::AlwaysFalsy),
|
KnownInstanceType::AlwaysFalsy => Ok(Type::AlwaysFalsy),
|
||||||
|
|
|
@ -1887,7 +1887,6 @@ pub enum KnownClass {
|
||||||
FrozenSet,
|
FrozenSet,
|
||||||
Dict,
|
Dict,
|
||||||
Slice,
|
Slice,
|
||||||
Range,
|
|
||||||
Property,
|
Property,
|
||||||
BaseException,
|
BaseException,
|
||||||
BaseExceptionGroup,
|
BaseExceptionGroup,
|
||||||
|
@ -1996,7 +1995,6 @@ impl<'db> KnownClass {
|
||||||
| Self::Bytes
|
| Self::Bytes
|
||||||
| Self::Bytearray
|
| Self::Bytearray
|
||||||
| Self::FrozenSet
|
| Self::FrozenSet
|
||||||
| Self::Range
|
|
||||||
| Self::Property
|
| Self::Property
|
||||||
| Self::SpecialForm
|
| Self::SpecialForm
|
||||||
| Self::Dict
|
| Self::Dict
|
||||||
|
@ -2051,7 +2049,6 @@ impl<'db> KnownClass {
|
||||||
| Self::List
|
| Self::List
|
||||||
| Self::Type
|
| Self::Type
|
||||||
| Self::Slice
|
| Self::Slice
|
||||||
| Self::Range
|
|
||||||
| Self::Property
|
| Self::Property
|
||||||
| Self::BaseException
|
| Self::BaseException
|
||||||
| Self::BaseExceptionGroup
|
| Self::BaseExceptionGroup
|
||||||
|
@ -2109,7 +2106,6 @@ impl<'db> KnownClass {
|
||||||
Self::List => "list",
|
Self::List => "list",
|
||||||
Self::Type => "type",
|
Self::Type => "type",
|
||||||
Self::Slice => "slice",
|
Self::Slice => "slice",
|
||||||
Self::Range => "range",
|
|
||||||
Self::Property => "property",
|
Self::Property => "property",
|
||||||
Self::BaseException => "BaseException",
|
Self::BaseException => "BaseException",
|
||||||
Self::BaseExceptionGroup => "BaseExceptionGroup",
|
Self::BaseExceptionGroup => "BaseExceptionGroup",
|
||||||
|
@ -2331,7 +2327,6 @@ impl<'db> KnownClass {
|
||||||
| Self::BaseExceptionGroup
|
| Self::BaseExceptionGroup
|
||||||
| Self::Classmethod
|
| Self::Classmethod
|
||||||
| Self::Slice
|
| Self::Slice
|
||||||
| Self::Range
|
|
||||||
| Self::Super
|
| Self::Super
|
||||||
| Self::Property => KnownModule::Builtins,
|
| Self::Property => KnownModule::Builtins,
|
||||||
Self::VersionInfo => KnownModule::Sys,
|
Self::VersionInfo => KnownModule::Sys,
|
||||||
|
@ -2416,7 +2411,6 @@ impl<'db> KnownClass {
|
||||||
| Self::FrozenSet
|
| Self::FrozenSet
|
||||||
| Self::Dict
|
| Self::Dict
|
||||||
| Self::Slice
|
| Self::Slice
|
||||||
| Self::Range
|
|
||||||
| Self::Property
|
| Self::Property
|
||||||
| Self::BaseException
|
| Self::BaseException
|
||||||
| Self::BaseExceptionGroup
|
| Self::BaseExceptionGroup
|
||||||
|
@ -2478,7 +2472,6 @@ impl<'db> KnownClass {
|
||||||
| Self::List
|
| Self::List
|
||||||
| Self::Type
|
| Self::Type
|
||||||
| Self::Slice
|
| Self::Slice
|
||||||
| Self::Range
|
|
||||||
| Self::Property
|
| Self::Property
|
||||||
| Self::GenericAlias
|
| Self::GenericAlias
|
||||||
| Self::ModuleType
|
| Self::ModuleType
|
||||||
|
@ -2537,7 +2530,6 @@ impl<'db> KnownClass {
|
||||||
"dict" => Self::Dict,
|
"dict" => Self::Dict,
|
||||||
"list" => Self::List,
|
"list" => Self::List,
|
||||||
"slice" => Self::Slice,
|
"slice" => Self::Slice,
|
||||||
"range" => Self::Range,
|
|
||||||
"property" => Self::Property,
|
"property" => Self::Property,
|
||||||
"BaseException" => Self::BaseException,
|
"BaseException" => Self::BaseException,
|
||||||
"BaseExceptionGroup" => Self::BaseExceptionGroup,
|
"BaseExceptionGroup" => Self::BaseExceptionGroup,
|
||||||
|
@ -2607,7 +2599,6 @@ impl<'db> KnownClass {
|
||||||
| Self::FrozenSet
|
| Self::FrozenSet
|
||||||
| Self::Dict
|
| Self::Dict
|
||||||
| Self::Slice
|
| Self::Slice
|
||||||
| Self::Range
|
|
||||||
| Self::Property
|
| Self::Property
|
||||||
| Self::GenericAlias
|
| Self::GenericAlias
|
||||||
| Self::ChainMap
|
| Self::ChainMap
|
||||||
|
|
|
@ -26,10 +26,6 @@ pub enum ClassBase<'db> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'db> ClassBase<'db> {
|
impl<'db> ClassBase<'db> {
|
||||||
pub(crate) const fn any() -> Self {
|
|
||||||
Self::Dynamic(DynamicType::Any)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) const fn unknown() -> Self {
|
pub(crate) const fn unknown() -> Self {
|
||||||
Self::Dynamic(DynamicType::Unknown)
|
Self::Dynamic(DynamicType::Unknown)
|
||||||
}
|
}
|
||||||
|
@ -164,7 +160,6 @@ impl<'db> ClassBase<'db> {
|
||||||
| KnownInstanceType::AlwaysTruthy
|
| KnownInstanceType::AlwaysTruthy
|
||||||
| KnownInstanceType::AlwaysFalsy => None,
|
| KnownInstanceType::AlwaysFalsy => None,
|
||||||
KnownInstanceType::Unknown => Some(Self::unknown()),
|
KnownInstanceType::Unknown => Some(Self::unknown()),
|
||||||
KnownInstanceType::Any => Some(Self::any()),
|
|
||||||
// TODO: Classes inheriting from `typing.Type` et al. also have `Generic` in their MRO
|
// TODO: Classes inheriting from `typing.Type` et al. also have `Generic` in their MRO
|
||||||
KnownInstanceType::Dict => {
|
KnownInstanceType::Dict => {
|
||||||
Self::try_from_type(db, KnownClass::Dict.to_class_literal(db))
|
Self::try_from_type(db, KnownClass::Dict.to_class_literal(db))
|
||||||
|
|
|
@ -8045,9 +8045,6 @@ impl<'db> TypeInferenceBuilder<'db> {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Type::KnownInstance(KnownInstanceType::Any) => {
|
|
||||||
SubclassOfType::subclass_of_any()
|
|
||||||
}
|
|
||||||
Type::KnownInstance(KnownInstanceType::Unknown) => {
|
Type::KnownInstance(KnownInstanceType::Unknown) => {
|
||||||
SubclassOfType::subclass_of_unknown()
|
SubclassOfType::subclass_of_unknown()
|
||||||
}
|
}
|
||||||
|
@ -8486,7 +8483,6 @@ impl<'db> TypeInferenceBuilder<'db> {
|
||||||
}
|
}
|
||||||
KnownInstanceType::NoReturn
|
KnownInstanceType::NoReturn
|
||||||
| KnownInstanceType::Never
|
| KnownInstanceType::Never
|
||||||
| KnownInstanceType::Any
|
|
||||||
| KnownInstanceType::AlwaysTruthy
|
| KnownInstanceType::AlwaysTruthy
|
||||||
| KnownInstanceType::AlwaysFalsy => {
|
| KnownInstanceType::AlwaysFalsy => {
|
||||||
self.infer_type_expression(arguments_slice);
|
self.infer_type_expression(arguments_slice);
|
||||||
|
|
|
@ -34,11 +34,6 @@ pub enum KnownInstanceType<'db> {
|
||||||
NoReturn,
|
NoReturn,
|
||||||
/// The symbol `typing.Never` available since 3.11 (which can also be found as `typing_extensions.Never`)
|
/// The symbol `typing.Never` available since 3.11 (which can also be found as `typing_extensions.Never`)
|
||||||
Never,
|
Never,
|
||||||
/// The symbol `typing.Any` (which can also be found as `typing_extensions.Any`)
|
|
||||||
/// This is not used since typeshed switched to representing `Any` as a class; now we use
|
|
||||||
/// `KnownClass::Any` instead. But we still support the old `Any = object()` representation, at
|
|
||||||
/// least for now. TODO maybe remove?
|
|
||||||
Any,
|
|
||||||
/// The symbol `typing.Tuple` (which can also be found as `typing_extensions.Tuple`)
|
/// The symbol `typing.Tuple` (which can also be found as `typing_extensions.Tuple`)
|
||||||
Tuple,
|
Tuple,
|
||||||
/// The symbol `typing.List` (which can also be found as `typing_extensions.List`)
|
/// The symbol `typing.List` (which can also be found as `typing_extensions.List`)
|
||||||
|
@ -121,7 +116,6 @@ impl<'db> KnownInstanceType<'db> {
|
||||||
| Self::Union
|
| Self::Union
|
||||||
| Self::NoReturn
|
| Self::NoReturn
|
||||||
| Self::Never
|
| Self::Never
|
||||||
| Self::Any
|
|
||||||
| Self::Tuple
|
| Self::Tuple
|
||||||
| Self::Type
|
| Self::Type
|
||||||
| Self::TypingSelf
|
| Self::TypingSelf
|
||||||
|
@ -177,7 +171,6 @@ impl<'db> KnownInstanceType<'db> {
|
||||||
Self::Union => KnownClass::SpecialForm,
|
Self::Union => KnownClass::SpecialForm,
|
||||||
Self::NoReturn => KnownClass::SpecialForm,
|
Self::NoReturn => KnownClass::SpecialForm,
|
||||||
Self::Never => KnownClass::SpecialForm,
|
Self::Never => KnownClass::SpecialForm,
|
||||||
Self::Any => KnownClass::Object,
|
|
||||||
Self::Tuple => KnownClass::SpecialForm,
|
Self::Tuple => KnownClass::SpecialForm,
|
||||||
Self::Type => KnownClass::SpecialForm,
|
Self::Type => KnownClass::SpecialForm,
|
||||||
Self::TypingSelf => KnownClass::SpecialForm,
|
Self::TypingSelf => KnownClass::SpecialForm,
|
||||||
|
@ -236,7 +229,6 @@ impl<'db> KnownInstanceType<'db> {
|
||||||
symbol_name: &str,
|
symbol_name: &str,
|
||||||
) -> Option<Self> {
|
) -> Option<Self> {
|
||||||
let candidate = match symbol_name {
|
let candidate = match symbol_name {
|
||||||
"Any" => Self::Any,
|
|
||||||
"ClassVar" => Self::ClassVar,
|
"ClassVar" => Self::ClassVar,
|
||||||
"Deque" => Self::Deque,
|
"Deque" => Self::Deque,
|
||||||
"List" => Self::List,
|
"List" => Self::List,
|
||||||
|
@ -291,8 +283,7 @@ impl<'db> KnownInstanceType<'db> {
|
||||||
/// Some variants could validly be defined in either `typing` or `typing_extensions`, however.
|
/// Some variants could validly be defined in either `typing` or `typing_extensions`, however.
|
||||||
pub(super) fn check_module(self, module: KnownModule) -> bool {
|
pub(super) fn check_module(self, module: KnownModule) -> bool {
|
||||||
match self {
|
match self {
|
||||||
Self::Any
|
Self::ClassVar
|
||||||
| Self::ClassVar
|
|
||||||
| Self::Deque
|
| Self::Deque
|
||||||
| Self::List
|
| Self::List
|
||||||
| Self::Dict
|
| Self::Dict
|
||||||
|
@ -359,7 +350,6 @@ impl Display for KnownInstanceRepr<'_> {
|
||||||
KnownInstanceType::Union => f.write_str("typing.Union"),
|
KnownInstanceType::Union => f.write_str("typing.Union"),
|
||||||
KnownInstanceType::NoReturn => f.write_str("typing.NoReturn"),
|
KnownInstanceType::NoReturn => f.write_str("typing.NoReturn"),
|
||||||
KnownInstanceType::Never => f.write_str("typing.Never"),
|
KnownInstanceType::Never => f.write_str("typing.Never"),
|
||||||
KnownInstanceType::Any => f.write_str("typing.Any"),
|
|
||||||
KnownInstanceType::Tuple => f.write_str("typing.Tuple"),
|
KnownInstanceType::Tuple => f.write_str("typing.Tuple"),
|
||||||
KnownInstanceType::Type => f.write_str("typing.Type"),
|
KnownInstanceType::Type => f.write_str("typing.Type"),
|
||||||
KnownInstanceType::TypingSelf => f.write_str("typing.Self"),
|
KnownInstanceType::TypingSelf => f.write_str("typing.Self"),
|
||||||
|
|
|
@ -185,9 +185,6 @@ pub(super) fn union_or_intersection_elements_ordering<'db>(
|
||||||
|
|
||||||
(Type::KnownInstance(left_instance), Type::KnownInstance(right_instance)) => {
|
(Type::KnownInstance(left_instance), Type::KnownInstance(right_instance)) => {
|
||||||
match (left_instance, right_instance) {
|
match (left_instance, right_instance) {
|
||||||
(KnownInstanceType::Any, _) => Ordering::Less,
|
|
||||||
(_, KnownInstanceType::Any) => Ordering::Greater,
|
|
||||||
|
|
||||||
(KnownInstanceType::Tuple, _) => Ordering::Less,
|
(KnownInstanceType::Tuple, _) => Ordering::Less,
|
||||||
(_, KnownInstanceType::Tuple) => Ordering::Greater,
|
(_, KnownInstanceType::Tuple) => Ordering::Greater,
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue