[ty] Improve isinstance() truthiness analysis for generic types (#19668)

This commit is contained in:
Alex Waygood 2025-08-01 14:44:22 +01:00 committed by GitHub
parent d8151f0239
commit 18aae21b9a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 111 additions and 5 deletions

View file

@ -76,9 +76,9 @@ use crate::types::narrow::ClassInfoConstraintFunction;
use crate::types::signatures::{CallableSignature, Signature};
use crate::types::visitor::any_over_type;
use crate::types::{
BoundMethodType, CallableType, ClassLiteral, ClassType, DeprecatedInstance, DynamicType,
KnownClass, Truthiness, Type, TypeMapping, TypeRelation, TypeTransformer, TypeVarInstance,
UnionBuilder, all_members, walk_type_mapping,
BoundMethodType, CallableType, ClassBase, ClassLiteral, ClassType, DeprecatedInstance,
DynamicType, KnownClass, Truthiness, Type, TypeMapping, TypeRelation, TypeTransformer,
TypeVarInstance, UnionBuilder, all_members, walk_type_mapping,
};
use crate::{Db, FxOrderSet, ModuleName, resolve_module};
@ -901,7 +901,12 @@ fn is_instance_truthiness<'db>(
if let Type::NominalInstance(instance) = ty {
if instance
.class
.is_subclass_of(db, ClassType::NonGeneric(class))
.iter_mro(db)
.filter_map(ClassBase::into_class)
.any(|c| match c {
ClassType::Generic(c) => c.origin(db) == class,
ClassType::NonGeneric(c) => c == class,
})
{
return true;
}