diff --git a/mypy/renaming.py b/mypy/renaming.py index 05b67f41ab859..2fa3ef168a66c 100644 --- a/mypy/renaming.py +++ b/mypy/renaming.py @@ -182,6 +182,7 @@ def visit_assignment_stmt(self, s: AssignmentStmt) -> None: self.analyze_lvalue(lvalue) def visit_match_stmt(self, s: MatchStmt) -> None: + s.subject.accept(self) for i in range(len(s.patterns)): with self.enter_block(): s.patterns[i].accept(self) diff --git a/test-data/unit/check-python310.test b/test-data/unit/check-python310.test index 15454fc3e216f..feedd02d82eb5 100644 --- a/test-data/unit/check-python310.test +++ b/test-data/unit/check-python310.test @@ -1933,3 +1933,28 @@ def match_stmt_error5(x: Optional[str]) -> None: pass nested() [builtins fixtures/tuple.pyi] + +[case testMatchSubjectRedefinition] +# flags: --allow-redefinition +def transform1(a: str) -> int: + ... + +def transform2(a: int) -> str: + ... + +def redefinition_good(a: str): + a = transform1(a) + + match (a + 1): + case _: + ... + + +def redefinition_bad(a: int): + a = transform2(a) + + match (a + 1): # E: Unsupported operand types for + ("str" and "int") + case _: + ... + +[builtins fixtures/primitives.pyi]