[ty] Remove unused variants from various Known* enums (#18015)
Some checks failed
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions
[ty Playground] Release / publish (push) Has been cancelled

## 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:
Alex Waygood 2025-05-11 11:18:55 +01:00 committed by GitHub
parent ff7ebecf89
commit 669855d2b5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 2 additions and 73 deletions

View file

@ -114,8 +114,7 @@ pub enum KnownModule {
TypingExtensions,
Typing,
Sys,
#[allow(dead_code)]
Abc, // currently only used in tests
Abc,
Dataclasses,
Collections,
Inspect,

View file

@ -2432,25 +2432,6 @@ impl<'db> Type<'db> {
))
.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)),
}
@ -2460,25 +2441,6 @@ impl<'db> Type<'db> {
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) => {
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::Never | KnownInstanceType::NoReturn => Ok(Type::Never),
KnownInstanceType::LiteralString => Ok(Type::LiteralString),
KnownInstanceType::Any => Ok(Type::any()),
KnownInstanceType::Unknown => Ok(Type::unknown()),
KnownInstanceType::AlwaysTruthy => Ok(Type::AlwaysTruthy),
KnownInstanceType::AlwaysFalsy => Ok(Type::AlwaysFalsy),

View file

@ -1887,7 +1887,6 @@ pub enum KnownClass {
FrozenSet,
Dict,
Slice,
Range,
Property,
BaseException,
BaseExceptionGroup,
@ -1996,7 +1995,6 @@ impl<'db> KnownClass {
| Self::Bytes
| Self::Bytearray
| Self::FrozenSet
| Self::Range
| Self::Property
| Self::SpecialForm
| Self::Dict
@ -2051,7 +2049,6 @@ impl<'db> KnownClass {
| Self::List
| Self::Type
| Self::Slice
| Self::Range
| Self::Property
| Self::BaseException
| Self::BaseExceptionGroup
@ -2109,7 +2106,6 @@ impl<'db> KnownClass {
Self::List => "list",
Self::Type => "type",
Self::Slice => "slice",
Self::Range => "range",
Self::Property => "property",
Self::BaseException => "BaseException",
Self::BaseExceptionGroup => "BaseExceptionGroup",
@ -2331,7 +2327,6 @@ impl<'db> KnownClass {
| Self::BaseExceptionGroup
| Self::Classmethod
| Self::Slice
| Self::Range
| Self::Super
| Self::Property => KnownModule::Builtins,
Self::VersionInfo => KnownModule::Sys,
@ -2416,7 +2411,6 @@ impl<'db> KnownClass {
| Self::FrozenSet
| Self::Dict
| Self::Slice
| Self::Range
| Self::Property
| Self::BaseException
| Self::BaseExceptionGroup
@ -2478,7 +2472,6 @@ impl<'db> KnownClass {
| Self::List
| Self::Type
| Self::Slice
| Self::Range
| Self::Property
| Self::GenericAlias
| Self::ModuleType
@ -2537,7 +2530,6 @@ impl<'db> KnownClass {
"dict" => Self::Dict,
"list" => Self::List,
"slice" => Self::Slice,
"range" => Self::Range,
"property" => Self::Property,
"BaseException" => Self::BaseException,
"BaseExceptionGroup" => Self::BaseExceptionGroup,
@ -2607,7 +2599,6 @@ impl<'db> KnownClass {
| Self::FrozenSet
| Self::Dict
| Self::Slice
| Self::Range
| Self::Property
| Self::GenericAlias
| Self::ChainMap

View file

@ -26,10 +26,6 @@ pub enum ClassBase<'db> {
}
impl<'db> ClassBase<'db> {
pub(crate) const fn any() -> Self {
Self::Dynamic(DynamicType::Any)
}
pub(crate) const fn unknown() -> Self {
Self::Dynamic(DynamicType::Unknown)
}
@ -164,7 +160,6 @@ impl<'db> ClassBase<'db> {
| KnownInstanceType::AlwaysTruthy
| KnownInstanceType::AlwaysFalsy => None,
KnownInstanceType::Unknown => Some(Self::unknown()),
KnownInstanceType::Any => Some(Self::any()),
// TODO: Classes inheriting from `typing.Type` et al. also have `Generic` in their MRO
KnownInstanceType::Dict => {
Self::try_from_type(db, KnownClass::Dict.to_class_literal(db))

View file

@ -8045,9 +8045,6 @@ impl<'db> TypeInferenceBuilder<'db> {
)
}
}
Type::KnownInstance(KnownInstanceType::Any) => {
SubclassOfType::subclass_of_any()
}
Type::KnownInstance(KnownInstanceType::Unknown) => {
SubclassOfType::subclass_of_unknown()
}
@ -8486,7 +8483,6 @@ impl<'db> TypeInferenceBuilder<'db> {
}
KnownInstanceType::NoReturn
| KnownInstanceType::Never
| KnownInstanceType::Any
| KnownInstanceType::AlwaysTruthy
| KnownInstanceType::AlwaysFalsy => {
self.infer_type_expression(arguments_slice);

View file

@ -34,11 +34,6 @@ pub enum KnownInstanceType<'db> {
NoReturn,
/// The symbol `typing.Never` available since 3.11 (which can also be found as `typing_extensions.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`)
Tuple,
/// The symbol `typing.List` (which can also be found as `typing_extensions.List`)
@ -121,7 +116,6 @@ impl<'db> KnownInstanceType<'db> {
| Self::Union
| Self::NoReturn
| Self::Never
| Self::Any
| Self::Tuple
| Self::Type
| Self::TypingSelf
@ -177,7 +171,6 @@ impl<'db> KnownInstanceType<'db> {
Self::Union => KnownClass::SpecialForm,
Self::NoReturn => KnownClass::SpecialForm,
Self::Never => KnownClass::SpecialForm,
Self::Any => KnownClass::Object,
Self::Tuple => KnownClass::SpecialForm,
Self::Type => KnownClass::SpecialForm,
Self::TypingSelf => KnownClass::SpecialForm,
@ -236,7 +229,6 @@ impl<'db> KnownInstanceType<'db> {
symbol_name: &str,
) -> Option<Self> {
let candidate = match symbol_name {
"Any" => Self::Any,
"ClassVar" => Self::ClassVar,
"Deque" => Self::Deque,
"List" => Self::List,
@ -291,8 +283,7 @@ impl<'db> KnownInstanceType<'db> {
/// Some variants could validly be defined in either `typing` or `typing_extensions`, however.
pub(super) fn check_module(self, module: KnownModule) -> bool {
match self {
Self::Any
| Self::ClassVar
Self::ClassVar
| Self::Deque
| Self::List
| Self::Dict
@ -359,7 +350,6 @@ impl Display for KnownInstanceRepr<'_> {
KnownInstanceType::Union => f.write_str("typing.Union"),
KnownInstanceType::NoReturn => f.write_str("typing.NoReturn"),
KnownInstanceType::Never => f.write_str("typing.Never"),
KnownInstanceType::Any => f.write_str("typing.Any"),
KnownInstanceType::Tuple => f.write_str("typing.Tuple"),
KnownInstanceType::Type => f.write_str("typing.Type"),
KnownInstanceType::TypingSelf => f.write_str("typing.Self"),

View file

@ -185,9 +185,6 @@ pub(super) fn union_or_intersection_elements_ordering<'db>(
(Type::KnownInstance(left_instance), Type::KnownInstance(right_instance)) => {
match (left_instance, right_instance) {
(KnownInstanceType::Any, _) => Ordering::Less,
(_, KnownInstanceType::Any) => Ordering::Greater,
(KnownInstanceType::Tuple, _) => Ordering::Less,
(_, KnownInstanceType::Tuple) => Ordering::Greater,