From f7804ea29da839e747cd40afcf4016bde76da19c Mon Sep 17 00:00:00 2001 From: Eric Mark Martin Date: Wed, 22 Oct 2025 17:09:23 -0400 Subject: [PATCH] fix typeguard overriding logic --- .../resources/mdtest/narrow/type_guards.md | 28 ++++++++++++++++++- crates/ty_python_semantic/src/types/narrow.rs | 14 ++++++++-- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/crates/ty_python_semantic/resources/mdtest/narrow/type_guards.md b/crates/ty_python_semantic/resources/mdtest/narrow/type_guards.md index 9c88845f12..c2a5f8feb6 100644 --- a/crates/ty_python_semantic/resources/mdtest/narrow/type_guards.md +++ b/crates/ty_python_semantic/resources/mdtest/narrow/type_guards.md @@ -359,7 +359,33 @@ def narrowed_type_must_be_exact(a: object, b: Baz): reveal_type(a) # revealed: Foo ``` -## Complex boolean logic with TypeGuard and TypeIs +## TypeGuard overrides normal constraints + +TypeGuard constraints override any previous narrowing, but additional "regular" constraints can be +added on to TypeGuard constraints. + +```py +from typing_extensions import TypeGuard, TypeIs + +class A: ... +class B: ... +class C: ... + +def f(x: object) -> TypeGuard[A]: + return True + +def g(x: object) -> TypeGuard[B]: + return True + +def h(x: object) -> TypeIs[C]: + return True + +def _(x: object): + if f(x) and g(x) and h(x): + reveal_type(x) # revealed: B & C +``` + +## Boolean logic with TypeGuard and TypeIs TypeGuard constraints need to properly distribute through boolean operations. diff --git a/crates/ty_python_semantic/src/types/narrow.rs b/crates/ty_python_semantic/src/types/narrow.rs index 72e34c83db..b7cdf10e02 100644 --- a/crates/ty_python_semantic/src/types/narrow.rs +++ b/crates/ty_python_semantic/src/types/narrow.rs @@ -317,8 +317,16 @@ impl<'db> Conjunction<'db> { /// Evaluate this conjunction to a single type. /// If there's a `TypeGuard` constraint, it replaces the regular constraint. /// Otherwise, returns the regular constraint. - fn evaluate_type_constraint(self) -> Type<'db> { - self.typeguard.unwrap_or(self.constraint) + fn evaluate_type_constraint(self, db: &'db dyn Db) -> Type<'db> { + self.typeguard.map_or_else( + || self.constraint, + |typeguard_constraint| { + IntersectionBuilder::new(db) + .add_positive(typeguard_constraint) + .add_positive(self.constraint) + .build() + }, + ) } } @@ -367,7 +375,7 @@ impl<'db> NarrowingConstraint<'db> { db, self.disjuncts .into_iter() - .map(Conjunction::evaluate_type_constraint), + .map(|disjunct| Conjunction::evaluate_type_constraint(disjunct, db)), ) } }