-
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 PLU002: blank lines before return statement
- Loading branch information
Showing
12 changed files
with
217 additions
and
20 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 |
---|---|---|
@@ -1,16 +1,24 @@ | ||
"""Config class.""" | ||
# pylint: disable=too-few-public-methods | ||
from . import defaults | ||
|
||
|
||
class Config: | ||
"""Plugin configuration class.""" | ||
|
||
def __init__(self, blanks_before_imports: int): | ||
def __init__( | ||
self, | ||
blanks_before_imports: int = defaults.BLANKS_BEFORE_IMPORTS, | ||
blanks_before_return: int = defaults.BLANKS_BEFORE_RETURN, | ||
): | ||
""" | ||
Initialize a `Configuration` instance. | ||
Args: | ||
blanks_before_imports (int): Number of blanks line expected before first | ||
import statements. | ||
blanks_before_return (int): Number of blanks line expected before return | ||
statement. | ||
""" | ||
self.blanks_before_imports = blanks_before_imports | ||
self.blanks_before_return = blanks_before_return |
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,3 +1,4 @@ | ||
"""Default values.""" | ||
|
||
BLANKS_BEFORE_IMPORTS = 0 | ||
BLANKS_BEFORE_RETURN = 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,81 @@ | ||
"""Exception classes raised by various operations within pylint.""" | ||
# pylint: disable=too-few-public-methods | ||
import ast | ||
from typing import Any | ||
|
||
from ..config import Config | ||
from ..exceptions import MultipleStatementsError | ||
from ..problem import Problem | ||
from .base_visitor import BaseVisitor | ||
|
||
|
||
class PLU002Problem(Problem): | ||
"""Problem 002: Number of blank lines before return statement.""" | ||
|
||
code = "PLU002" | ||
format_ = "expected {} blank lines before return statement, 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 `PLU002Problem` instance. | ||
Args: | ||
line_number (int): The line number. | ||
col_offset (int): The column offset. | ||
blanks_actual (int): Number of actual blanks before return statement. | ||
blanks_expected (int): Number of expected blanks before return statement. | ||
""" | ||
message = PLU002Problem.format_.format(blanks_expected, blanks_actual) | ||
super().__init__(line_number, col_offset, message) | ||
|
||
|
||
class PLU002Visitor(BaseVisitor): | ||
"""Visitor class for the PLU002 rule.""" | ||
|
||
def __init__(self, lines: list[str], config: Config): | ||
""" | ||
Initialize a PLU002Visitor instance. | ||
Args: | ||
lines (list[str]): The physical lines. | ||
config (Config): Configuration instance for the plugin and visitor. | ||
""" | ||
self._last_end: int = 0 | ||
super().__init__(lines, config) | ||
|
||
def visit(self, node: ast.AST) -> Any: | ||
""" | ||
Visit the specified node. | ||
Args: | ||
node (ast.AST): The node to visit. | ||
Returns: | ||
Any: The value returned by the base class `visit` method. | ||
""" | ||
self._process_node(node) | ||
return super().visit(node) | ||
|
||
def _process_node(self, node: ast.AST): | ||
if isinstance(node, ast.Return): | ||
try: | ||
actual = self.compute_blanks_before(node) | ||
except MultipleStatementsError: | ||
return | ||
if actual != self.config.blanks_before_return: | ||
problem = PLU002Problem( | ||
node.lineno, | ||
node.col_offset, | ||
actual, | ||
self.config.blanks_before_return, | ||
) | ||
self.problems.append(problem) | ||
elif hasattr(node, "end_lineno") and (node.end_lineno is not None): | ||
self._last_end = node.end_lineno |
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,2 @@ | ||
[tool.black] | ||
extend-exclude="case_files" |
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,3 @@ | ||
def some_func() -> int: | ||
|
||
return 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,41 @@ | ||
[ | ||
{ | ||
"filename": "1_blank.py", | ||
"cases": [ | ||
{ | ||
"expectation": { "blanks_expected": 0 }, | ||
"problems": [{ "line_number": 3, "col_offset": 4, "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": 2, "col_offset": 4, "blanks_actual": 0 }] | ||
} | ||
] | ||
}, | ||
{ | ||
"filename": "print_return_same_line.py", | ||
"cases": [ | ||
{ | ||
"expectation": { "blanks_expected": 0 }, | ||
"problems": [] | ||
}, | ||
{ | ||
"expectation": { "blanks_expected": 1 }, | ||
"problems": [] | ||
} | ||
] | ||
} | ||
] |
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,2 @@ | ||
def some_func() -> int: | ||
return 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,2 @@ | ||
def something(): | ||
print("something");return |
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 `plu002_visitor` module.""" | ||
# pylint: disable=no-self-use,too-few-public-methods | ||
import pytest | ||
|
||
from flake8_plus.config import Config | ||
from flake8_plus.visitors.plu002_visitor import PLU002Problem, PLU002Visitor | ||
|
||
from .util import generate_bulk_cases, generate_results | ||
|
||
|
||
class TestPLU002Visitor: | ||
"""Tests for the `PLU002Visitor` class.""" | ||
|
||
@pytest.mark.parametrize( | ||
("source_code", "expectation", "problems_expected"), | ||
generate_bulk_cases(PLU002Problem), | ||
) | ||
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_return=blanks_expected) | ||
actual = generate_results(PLU002Visitor, config, source_code) | ||
assert actual == problems_expected |