-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpython_linter.py
More file actions
57 lines (44 loc) · 1.82 KB
/
python_linter.py
File metadata and controls
57 lines (44 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import ast
import os
class PythonLinter(ast.NodeVisitor):
def __init__(self):
self.errors = []
def visit_ClassDef(self, node):
# Rule 1: Check class names for CapWords convention
if not node.name[0].isupper():
self.errors.append(f"Class name '{node.name}' should start with a capital letter. Line: {node.lineno}")
self.generic_visit(node)
def visit_FunctionDef(self, node):
# Rule 1: Check function names for snake_case convention
if "_" not in node.name:
self.errors.append(f"Function name '{node.name}' should be in snake_case. Line: {node.lineno}")
self.generic_visit(node)
def visit_Import(self, node):
# Rule 3: Check for unused imports
for alias in node.names:
module_name = alias.name.split('.')[0]
if not any(alias.name in line for line in self.code_lines[node.lineno - 1:]):
self.errors.append(f"Unused import: '{alias.name}'. Line: {node.lineno}")
self.generic_visit(node)
def visit_ImportFrom(self, node):
# Rule 3: Check for unused imports
for alias in node.names:
if not any(alias.name in line for line in self.code_lines[node.lineno - 1:]):
self.errors.append(f"Unused import: '{alias.name}'. Line: {node.lineno}")
self.generic_visit(node)
def lint_python_code(code):
tree = ast.parse(code)
linter = PythonLinter()
linter.code_lines = code.split('\n')
linter.visit(tree)
return linter.errors
if __name__ == "__main__":
with open("trivia.py", "r") as python_file:
python_code = python_file.read()
errors = lint_python_code(python_code)
if errors:
print("Linting failed:")
for error in errors:
print(error)
else:
print("Linting passed.")