diff --git a/mypy/checker.py b/mypy/checker.py index 7579c36a97d0..e4ee0bf4d14c 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -5577,6 +5577,8 @@ def infer_variable_types_from_type_maps( previous_type, _, _ = self.check_lvalue(expr) if previous_type is not None: already_exists = True + if isinstance(expr.node, Var) and expr.node.is_final: + self.msg.cant_assign_to_final(expr.name, False, expr) if self.check_subtype( typ, previous_type, diff --git a/test-data/unit/check-python310.test b/test-data/unit/check-python310.test index bb8f038eb1eb..80fd64fa3569 100644 --- a/test-data/unit/check-python310.test +++ b/test-data/unit/check-python310.test @@ -2839,3 +2839,13 @@ match value_type: case _: assert_never(value_type) [builtins fixtures/tuple.pyi] + +[case testAssignmentToFinalInMatchCaseNotAllowed] +from typing import Final + +FOO: Final[int] = 10 + +val: int = 8 +match val: + case FOO: # E: Cannot assign to final name "FOO" + pass