Skip to content

Commit 16ee558

Browse files
committed
fix: Preserve narrowing in chained-comparison operands
A regression was introduced that caused loss of type inference for operands in chained comparisons, such as `None is not a == b`. The logic to prevent unwanted type widening was too aggressive and discarded valid narrowing from `is not` checks. The fix adjusts the condition in `update_from_options` to allow updates when only a single frame is being merged. This correctly identifies sequential updates within expressions (like chained comparisons) and preserves the narrowing, while still preventing unwanted widening when merging multiple control-flow branches.
1 parent bdb0dd4 commit 16ee558

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

mypy/binder.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,12 @@ def update_from_options(self, frames: list[Frame]) -> bool:
336336
# See check-isinstance.test::testNoneCheckDoesNotMakeTypeVarOptional
337337
# This is a safe assumption: the fact that we checked something with `is`
338338
# or `isinstance` does not change the type of the value.
339-
continue
339+
if len(frames) == 1:
340+
# This is likely a sequential update, not a merge of
341+
# conditional branches. Let the update proceed to preserve narrowing.
342+
pass
343+
else:
344+
continue
340345

341346
# Remove exact duplicates to save pointless work later, this is
342347
# a micro-optimization for --allow-redefinition-new.

0 commit comments

Comments
 (0)