[ty] Add narrowing for isinstance() and issubclass() checks that use PEP-604 unions (#21334)

This commit is contained in:
Alex Waygood
2025-11-08 18:20:46 +00:00
committed by GitHub
parent 09e6af16c8
commit 020ff1723b
3 changed files with 156 additions and 3 deletions

View File

@@ -11,9 +11,9 @@ use crate::types::enums::{enum_member_literals, enum_metadata};
use crate::types::function::KnownFunction;
use crate::types::infer::infer_same_file_expression_type;
use crate::types::{
ClassLiteral, ClassType, IntersectionBuilder, KnownClass, SpecialFormType, SubclassOfInner,
SubclassOfType, Truthiness, Type, TypeContext, TypeVarBoundOrConstraints, UnionBuilder,
infer_expression_types,
ClassLiteral, ClassType, IntersectionBuilder, KnownClass, KnownInstanceType, SpecialFormType,
SubclassOfInner, SubclassOfType, Truthiness, Type, TypeContext, TypeVarBoundOrConstraints,
UnionBuilder, infer_expression_types,
};
use ruff_db::parsed::{ParsedModuleRef, parsed_module};
@@ -212,6 +212,23 @@ impl ClassInfoConstraintFunction {
)
}),
Type::KnownInstance(KnownInstanceType::UnionType(elements)) => {
UnionType::try_from_elements(
db,
elements.elements(db).iter().map(|element| {
// A special case is made for `None` at runtime
// (it's implicitly converted to `NoneType` in `int | None`)
// which means that `isinstance(x, int | None)` works even though
// `None` is not a class literal.
if element.is_none(db) {
self.generate_constraint(db, KnownClass::NoneType.to_class_literal(db))
} else {
self.generate_constraint(db, *element)
}
}),
)
}
Type::AlwaysFalsy
| Type::AlwaysTruthy
| Type::BooleanLiteral(_)