Skip to content

Commit 8c09147

Browse files
committed
Honor NoReturn as __setitem__ return type to mark unreachable code
1 parent 8a487ff commit 8c09147

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

mypy/checker.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4047,14 +4047,16 @@ def check_indexed_assignment(
40474047
)
40484048

40494049
lvalue.method_type = method_type
4050-
self.expr_checker.check_method_call(
4050+
res_type, _ = self.expr_checker.check_method_call(
40514051
"__setitem__",
40524052
basetype,
40534053
method_type,
40544054
[lvalue.index, rvalue],
40554055
[nodes.ARG_POS, nodes.ARG_POS],
40564056
context,
40574057
)
4058+
if isinstance(get_proper_type(res_type), UninhabitedType):
4059+
self.binder.unreachable()
40584060

40594061
def try_infer_partial_type_from_indexed_assignment(
40604062
self, lvalue: IndexExpr, rvalue: Expression

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,3 +1423,13 @@ def f(value: None) -> None:
14231423

14241424
x = force_forward_ref()
14251425
[builtins fixtures/exception.pyi]
1426+
1427+
[case testSetitemNoReturn]
1428+
# flags: --warn-unreachable
1429+
from typing import NoReturn
1430+
class Foo:
1431+
def __setitem__(self, key: str, value: str) -> NoReturn:
1432+
raise Exception
1433+
Foo()['a'] = 'a'
1434+
x = 0 # E: Statement is unreachable
1435+
[builtins fixtures/exception.pyi]

0 commit comments

Comments
 (0)