Skip to content
Merged
Show file tree
Hide file tree
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
else_irrelevant_for_possibly_undefined -> unreachable_else
  • Loading branch information
tyralla committed Oct 31, 2025
commit 7e9b592d0a15c0da26a8c073eb90f45e2c0ebeba
2 changes: 1 addition & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -5036,7 +5036,7 @@ def visit_if_stmt(self, s: IfStmt) -> None:

if_map, else_map = self.find_isinstance_check(e)

s.else_irrelevant_for_possibly_undefined = else_map is None
s.unreachable_else = else_map is None

# XXX Issue a warning if condition is always False?
with self.binder.frame_context(can_skip=True, fall_through=2):
Expand Down
6 changes: 3 additions & 3 deletions mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1792,21 +1792,21 @@ def accept(self, visitor: StatementVisitor[T]) -> T:


class IfStmt(Statement):
__slots__ = ("expr", "body", "else_body", "else_irrelevant_for_possibly_undefined")
__slots__ = ("expr", "body", "else_body", "unreachable_else")

__match_args__ = ("expr", "body", "else_body", "else_body_irrelevant_for_possibly_undefined")

expr: list[Expression]
body: list[Block]
else_body: Block | None
else_irrelevant_for_possibly_undefined: bool
unreachable_else: bool

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a brief comment explaining the attribute here? It would be nice to immediately see a notice with this attr's meaning and "volatile" nature

@tyralla tyralla Nov 15, 2025 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, sorry for the delay. I added some explanations that hopefully clarify things.


def __init__(self, expr: list[Expression], body: list[Block], else_body: Block | None) -> None:
super().__init__()
self.expr = expr
self.body = body
self.else_body = else_body
self.else_irrelevant_for_possibly_undefined = False
self.unreachable_else = False

def accept(self, visitor: StatementVisitor[T]) -> T:
return visitor.visit_if_stmt(self)
Expand Down
2 changes: 1 addition & 1 deletion mypy/partially_defined.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ def visit_if_stmt(self, o: IfStmt) -> None:
continue
b.accept(self)
self.tracker.next_branch()
if o.else_irrelevant_for_possibly_undefined:
if o.unreachable_else:
self.tracker.skip_branch()
elif o.else_body:
if o.else_body.is_unreachable:
Expand Down
Loading