Skip to content

fix: handle SyntaxError gracefully in ast_analyzer#742

Open
jyotish6699 wants to merge 1 commit into
imDarshanGK:mainfrom
jyotish6699:fix-ast-syntax-error
Open

fix: handle SyntaxError gracefully in ast_analyzer#742
jyotish6699 wants to merge 1 commit into
imDarshanGK:mainfrom
jyotish6699:fix-ast-syntax-error

Conversation

@jyotish6699
Copy link
Copy Markdown

Description

This PR fixes a bug in the AST analyzer where invalid Python source code causes analyze() to raise a SyntaxError instead of returning a structured analyzer issue.

The analyzer currently crashes when malformed Python code is passed to ast.parse(). This behavior interrupts analysis and requires callers to handle unexpected exceptions.

With this change, syntax errors are handled gracefully and returned through the analyzer's existing issue-reporting system.

Changes Made

  • Added SyntaxError handling around ast.parse()
  • Return a structured analyzer issue when parsing fails
  • Added test coverage for syntax-error handling
  • Preserved existing analyzer behavior for valid Python code

Example

Before

analyze(
"""
def f(:
    pass
"""
)

Result:

SyntaxError: invalid syntax

After

The analyzer returns a structured issue describing the syntax error instead of crashing.

Related Issue

Fixes #741

Testing

Added:

  • test_analyze_handles_syntax_error

Verified:

  • Invalid Python syntax returns a structured analyzer issue
  • Existing analyzer functionality remains unchanged
  • Tests pass successfully

Notes

This issue is different from #466.

Issue #466 focuses on adding test coverage for existing AST analyzer behaviors, while #741 addresses a separate bug where invalid Python syntax can cause the analyzer to raise an unhandled exception and terminate analysis.

This PR implements the bug fix and corresponding test coverage for Issue #741.

@jyotish6699 jyotish6699 requested a review from imDarshanGK as a code owner May 31, 2026 12:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: ast_analyzer crashes on invalid Python syntax instead of returning a structured issue

1 participant