Skip to content

Improvements to verify_pre_check helper for stronger error catching #1181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added a syntax error checker to pre_check
  • Loading branch information
Karl-Michaud committed May 25, 2025
commit 92e2bad8593a7010d488ebc3ebb83ff2d61f30c7
13 changes: 12 additions & 1 deletion python_ta/check/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import pylint.config
import pylint.lint
import pylint.utils
from astroid import MANAGER, modutils
from astroid import MANAGER, exceptions, modutils, parse
from pylint.exceptions import UnknownMessageError
from pylint.lint import PyLinter
from pylint.reporters import BaseReporter, MultiReporter
Expand Down Expand Up @@ -400,6 +400,10 @@ def verify_pre_check(
# trying to disable a check.
if allow_pylint_comments:
return True
with open(os.path.expanduser(filepath)) as f:
contents = f.read()
# Note: astroid.parse will parse the code for any syntax errors. If found, it will raise an AstroidSyntaxError
parse(contents)
with tokenize.open(os.path.expanduser(filepath)) as f:
for tok_type, content, _, _, _ in tokenize.generate_tokens(f.readline):
if tok_type != tokenize.COMMENT:
Expand All @@ -419,6 +423,13 @@ def verify_pre_check(
if on_verify_fail == "raise":
raise
return False
except exceptions.AstroidSyntaxError as e:
logging.error(
f"python_ta could not check your code due to a syntax error in your file: {e}"
)
if on_verify_fail == "raise":
raise e
return False
except tokenize.TokenError as e:
logging.error(
"python_ta could not check your code due to a " + "syntax error in your file."
Expand Down