-
-
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
Open
mbyrnepr2
wants to merge
5
commits into
pylint-dev:main
Choose a base branch
from
mbyrnepr2:7128_remaining_patterns_unreachable
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+121
β0
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0e5ef7c
Add ``match-statements`` checker and the following message:
mbyrnepr2 a4c6cf8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e54d1ec
Remove the trailing `/` from the url.
mbyrnepr2 4a742e8
Remove the unnecessary logic which checks if the version of Python isβ¦
mbyrnepr2 6488491
Remove unnecessary `.rc` file from the functional test.
mbyrnepr2 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
red = 0 | ||
green = 1 | ||
blue = 2 | ||
|
||
|
||
color = blue | ||
match color: | ||
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 :(") |
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,17 @@ | ||
from enum import Enum | ||
|
||
|
||
class Color(Enum): | ||
RED = 0 | ||
GREEN = 1 | ||
BLUE = 2 | ||
|
||
|
||
color = Color.BLUE | ||
match color: | ||
case Color.RED: | ||
print("I see red!") | ||
case Color.GREEN: | ||
print("Grass is green") | ||
case Color.BLUE: | ||
print("I'm feeling the blues :(") |
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 @@ | ||
- `PEP 636 <https://peps.python.org/pep-0636/#matching-against-constants-and-enums>`_ |
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,6 @@ | ||
Add ``match-statements`` checker and the following message: | ||
``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 #7128 |
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,59 @@ | ||
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html | ||
# For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE | ||
# Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt | ||
|
||
"""Match statement checker for Python code.""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from astroid import nodes | ||
|
||
from pylint.checkers import BaseChecker | ||
from pylint.checkers.utils import only_required_for_messages | ||
from pylint.interfaces import HIGH | ||
|
||
if TYPE_CHECKING: | ||
from pylint.lint import PyLinter | ||
|
||
|
||
class MatchStatementChecker(BaseChecker): | ||
name = "match_statements" | ||
msgs = { | ||
"E5000": ( | ||
"The name capture `case %s` makes the remaining patterns unreachable. " | ||
"Use a dotted name(for example an enum) to fix this", | ||
"unreachable-match-patterns", | ||
"Emitted when a name capture pattern in a match statement is used " | ||
"and there are case statements below it.", | ||
) | ||
} | ||
|
||
def open(self) -> None: | ||
py_version = self.linter.config.py_version | ||
self._py310_plus = py_version >= (3, 10) | ||
|
||
@only_required_for_messages("unreachable-match-patterns") | ||
def visit_match(self, node: nodes.Match) -> None: | ||
"""Check if a name capture pattern prevents the other cases from being | ||
reached. | ||
""" | ||
for idx, case in enumerate(node.cases): | ||
if ( | ||
isinstance(case.pattern, nodes.MatchAs) | ||
and case.pattern.pattern is None | ||
and isinstance(case.pattern.name, nodes.AssignName) | ||
and idx < len(node.cases) - 1 | ||
and self._py310_plus | ||
): | ||
self.add_message( | ||
"unreachable-match-patterns", | ||
node=case, | ||
args=case.pattern.name.name, | ||
confidence=HIGH, | ||
) | ||
|
||
|
||
def register(linter: PyLinter) -> None: | ||
linter.register_checker(MatchStatementChecker(linter)) |
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,15 @@ | ||
"""Functional tests for the ``unreachable-match-patterns`` message""" | ||
|
||
|
||
a = 'a' | ||
b = 'b' | ||
s = 'a' | ||
|
||
|
||
match s: | ||
case a: # [unreachable-match-patterns] | ||
pass | ||
case b: # [unreachable-match-patterns] | ||
pass | ||
case s: | ||
pass |
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,2 @@ | ||
[testoptions] | ||
min_pyver=3.10 |
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,2 @@ | ||
unreachable-match-patterns:10:0:None:None::The name capture `case a` makes the remaining patterns unreachable. Use a dotted name(for example an enum) to fix this:HIGH | ||
unreachable-match-patterns:12:0:None:None::The name capture `case b` makes the remaining patterns unreachable. Use a dotted name(for example an enum) to fix this:HIGH |
Oops, something went wrong.
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.
Why check the version? Shouldn't this always be checked if we find these patterns?
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.
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?