Skip to content

Commit 6cc9930

Browse files
committed
Avoid strange behavior regarding "narrowing" expressions for unreachability
1 parent 53553e7 commit 6cc9930

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

mypy/binder.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,6 @@ def push_frame(self, conditional_frame: bool = False) -> Frame:
153153
return f
154154

155155
def _put(self, key: Key, type: Type, from_assignment: bool, index: int = -1) -> None:
156-
if isinstance(get_proper_type(type), UninhabitedType):
157-
self.frames[index].unreachable = True
158156
self.frames[index].types[key] = CurrentType(type, from_assignment)
159157

160158
def _get(self, key: Key, index: int = -1) -> CurrentType | None:
@@ -170,6 +168,9 @@ def put(self, expr: Expression, typ: Type, *, from_assignment: bool = True) -> N
170168
171169
This is used for isinstance() etc. Assignments should go through assign_type().
172170
"""
171+
if isinstance(get_proper_type(typ), UninhabitedType):
172+
self.frames[-1].unreachable = True
173+
173174
if not isinstance(expr, (IndexExpr, MemberExpr, NameExpr)):
174175
return
175176
if not literal(expr):

test-data/unit/check-unreachable-code.test

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,9 +1403,8 @@ from typing import Literal
14031403
def nope() -> Literal[False]: ...
14041404

14051405
def f() -> None:
1406-
# TODO: this should be unreachable
14071406
if nope():
1408-
x = 1
1407+
x = 1 # E: Statement is unreachable
14091408
[builtins fixtures/dict.pyi]
14101409

14111410
[case testUnreachableLiteralFrom__bool__]
@@ -1428,7 +1427,7 @@ else:
14281427
x = 2 # E: Statement is unreachable
14291428

14301429
if Lie():
1431-
x = 3
1430+
x = 3 # E: Statement is unreachable
14321431

14331432
if Maybe():
14341433
x = 4

0 commit comments

Comments
 (0)