-
-
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
02796d9
Add check for exhaustive match statements
Don-Burns 3aaa98b
Add flag for exhaustive match statements
Don-Burns 6b613f5
Update docs for exhaustive match statements
Don-Burns 3341b4b
Add flag to mypy_primer
Don-Burns a186cc8
Revert "Add flag to mypy_primer"
Don-Burns ec55c81
Move tests to 3.10 file so only run on >=3.10
Don-Burns 8ce5b6a
Rename flag to better follow other flags
Don-Burns 3581ec9
Fix doc gen error
Don-Burns 0f9ed6a
Set default True for CI run, will revert before merge
Don-Burns 62e4089
Revert "Set default True for CI run, will revert before merge"
Don-Burns abf7e98
Add explicit error code - change [misc] -> [exhaustive-match]
Don-Burns 6a525f7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7dc7c01
Add missed docs
Don-Burns 3ab9848
fixup! Add missed docs
Don-Burns 1a3e359
fixup! fixup! Add missed docs
Don-Burns fecd37d
Change to error code only for exhaustive match
Don-Burns 525924f
Merge doc literal paragraph
Don-Burns f798b43
Empty commit to trigger CI
Don-Burns 9b200ac
Apply suggestions to remove whitespace from tests
Don-Burns 681a327
fixup! Apply suggestions to remove whitespace from tests
Don-Burns d219871
Add test for more complex narrowing use case
Don-Burns 914bff4
fixup! Add test for more complex narrowing use case
Don-Burns 4cc25fe
Merge branch 'master' into feat/exhaustive-match
Don-Burns da2752a
Change test for readability
Don-Burns 3eaa5ad
error message change, use notes
hauntsaninja 23afed2
tweak docs
hauntsaninja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Add check for exhaustive match statements
- Loading branch information
commit 02796d944b0144535a7029e18947c49ca45c161b
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
[case testExhaustiveMatchNoFlag] | ||
# flags: --python-version 3.12 | ||
a: int = 5 | ||
match a: | ||
case 1: | ||
pass | ||
case _: | ||
pass | ||
|
||
b: str = "hello" | ||
match b: | ||
case "bye": | ||
pass | ||
case _: | ||
pass | ||
|
||
[case testNonExhaustiveMatchNoFlag] | ||
# flags: --python-version 3.12 | ||
a: int = 5 | ||
match a: | ||
case 1: | ||
pass | ||
|
||
b: str = "hello" | ||
match b: | ||
case "bye": | ||
pass | ||
|
||
|
||
[case testExhaustiveMatchWithFlag] | ||
# flags: --python-version 3.12 --only-allow-exhaustive-match-statements | ||
a: int = 5 | ||
match a: | ||
case 1: | ||
pass | ||
case _: | ||
pass | ||
|
||
b: str = "hello" | ||
match b: | ||
case "bye": | ||
pass | ||
case _: | ||
pass | ||
|
||
[case testNonExhaustiveMatchWithFlag] | ||
# flags: --python-version 3.12 --only-allow-exhaustive-match-statements | ||
a: int = 5 | ||
match a: # E: Cases within match statement do not exhaustively handle all values: "int". If not intended to handle all cases, use `case _: pass` | ||
case 1: | ||
pass | ||
|
||
b: str = "hello" | ||
match b: # E: Cases within match statement do not exhaustively handle all values: "str". If not intended to handle all cases, use `case _: pass` | ||
case "bye": | ||
pass | ||
|
||
[case testEnumNonExhaustiveWithFlag] | ||
# flags: --python-version 3.12 --only-allow-exhaustive-match-statements | ||
|
||
import enum | ||
|
||
class Color(enum.Enum): | ||
RED = 1 | ||
BLUE = 2 | ||
GREEN = 3 | ||
|
||
val: Color = Color.RED | ||
|
||
match val: # E: Cases within match statement do not exhaustively handle all values: "Literal[Color.GREEN]". If not intended to handle all cases, use `case _: pass` | ||
case Color.RED: | ||
a = "red" | ||
case Color.BLUE: | ||
a= "blue" | ||
|
||
[builtins fixtures/enum.pyi] | ||
|
||
|
||
[case testEnumExhaustiveWithFlag] | ||
# flags: --python-version 3.12 --only-allow-exhaustive-match-statements | ||
|
||
import enum | ||
|
||
class Color(enum.Enum): | ||
RED = 1 | ||
BLUE = 2 | ||
|
||
val: Color = Color.RED | ||
|
||
match val: | ||
case Color.RED: | ||
a = "red" | ||
case Color.BLUE: | ||
a= "blue" | ||
|
||
[builtins fixtures/enum.pyi] | ||
|
||
[case testEnumMultipleMissingMatchesWithFlag] | ||
# flags: --python-version 3.12 --only-allow-exhaustive-match-statements | ||
|
||
import enum | ||
|
||
class Color(enum.Enum): | ||
RED = 1 | ||
BLUE = 2 | ||
GREEN = 3 | ||
|
||
val: Color = Color.RED | ||
|
||
match val: # E: Cases within match statement do not exhaustively handle all values: "Literal[Color.BLUE, Color.GREEN]". If not intended to handle all cases, use `case _: pass` | ||
case Color.RED: | ||
a = "red" | ||
|
||
|
||
[builtins fixtures/enum.pyi] | ||
|
||
[case testEnumFallbackWithFlag] | ||
# flags: --python-version 3.12 --only-allow-exhaustive-match-statements | ||
|
||
import enum | ||
|
||
class Color(enum.Enum): | ||
RED = 1 | ||
BLUE = 2 | ||
GREEN = 3 | ||
|
||
val: Color = Color.RED | ||
|
||
match val: | ||
case Color.RED: | ||
a = "red" | ||
case _: | ||
a = "other" | ||
|
||
|
||
[builtins fixtures/enum.pyi] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Does it work as intended when typemaps contain more than immediate targets?
For example, what happens in the following fork of an existing test
testMatchNarrowingUnionTypedDictViaIndex
with this flag enabled? If I remember the surrounding logic correctly, there will be two typemap entries - ford['tag']
and ford
, so two diagnostics?Uh oh!
There was an error while loading. Please reload this page.
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 would say that yes, it is behaving as I originally intended with 2 diagnostics. Added a test
testExhaustiveMatchNarrowingUnionTypedDictViaIndex
for the use case you have above.d219871
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