1
1
import ast
2
2
import pycodestyle
3
3
4
+
4
5
class CodeReviewer :
5
6
def __init__ (self ):
6
7
self .feedback = []
@@ -27,10 +28,12 @@ def _check_indentation(self, tree):
27
28
for node in ast .walk (tree ):
28
29
if isinstance (node , ast .FunctionDef ):
29
30
if node .body and not isinstance (node .body [0 ], ast .Expr ):
30
- self .feedback .append (f"Function '{ node .name } ' should have a docstring or 'pass' statement." )
31
+ self .feedback .append (
32
+ f"Function '{ node .name } ' should have a docstring or 'pass' statement." )
31
33
elif isinstance (node , (ast .For , ast .While , ast .If , ast .With )):
32
34
if not isinstance (node .body [0 ], ast .Expr ):
33
- self .feedback .append (f"Indentation Error: Missing 'pass' statement for '{ ast .dump (node )} '." )
35
+ self .feedback .append (
36
+ f"Indentation Error: Missing 'pass' statement for '{ ast .dump (node )} '." )
34
37
35
38
def _check_undefined_vars (self , tree ):
36
39
undefined_vars = set ()
@@ -47,19 +50,22 @@ def _check_code_style(self, code):
47
50
style_guide = pycodestyle .StyleGuide ()
48
51
result = style_guide .check_code (code )
49
52
if result .total_errors :
50
- self .feedback .append ("Code style issues found. Please check and fix them." )
53
+ self .feedback .append (
54
+ "Code style issues found. Please check and fix them." )
51
55
52
56
def _check_comments (self , code ):
53
57
lines = code .split ('\n ' )
54
58
for i , line in enumerate (lines ):
55
59
if line .strip ().startswith ('#' ):
56
60
# Check for empty comments or comments without space after '#'
57
61
if len (line .strip ()) == 1 or line .strip ()[1 ] != ' ' :
58
- self .feedback .append (f"Improve comment style in line { i + 1 } : '{ line .strip ()} '" )
62
+ self .feedback .append (
63
+ f"Improve comment style in line { i + 1 } : '{ line .strip ()} '" )
59
64
60
65
def get_feedback (self ):
61
66
return self .feedback
62
67
68
+
63
69
if __name__ == "__main__" :
64
70
# Example Python code to analyze
65
71
python_code = """
0 commit comments