-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Add flag to raise error if match statement does not match exaustively #19144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 18 commits
02796d9
3aaa98b
6b613f5
3341b4b
a186cc8
ec55c81
8ce5b6a
3581ec9
0f9ed6a
62e4089
abf7e98
6a525f7
7dc7c01
3ab9848
1a3e359
fecd37d
525924f
f798b43
9b200ac
681a327
d219871
914bff4
4cc25fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -468,6 +468,10 @@ If we forget to handle one of the cases, mypy will generate an error: | |||||||||
assert_never(direction) # E: Argument 1 to "assert_never" has incompatible type "Direction"; expected "NoReturn" | ||||||||||
|
||||||||||
Exhaustiveness checking is also supported for match statements (Python 3.10 and later). | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this could be expanded instead of adding a new paragraph:
Suggested change
(not sure that's better than your current approach now that I write it out, actually) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you are right here 👍 |
||||||||||
For match statements specifically, inexhaustive matches can be caught | ||||||||||
without needing to use ``assert_never`` by using | ||||||||||
:option:`--enable-error-code exhaustive-match <mypy --enable-error-code>`. | ||||||||||
|
||||||||||
|
||||||||||
Extra Enum checks | ||||||||||
***************** | ||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5452,6 +5452,7 @@ def visit_match_stmt(self, s: MatchStmt) -> None: | |
inferred_types = self.infer_variable_types_from_type_maps(type_maps) | ||
|
||
# The second pass narrows down the types and type checks bodies. | ||
unmatched_types: TypeMap = None | ||
for p, g, b in zip(s.patterns, s.guards, s.bodies): | ||
current_subject_type = self.expr_checker.narrow_type_from_binder( | ||
named_subject, subject_type | ||
|
@@ -5508,6 +5509,11 @@ def visit_match_stmt(self, s: MatchStmt) -> None: | |
else: | ||
self.accept(b) | ||
self.push_type_map(else_map, from_assignment=False) | ||
unmatched_types = else_map | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it work as intended when typemaps contain more than immediate targets? For example, what happens in the following fork of an existing test from typing import Literal, TypedDict
class A(TypedDict):
tag: Literal["a"]
name: str
class B(TypedDict):
tag: Literal["b"]
num: int
d: A | B
match d["tag"]:
case "a":
reveal_type(d) # N: Revealed type is "TypedDict('__main__.A', {'tag': Literal['a'], 'name': builtins.str})"
reveal_type(d["name"]) # N: Revealed type is "builtins.str" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would say that yes, it is behaving as I originally intended with 2 diagnostics. Added a test My thinking of having multiple diagnostics is it might be a bit easier to read/understand in the case of longer unions, but I don't have a very strong opinion here either way |
||
|
||
if unmatched_types is not None: | ||
for typ in set(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 | ||
# after the match. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if the others have duplicated docs but if not could you just link from one of these sections to the other?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on feedback below, I moved to only be error code based.
Means the duplication in the CLI command doc page is removed.