Skip to content

Python: Fail early on syntax errors - #22723

Merged
tausbn merged 1 commit into
mainfrom
tausbn/python-fail-early-on-syntax-errors
Oct 2, 2026
Merged

tausbn merged 1 commit into
mainfrom
tausbn/python-fail-early-on-syntax-errors

Conversation

@tausbn

@tausbn tausbn commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

Previously, we would only signal a syntax error when we created a SyntaxErrorNode from the tsg-python output. However, this meant that we might accidentally try to construct nodes based on bogus input (since if the file has a syntax error, then all bets are off).

Instead, we now propagate the syntax error as soon as we encounter it (immediately after reading the output of tsg-python).

Previously, we would only signal a syntax error when we created a
SyntaxErrorNode from the tsg-python output. However, this meant that we
might accidentally try to construct nodes based on bogus input (since if
the file has a syntax error, then all bets are off).

Instead, we now propagate the syntax error as soon as we encounter it
(immediately after reading the output of tsg-python).
@tausbn tausbn added the no-change-note-required This PR does not need a change note label Oct 1, 2026
@github-actions github-actions Bot added the Python label Oct 1, 2026
@tausbn
tausbn marked this pull request as ready for review October 1, 2026 15:27
@tausbn
tausbn requested review from a team as code owners October 1, 2026 15:27
Copilot AI balanced review requested due to automatic review settings October 1, 2026 15:27

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The implementation preserves existing error details while preventing AST construction from invalid parser output.

Review effort: Balanced
Findings: None

What changed in this PR

Moves tree-sitter syntax-error detection ahead of AST construction.

Changes:

  • Raises SyntaxError while decoding TSG output.
  • Adds a regression test validating error location data.
File Description
python/​extractor/​semmle/​python/​parser/​tsg_parser.py Detects syntax errors before constructing nodes.
python/​extractor/​tests/​test_tsg_parser.py Tests early failure and location propagation.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@andersfugmann andersfugmann left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved.
Consider if the class SyntaxErrorNode could be deleted, now that it's never constructed.

@tausbn
tausbn merged commit a3db94d into main Oct 2, 2026
14 checks passed
@tausbn
tausbn deleted the tausbn/python-fail-early-on-syntax-errors branch October 2, 2026 11:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-change-note-required This PR does not need a change note Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants