Skip to content

Use union of current context and left side for right side narrowing of binary ops #19249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Only do that for boolean ops, not ternaries
  • Loading branch information
sterliakov committed Jun 7, 2025
commit 862a8d09177e7bdc17ae7986f8163e3080741b30
28 changes: 15 additions & 13 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4271,7 +4271,9 @@ def check_boolean_op(self, e: OpExpr, context: Context) -> Type:
):
self.msg.unreachable_right_operand(e.op, e.right)

right_type = self.analyze_cond_branch(right_map, e.right, expanded_left_type)
right_type = self.analyze_cond_branch(
right_map, e.right, self._combined_context(expanded_left_type)
)

if left_map is None and right_map is None:
return UninhabitedType()
Expand Down Expand Up @@ -5879,26 +5881,26 @@ def analyze_cond_branch(
allow_none_return: bool = False,
suppress_unreachable_errors: bool = True,
) -> Type:
ctx_items = []
if context is not None:
ctx_items.append(context)
if self.type_context and self.type_context[-1] is not None:
ctx_items.append(self.type_context[-1])
ctx: Type | None
if ctx_items:
ctx = make_simplified_union(ctx_items)
else:
ctx = None
with self.chk.binder.frame_context(can_skip=True, fall_through=0):
if map is None:
# We still need to type check node, in case we want to
# process it for isinstance checks later. Since the branch was
# determined to be unreachable, any errors should be suppressed.
with self.msg.filter_errors(filter_errors=suppress_unreachable_errors):
self.accept(node, type_context=ctx, allow_none_return=allow_none_return)
self.accept(node, type_context=context, allow_none_return=allow_none_return)
return UninhabitedType()
self.chk.push_type_map(map)
return self.accept(node, type_context=ctx, allow_none_return=allow_none_return)
return self.accept(node, type_context=context, allow_none_return=allow_none_return)

def _combined_context(self, ty: Type | None) -> Type | None:
ctx_items = []
if ty is not None:
ctx_items.append(ty)
if self.type_context and self.type_context[-1] is not None:
ctx_items.append(self.type_context[-1])
if ctx_items:
return make_simplified_union(ctx_items)
return None

#
# Helpers
Expand Down