[ty] Simplify Type::try_bool() (#18342)

## Summary

I don't think we're ever going to add any `KnownInstanceType` variants
that evaluate to `False` in a boolean context; the
`KnownInstanceType::bool()` method just seems like unnecessary
complexity.

## Test Plan

`cargo test -p ty_python_semantic`
This commit is contained in:
Alex Waygood 2025-05-27 19:32:17 +01:00 committed by GitHub
parent e03e05d2b3
commit 743764d384
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 4 additions and 58 deletions

View file

@ -3413,6 +3413,9 @@ impl<'db> Type<'db> {
| Type::DataclassDecorator(_)
| Type::DataclassTransformer(_)
| Type::ModuleLiteral(_)
| Type::PropertyInstance(_)
| Type::BoundSuper(_)
| Type::KnownInstance(_)
| Type::AlwaysTruthy => Truthiness::AlwaysTrue,
Type::AlwaysFalsy => Truthiness::AlwaysFalse,
@ -3448,10 +3451,6 @@ impl<'db> Type<'db> {
Type::ProtocolInstance(_) => try_dunder_bool()?,
Type::KnownInstance(known_instance) => known_instance.bool(),
Type::PropertyInstance(_) => Truthiness::AlwaysTrue,
Type::Union(union) => try_union(*union)?,
Type::Intersection(_) => {
@ -3464,7 +3463,6 @@ impl<'db> Type<'db> {
Type::StringLiteral(str) => Truthiness::from(!str.value(db).is_empty()),
Type::BytesLiteral(bytes) => Truthiness::from(!bytes.value(db).is_empty()),
Type::Tuple(items) => Truthiness::from(!items.elements(db).is_empty()),
Type::BoundSuper(_) => Truthiness::AlwaysTrue,
};
Ok(truthiness)

View file

@ -11,7 +11,7 @@
use std::fmt::Display;
use super::generics::GenericContext;
use super::{ClassType, Truthiness, Type, TypeAliasType, TypeVarInstance, class::KnownClass};
use super::{ClassType, Type, TypeAliasType, TypeVarInstance, class::KnownClass};
use crate::db::Db;
use crate::module_resolver::{KnownModule, file_to_module};
use ruff_db::files::File;
@ -101,58 +101,6 @@ pub enum KnownInstanceType<'db> {
}
impl<'db> KnownInstanceType<'db> {
/// Evaluate the known instance in boolean context
pub(crate) const fn bool(self) -> Truthiness {
match self {
Self::Annotated
| Self::Literal
| Self::LiteralString
| Self::Optional
// This is a legacy `TypeVar` _outside_ of any generic class or function, so it's
// AlwaysTrue. The truthiness of a typevar inside of a generic class or function
// depends on its bounds and constraints; but that's represented by `Type::TypeVar` and
// handled in elsewhere.
| Self::TypeVar(_)
| Self::Union
| Self::NoReturn
| Self::Never
| Self::Tuple
| Self::Type
| Self::TypingSelf
| Self::Final
| Self::ClassVar
| Self::Callable
| Self::Concatenate
| Self::Unpack
| Self::Required
| Self::NotRequired
| Self::TypeAlias
| Self::TypeGuard
| Self::TypedDict
| Self::TypeIs
| Self::List
| Self::Dict
| Self::DefaultDict
| Self::Set
| Self::FrozenSet
| Self::Counter
| Self::Deque
| Self::ChainMap
| Self::OrderedDict
| Self::Protocol(_)
| Self::Generic(_)
| Self::ReadOnly
| Self::TypeAliasType(_)
| Self::Unknown
| Self::AlwaysTruthy
| Self::AlwaysFalsy
| Self::Not
| Self::Intersection
| Self::TypeOf
| Self::CallableTypeOf => Truthiness::AlwaysTrue,
}
}
pub(crate) fn normalized(self, db: &'db dyn Db) -> Self {
match self {
Self::Annotated