Skip to content

Commit b568909

Browse files
committed
Refactor function verb check and move utility functions
1 parent 1348502 commit b568909

5 files changed

Lines changed: 26 additions & 33 deletions

File tree

src/community_of_python_flake8_plugin/checks/function_verb.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,6 @@ def validate_function_name(
8282
):
8383
return
8484

85-
if len(ast_node.name) < 3: # noqa: PLR2004 # Short names are likely acronyms or special cases
86-
return
87-
8885
self.violations.append(
8986
Violation(
9087
line_number=ast_node.lineno,

src/community_of_python_flake8_plugin/checks/module_import_many_names.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,25 @@
44
from importlib import util as importlib_util
55

66
from community_of_python_flake8_plugin import constants
7-
from community_of_python_flake8_plugin.utils import check_module_has_all_declaration
87
from community_of_python_flake8_plugin.violation_codes import ViolationCodes
98
from community_of_python_flake8_plugin.violations import Violation
109

1110

11+
def check_module_has_all_declaration(module_node: ast.Module) -> bool:
12+
for statement in module_node.body:
13+
if isinstance(statement, ast.Assign) and any(
14+
isinstance(target, ast.Name) and target.id == "__all__" for target in statement.targets
15+
):
16+
return True
17+
if (
18+
isinstance(statement, ast.AnnAssign)
19+
and isinstance(statement.target, ast.Name)
20+
and statement.target.id == "__all__"
21+
):
22+
return True
23+
return False
24+
25+
1226
def check_module_path_exists(module_name: str) -> bool:
1327
try:
1428
return importlib_util.find_spec(module_name) is not None

src/community_of_python_flake8_plugin/checks/scalar_annotation.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import typing
44

55
from community_of_python_flake8_plugin.constants import SCALAR_ANNOTATIONS
6-
from community_of_python_flake8_plugin.utils import find_parent_class_definition, find_parent_function
6+
from community_of_python_flake8_plugin.utils import find_parent_class_definition
77
from community_of_python_flake8_plugin.violation_codes import ViolationCodes
88
from community_of_python_flake8_plugin.violations import Violation
99

@@ -36,9 +36,18 @@ def check_is_scalar_annotation(annotation_node: ast.AST) -> bool:
3636
return False
3737

3838

39+
def find_parent_function(syntax_tree: ast.AST, target_node: ast.AST) -> ast.FunctionDef | ast.AsyncFunctionDef | None:
40+
for potential_parent in ast.walk(syntax_tree):
41+
if isinstance(potential_parent, (ast.FunctionDef, ast.AsyncFunctionDef)):
42+
for child_node in ast.walk(potential_parent): # noqa: COP007
43+
if child_node is target_node:
44+
return potential_parent
45+
return None
46+
47+
3948
@typing.final
4049
class ScalarAnnotationCheck(ast.NodeVisitor):
41-
def __init__(self, tree: ast.AST) -> None: # noqa: COP004G
50+
def __init__(self, tree: ast.AST) -> None: # noqa: COP004G
4251
self.violations: list[Violation] = []
4352
self.syntax_tree: typing.Final[ast.AST] = tree
4453

src/community_of_python_flake8_plugin/plugin.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ def run(self) -> Iterable[tuple[int, int, str, type[object]]]: # noqa: COP004F
3232
checks_collection: typing.Final[list[PluginCheckProtocol]] = []
3333

3434
for _, module_name, _ in pkgutil.iter_modules(checks_module.__path__):
35-
if module_name == "__init__":
36-
continue
37-
3835
module_full_name = f"{checks_module.__name__}.{module_name}" # noqa: COP007
3936
imported_module = importlib.import_module(module_full_name)
4037

src/community_of_python_flake8_plugin/utils.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,10 @@
22
import ast
33

44

5-
def check_module_has_all_declaration(module_node: ast.Module) -> bool:
6-
for statement in module_node.body:
7-
if isinstance(statement, ast.Assign) and any(
8-
isinstance(target, ast.Name) and target.id == "__all__" for target in statement.targets
9-
):
10-
return True
11-
if (
12-
isinstance(statement, ast.AnnAssign)
13-
and isinstance(statement.target, ast.Name)
14-
and statement.target.id == "__all__"
15-
):
16-
return True
17-
return False
18-
19-
205
def find_parent_class_definition(syntax_tree: ast.AST, target_node: ast.AST) -> ast.ClassDef | None:
216
for potential_parent in ast.walk(syntax_tree):
227
if isinstance(potential_parent, ast.ClassDef):
238
for child_node in ast.walk(potential_parent): # noqa: COP007
249
if child_node is target_node:
2510
return potential_parent
2611
return None
27-
28-
29-
def find_parent_function(syntax_tree: ast.AST, target_node: ast.AST) -> ast.FunctionDef | ast.AsyncFunctionDef | None:
30-
for potential_parent in ast.walk(syntax_tree):
31-
if isinstance(potential_parent, (ast.FunctionDef, ast.AsyncFunctionDef)):
32-
for child_node in ast.walk(potential_parent): # noqa: COP007
33-
if child_node is target_node:
34-
return potential_parent
35-
return None

0 commit comments

Comments
 (0)