-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add unreachable-match-patterns
message and new checker.
#10424
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: main
Are you sure you want to change the base?
Add unreachable-match-patterns
message and new checker.
#10424
Conversation
Codecov ReportAll modified and coverable lines are covered by tests β
Additional details and impacted files@@ Coverage Diff @@
## main #10424 +/- ##
=======================================
Coverage 95.86% 95.87%
=======================================
Files 175 177 +2
Lines 19074 19153 +79
=======================================
+ Hits 18286 18363 +77
- Misses 788 790 +2
π New features to boost your workflow:
|
``unreachable-match-patterns``. This will emit an error message when a name capture pattern is used in a match statement which would make the remaining patterns unreachable. This code is a SyntaxError at runtime. Closes pylint-dev#7128
f1ca097
to
0e5ef7c
Compare
remaining-patterns-unreachable
checker.unreachable-match-patterns
message and new checker.
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.
Nice check!
and case.pattern.pattern is None | ||
and isinstance(case.pattern.name, nodes.AssignName) | ||
and idx < len(node.cases) - 1 | ||
and self._py310_plus |
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.
Why check the version? Shouldn't this always be checked if we find these patterns?
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.
Why check the version?
My mistake. We shouldn't check it. I must have been thinking that this line would prevent an error from occurring if Pylint was run in an environment using Python 3.9 or lower since the match statement (and the ast.Match node) is only available in Python 3.10 and higher.
The solution would be to remove this line and wait until Python 3.9 is end of life before considering a merge?
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.
Seems very sensible to me!
Match Statements checker Messages | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
:unreachable-match-patterns (E5000): *The name capture `case %s` makes the remaining patterns unreachable. Use a dotted name(for example an enum) to fix this* | ||
Emitted when a name capture pattern in a match statement is used and there |
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.
Emitted when a name capture pattern in a match statement is used and there | |
Emitted when a name capture pattern is used in a match statement and there |
msgs = { | ||
"E5000": ( | ||
"The name capture `case %s` makes the remaining patterns unreachable. " | ||
"Use a dotted name(for example an enum) to fix this", |
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.
"Use a dotted name(for example an enum) to fix this", | |
"Use a dotted name (for example an enum) to fix this.", |
case red: # [unreachable-match-patterns] | ||
print("I see red!") | ||
case green: # [unreachable-match-patterns] | ||
print("Grass is green") | ||
case blue: | ||
print("I'm feeling the blues :(") |
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 a little torn, but I guess I would expect to see this message on the second and third cases, not the first and second, since it's the second and third that are unreachable. We don't know if the fix is to alter the name capture or to remove the extra cases.
Type of Changes
Description
This will emit an error message when a name capture pattern is used in a match statement which would make the remaining patterns unreachable. This code is a SyntaxError at runtime.
Closes #7128