Skip to content

Commit 09977fd

Browse files
committed
Prevent final reassignment in match case
1 parent 2e5d7ee commit 09977fd

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

mypy/checker.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5577,6 +5577,8 @@ def infer_variable_types_from_type_maps(
55775577
previous_type, _, _ = self.check_lvalue(expr)
55785578
if previous_type is not None:
55795579
already_exists = True
5580+
if isinstance(expr.node, Var) and expr.node.is_final:
5581+
self.msg.cant_assign_to_final(expr.name, False, expr)
55805582
if self.check_subtype(
55815583
typ,
55825584
previous_type,

test-data/unit/check-final.test

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,3 +1272,13 @@ if FOO is not None:
12721272

12731273
def func() -> int:
12741274
return FOO
1275+
1276+
[case testAssignmentToFinalInMatchCaseNotAllowed]
1277+
# flags: --python-version 3.10
1278+
from typing import Final
1279+
FOO: Final = 8
1280+
1281+
val = 8
1282+
match val:
1283+
case FOO: # E: Cannot assign to final name "FOO"
1284+
pass

0 commit comments

Comments
 (0)