-
-
Notifications
You must be signed in to change notification settings - Fork 388
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
163 additions
and
43 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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 |
---|---|---|
@@ -1,33 +1,28 @@ | ||
import ast | ||
|
||
from typing_extensions import final | ||
from typing import final | ||
|
||
from wemake_python_styleguide.violations import complexity | ||
from wemake_python_styleguide.visitors.base import BaseNodeVisitor | ||
from wemake_python_styleguide.visitors.decorators import alias | ||
|
||
|
||
@final | ||
@alias( | ||
'visit_match', | ||
('visit_Match',), | ||
) | ||
class MatchSubjectsVisitor(BaseNodeVisitor): | ||
"""Finds excessive match subjects in `match` statements.""" | ||
|
||
def visit_match(self, node: ast.Match) -> None: | ||
def visit_Match(self, node: ast.Match) -> None: | ||
"""Finds all `match` statements and checks their subjects.""" | ||
self._check_match_subjects_count(node) | ||
self.generic_visit(node) | ||
|
||
def _check_match_subjects_count(self, node: ast.Match) -> None: | ||
if isinstance(node.subject, ast.Tuple): | ||
elts = node.subject.elts | ||
if len(elts) > self.options.max_match_subjects: | ||
self.add_violation( | ||
complexity.TooManyMatchSubjectsViolation( | ||
node, | ||
text=str(len(elts)), | ||
baseline=self.options.max_match_subjects, | ||
) | ||
) | ||
if not isinstance(node.subject, ast.Tuple): | ||
return | ||
if len(node.subject.elts) <= self.options.max_match_subjects: | ||
return | ||
self.add_violation( | ||
complexity.TooManyMatchSubjectsViolation( | ||
node, | ||
text=str(len(node.subject.elts)), | ||
baseline=self.options.max_match_subjects, | ||
) | ||
) |