-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add PLU003: blank lines before except
- Loading branch information
Showing
10 changed files
with
169 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ ast | |
BaseVisitor | ||
config | ||
docstring | ||
ExceptHandler | ||
ParameterSet | ||
PLU | ||
pragma | ||
|
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 |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
|
||
BLANKS_BEFORE_IMPORTS = 0 | ||
BLANKS_BEFORE_RETURN = 0 | ||
BLANKS_BEFORE_EXCEPT = 0 |
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
"""Exception classes raised by various operations within pylint.""" | ||
# pylint: disable=too-few-public-methods | ||
import ast | ||
from typing import Any | ||
|
||
from ..problem import Problem | ||
from .base_visitor import BaseVisitor | ||
|
||
|
||
class PLU003Problem(Problem): | ||
"""Problem 003: Number of blank lines before except.""" | ||
|
||
code = "PLU003" | ||
format_ = "expected {} blank lines before except, found {}" | ||
|
||
def __init__( # pylint: disable=unused-argument | ||
self, | ||
line_number: int, | ||
col_offset: int, | ||
blanks_actual: int, | ||
blanks_expected: int, | ||
**kwargs: dict[str, Any], | ||
): | ||
""" | ||
Initialize a `PLU003Problem` instance. | ||
Args: | ||
line_number (int): The line number. | ||
col_offset (int): The column offset. | ||
blanks_actual (int): Number of actual blanks before except. | ||
blanks_expected (int): Number of expected blanks before except. | ||
""" | ||
message = PLU003Problem.format_.format(blanks_expected, blanks_actual) | ||
super().__init__(line_number, col_offset, message) | ||
|
||
|
||
class PLU003Visitor(BaseVisitor): | ||
"""Visitor class for the PLU003 rule.""" | ||
|
||
def visit_ExceptHandler(self, node: ast.ExceptHandler) -> Any: | ||
""" | ||
Visit a `ExceptHandler` node. | ||
Args: | ||
node (ast.ExceptHandler): The node to visit. | ||
Returns: | ||
Any: The result of calling `generic_visit`. | ||
""" | ||
# pylint: disable=invalid-name | ||
actual = self.compute_blanks_before(node) | ||
if actual != self.config.blanks_before_except: | ||
problem = PLU003Problem( | ||
node.lineno, | ||
node.col_offset, | ||
actual, | ||
self.config.blanks_before_except, | ||
) | ||
self.problems.append(problem) | ||
return self.generic_visit(node) |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
try: | ||
print(1 / 0) | ||
|
||
except ZeroDivisionError: | ||
pass |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
[ | ||
{ | ||
"filename": "1_blank.py", | ||
"cases": [ | ||
{ | ||
"expectation": { "blanks_expected": 0 }, | ||
"problems": [{ "line_number": 4, "col_offset": 0, "blanks_actual": 1 }] | ||
}, | ||
{ | ||
"expectation": { "blanks_expected": 1 }, | ||
"problems": [] | ||
} | ||
] | ||
}, | ||
{ | ||
"filename": "no_blanks.py", | ||
"cases": [ | ||
{ | ||
"expectation": { "blanks_expected": 0 }, | ||
"problems": [] | ||
}, | ||
{ | ||
"expectation": { "blanks_expected": 1 }, | ||
"problems": [{ "line_number": 3, "col_offset": 0, "blanks_actual": 0 }] | ||
} | ||
] | ||
} | ||
] |
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
try: | ||
print(1 / 0) | ||
except ZeroDivisionError: | ||
pass |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
"""Tests for the `plu003_visitor` module.""" | ||
# pylint: disable=no-self-use,too-few-public-methods | ||
import pytest | ||
|
||
from flake8_plus.config import Config | ||
from flake8_plus.visitors.plu003_visitor import PLU003Problem, PLU003Visitor | ||
|
||
from .util import generate_bulk_cases, generate_results | ||
|
||
|
||
class TestPLU003Visitor: | ||
"""Tests for the `PLU003Visitor` class.""" | ||
|
||
@pytest.mark.parametrize( | ||
("source_code", "expectation", "problems_expected"), | ||
generate_bulk_cases(PLU003Problem), | ||
) | ||
def test_bulk( | ||
self, source_code: str, expectation: dict[str, int], problems_expected: set[str] | ||
): | ||
"""Run bulk test cases.""" | ||
blanks_expected = expectation["blanks_expected"] | ||
config = Config(blanks_before_except=blanks_expected) | ||
actual = generate_results(PLU003Visitor, config, source_code) | ||
assert actual == problems_expected |