From 315351300e5d1dd258732af2deec258cd405a35e Mon Sep 17 00:00:00 2001 From: Vincent Vanlaer Date: Thu, 25 May 2023 20:56:25 +0200 Subject: [PATCH] Fix match subject ignoring redefinitions Fixes #14746 --- mypy/renaming.py | 1 + test-data/unit/check-python310.test | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/mypy/renaming.py b/mypy/renaming.py index 05b67f41ab85..2fa3ef168a66 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 15454fc3e216..feedd02d82eb 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]