Skip to content

Commit 8d620df

Browse files
committed
micro-optimisations
1 parent e3aa7a5 commit 8d620df

File tree

4 files changed

+25
-21
lines changed

4 files changed

+25
-21
lines changed

crates/ty_python_semantic/src/types.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3539,11 +3539,11 @@ impl<'db> Type<'db> {
35393539
}
35403540
},
35413541

3542-
Type::NominalInstance(instance) => match instance.class.known(db) {
3543-
Some(KnownClass::Tuple) => try_dunder_bool()?,
3544-
Some(known_class) => known_class.bool(),
3545-
None => try_dunder_bool()?,
3546-
},
3542+
Type::NominalInstance(NominalInstanceType { class, .. }) => class
3543+
.known(db)
3544+
.and_then(KnownClass::bool)
3545+
.map(Ok)
3546+
.unwrap_or_else(try_dunder_bool)?,
35473547

35483548
Type::ProtocolInstance(_) => try_dunder_bool()?,
35493549

crates/ty_python_semantic/src/types/class.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3173,7 +3173,10 @@ impl KnownClass {
31733173

31743174
/// Determine whether instances of this class are always truthy, always falsy,
31753175
/// or have an ambiguous truthiness.
3176-
pub(crate) const fn bool(self) -> Truthiness {
3176+
///
3177+
/// Returns `None` for `KnownClass::Tuple`, since the truthiness of a tuple
3178+
/// depends on its spec.
3179+
pub(crate) const fn bool(self) -> Option<Truthiness> {
31773180
match self {
31783181
// N.B. It's only generally safe to infer `Truthiness::AlwaysTrue` for a `KnownClass`
31793182
// variant if the class's `__bool__` method always returns the same thing *and* the
@@ -3199,9 +3202,9 @@ impl KnownClass {
31993202
| Self::GeneratorType
32003203
| Self::AsyncGeneratorType
32013204
| Self::MethodWrapperType
3202-
| Self::CoroutineType => Truthiness::AlwaysTrue,
3205+
| Self::CoroutineType => Some(Truthiness::AlwaysTrue),
32033206

3204-
Self::NoneType => Truthiness::AlwaysFalse,
3207+
Self::NoneType => Some(Truthiness::AlwaysFalse),
32053208

32063209
Self::Any
32073210
| Self::BaseException
@@ -3218,7 +3221,6 @@ impl KnownClass {
32183221
| Self::StdlibAlias
32193222
| Self::SupportsIndex
32203223
| Self::Set
3221-
| Self::Tuple
32223224
| Self::Int
32233225
| Self::Type
32243226
| Self::Bytes
@@ -3257,7 +3259,9 @@ impl KnownClass {
32573259
| Self::KwOnly
32583260
| Self::InitVar
32593261
| Self::NamedTupleFallback
3260-
| Self::TypedDictFallback => Truthiness::Ambiguous,
3262+
| Self::TypedDictFallback => Some(Truthiness::Ambiguous),
3263+
3264+
Self::Tuple => None,
32613265
}
32623266
}
32633267

@@ -4023,16 +4027,18 @@ impl KnownClass {
40234027
}
40244028
}
40254029

4026-
/// Return true if all instances of this `KnownClass` compare equal.
4027-
pub(super) const fn is_single_valued(self) -> bool {
4030+
/// Returns `Some(true)` if all instances of this `KnownClass` compare equal.
4031+
/// Returns `None` for `KnownClass::Tuple`, since whether or not a tuple type
4032+
/// is single-valued depends on the tuple spec.
4033+
pub(super) const fn is_single_valued(self) -> Option<bool> {
40284034
match self {
40294035
Self::NoneType
40304036
| Self::NoDefaultType
40314037
| Self::VersionInfo
40324038
| Self::EllipsisType
40334039
| Self::TypeAliasType
40344040
| Self::UnionType
4035-
| Self::NotImplementedType => true,
4041+
| Self::NotImplementedType => Some(true),
40364042

40374043
Self::Any
40384044
| Self::Bool
@@ -4045,7 +4051,6 @@ impl KnownClass {
40454051
| Self::Complex
40464052
| Self::Str
40474053
| Self::List
4048-
| Self::Tuple
40494054
| Self::Set
40504055
| Self::FrozenSet
40514056
| Self::Dict
@@ -4097,7 +4102,9 @@ impl KnownClass {
40974102
| Self::Iterable
40984103
| Self::Iterator
40994104
| Self::NamedTupleFallback
4100-
| Self::TypedDictFallback => false,
4105+
| Self::TypedDictFallback => Some(false),
4106+
4107+
Self::Tuple => None,
41014108
}
41024109
}
41034110

crates/ty_python_semantic/src/types/generics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ impl<'db> GenericContext<'db> {
263263

264264
pub(crate) fn default_specialization(self, db: &'db dyn Db) -> Specialization<'db> {
265265
let partial = self.specialize_partial(db, &vec![None; self.variables(db).len()]);
266-
if matches!(self.known_class(db), Some(KnownClass::Tuple)) {
266+
if self.known_class(db) == Some(KnownClass::Tuple) {
267267
Specialization::new(
268268
db,
269269
self,

crates/ty_python_semantic/src/types/instance.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,10 @@ impl<'db> NominalInstanceType<'db> {
161161
}
162162

163163
pub(super) fn is_single_valued(self, db: &'db dyn Db) -> bool {
164-
if let Some(tuple_spec) = self.class.tuple_spec(db) {
165-
return tuple_spec.is_single_valued(db);
166-
}
167-
168164
self.class
169165
.known(db)
170-
.map(KnownClass::is_single_valued)
166+
.and_then(KnownClass::is_single_valued)
167+
.or_else(|| Some(self.class.tuple_spec(db)?.is_single_valued(db)))
171168
.unwrap_or_else(|| is_single_member_enum(db, self.class.class_literal(db).0))
172169
}
173170

0 commit comments

Comments
 (0)