Skip to content

gh-98278: Highlight both mismatched brackets in SyntaxError - #162071

Open
Badi-Rodriguez wants to merge 4 commits into
python:mainfrom
Badi-Rodriguez:gh-98278-bracket-carets
Open

Badi-Rodriguez wants to merge 4 commits into
python:mainfrom
Badi-Rodriguez:gh-98278-bracket-carets

Conversation

@Badi-Rodriguez

Copy link
Copy Markdown

Summary

Improve the display of SyntaxError for mismatched brackets on the same source line by highlighting both the unmatched opening delimiter and the mismatched closing delimiter, as requested in gh-98278.

  • Pass the opening delimiter's character column from the tokenizer through a private _opening_offset attribute on the raised SyntaxError.
  • Render two carets in traceback output, including colorized output and source lines containing Unicode characters of different display widths.
  • Preserve the original exception message and closing delimiter's primary SyntaxError.offset.
  • Keep existing single-caret behavior for mismatched brackets spanning multiple lines.
  • Add regression tests and a NEWS entry.

Tests

Local validation completed prior to opening this PR:

  • CPython tests: 1,010 passed, 5 skipped.
  • patchcheck passed.
  • git diff --check passed.

Issue: gh-98278.

@python-cla-bot

python-cla-bot Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@ZeroIntensity ZeroIntensity added the AI-needs-explanation AI-assisted work that needs further explanation and follow up from the contributor label Nov 16, 2026
@bedevere-app

bedevere-app Bot commented Nov 16, 2026

Copy link
Copy Markdown

Thank you for your interest in contributing to Python. This is a community-driven project, and we want it to be a welcoming place to learn and collaborate.

Contributors are responsible for reviewing and understanding what they submit, checking its claims, and being able to explain changes in their own words. This helps reviewers focus on the contribution and leaves more time to support other contributors.

This contribution appears to have been authored with AI assistance. Please explain your contribution in your own words and describe how you checked its correctness.

Please read our guidelines for using AI tools.

@Badi-Rodriguez

Copy link
Copy Markdown
Author

Thank you for the feedback. I'd like to explain how I approached this issue.

The original issue states that syntax errors caused by mismatched brackets currently use a caret to point to the closing bracket. However, sometimes the opening bracket is the incorrect one, so the issue proposes highlighting both locations.

I am taking a compilers course, and based on what I've learned there, I figured this problem was most likely related to the lexing and tokenization process. I started looking through the components associated with that part and found the helper functions responsible for constructing SyntaxError exceptions.

I also noticed that the lexer uses a stack to keep track of opening brackets and match them against closing brackets. My initial idea was therefore to retrieve the position of the opening bracket, which was already being recorded, and somehow pass that information to the traceback formatter so that both locations could be highlighted.

However, while I had a general idea of what needed to change, I found it difficult to navigate the conventions and existing implementation patterns. I was particularly concerned about accidentally breaking existing functionality in the tokenizer or the error-reporting helpers while making changes to the lexer.

So after an initial draft of the functionality and a few minor code changes, I used ChatGPT to compare my ideas and proposed changes against the existing implementation, aiming to keep the modifications minimal and focused. After personally reviewing and confirming the proposed changes, I also used AI assistance to refine the tests and prepare the initial pull request description and submission, based on my own notes and initial draft.

The resulting implementation introduces a helper that preserves the opening bracket's position as an additional private attribute, _opening_offset, while keeping the existing SyntaxError.offset and error message unchanged.

The traceback formatter then uses this information to display two carets when the mismatched brackets are on the same line. This avoids modifying the parser's grammar or changing how the actual mismatch is detected.

Regarding correctness, I added regression tests covering different cases, including nested brackets, Unicode characters, f-strings, and colorized traceback output. I also checked that mismatched brackets occurring on different lines retained their previous behavior. During local validation, the test run reported 1,010 tests passed and 5 skipped. The patchcheck and git diff --check checks also passed. Additionally, the automated Tests and Lint workflows on the pull request completed successfully.

I recognize that the current implementation has limitations, particularly because the additional caret is only displayed when both mismatched brackets are on the same line. I decided to keep this behavior limited in scope to avoid interfering with existing multiline error reporting. This was the approach I considered most appropriate based on my initial reasoning and the examples provided in the issue.

I'm open to suggestions about the design and willing to revise the changes if there is a more appropriate way to implement this functionality.

I appreciate the opportunity to contribute and learn from the project. Thank you for your time.

@ZeroIntensity ZeroIntensity removed the AI-needs-explanation AI-assisted work that needs further explanation and follow up from the contributor label Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants