From 79f809cd1361d76f9353438320e7963eb60cbc44 Mon Sep 17 00:00:00 2001 From: David Peter Date: Tue, 15 Oct 2024 14:13:38 +0200 Subject: [PATCH] Handle non-singleton 'is not' case --- .../red_knot_python_semantic/src/types/narrow.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/crates/red_knot_python_semantic/src/types/narrow.rs b/crates/red_knot_python_semantic/src/types/narrow.rs index ffdb690591..0f84b9c84a 100644 --- a/crates/red_knot_python_semantic/src/types/narrow.rs +++ b/crates/red_knot_python_semantic/src/types/narrow.rs @@ -156,11 +156,15 @@ impl<'db> NarrowingConstraintsBuilder<'db> { for (op, comparator) in std::iter::zip(&**ops, &**comparators) { let comp_ty = inference.expression_ty(comparator.scoped_ast_id(self.db, scope)); match op { - ast::CmpOp::IsNot if comp_ty.is_singleton(self.db) => { - let ty = IntersectionBuilder::new(self.db) - .add_negative(comp_ty) - .build(); - self.constraints.insert(symbol, ty); + ast::CmpOp::IsNot => { + if comp_ty.is_singleton(self.db) { + let ty = IntersectionBuilder::new(self.db) + .add_negative(comp_ty) + .build(); + self.constraints.insert(symbol, ty); + } else { + // Non-singletons cannot be safely narrowed using `is not` + } } ast::CmpOp::Is => { self.constraints.insert(symbol, comp_ty);