From b63ffd81199e41c69d7b1930755ced8be945725f Mon Sep 17 00:00:00 2001 From: STerliakov Date: Sat, 6 Sep 2025 22:10:22 +0200 Subject: [PATCH] Do not report exhaustive-match after deferral --- mypy/checker.py | 4 ++-- test-data/unit/check-python310.test | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/mypy/checker.py b/mypy/checker.py index ba821df621e5..5843148d4b4e 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -5682,8 +5682,8 @@ def visit_match_stmt(self, s: MatchStmt) -> None: self.push_type_map(else_map, from_assignment=False) unmatched_types = else_map - if unmatched_types is not None: - for typ in list(unmatched_types.values()): + if unmatched_types is not None and not self.current_node_deferred: + for typ in unmatched_types.values(): self.msg.match_statement_inexhaustive_match(typ, s) # This is needed due to a quirk in frame_context. Without it types will stay narrowed diff --git a/test-data/unit/check-python310.test b/test-data/unit/check-python310.test index 5c495d2ed863..7d76c09b6151 100644 --- a/test-data/unit/check-python310.test +++ b/test-data/unit/check-python310.test @@ -2975,3 +2975,18 @@ val: int = 8 match val: case FOO: # E: Cannot assign to final name "FOO" pass + +[case testMatchExhaustivenessWithDeferral] +# flags: --enable-error-code exhaustive-match +from typing import Literal +import unknown_module # E: Cannot find implementation or library stub for module named "unknown_module" \ + # N: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports + +def foo(e: Literal[0, 1]) -> None: + match e: + case 0: + defer + case 1: + ... + +defer = unknown_module.foo