Skip to content

Commit

Permalink
fix, feat: Raises error is python file has a Syntax error. Add proper…
Browse files Browse the repository at this point in the history
… Error logging for Syntax error in project files
  • Loading branch information
Emmo00 committed Nov 19, 2023
1 parent c284d98 commit edb1a50
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
9 changes: 8 additions & 1 deletion alxcheck/checks/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
print_no_function_docstring,
print_no_class_docstring,
print_check_docstrings,
print_error_parsing_file,
)


Expand Down Expand Up @@ -35,8 +36,14 @@ def check_module_function_class_documentation(file_path):
content = ""
else:
content = content.split(b"\n", 1)[1]
tree = ast.parse(content)
tree = None
try:
tree = ast.parse(content)
except Exception:
print_error_parsing_file(file_path)
try:
if tree is None:
return
for node in ast.walk(tree):
# check module docstring
if isinstance(node, ast.Module):
Expand Down
16 changes: 16 additions & 0 deletions alxcheck/utils/error_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,19 @@ def print_var_was_used(file_path):

def print_check_docstrings(file_path):
print(Fore.RED + f"Error: Check docstrings in {file_path}" + Fore.RESET)


def print_error_parsing_file(file_path):
import ast

try:
with open(file_path, "r") as f:
ast.parse(f.read())
except SyntaxError as syntax_error:
print(
Fore.RED
+ f"SyntaxError\n\tFile: {file_path}\n\tLine: {syntax_error.lineno}\tMessage: {syntax_error.msg}"
+ Fore.RESET
)
except Exception as e:
print(Fore.RED + f"Error Parsing File:\n\t{type(e)}" + Fore.RESET)

0 comments on commit edb1a50

Please sign in to comment.