Repository navigation
gh-98278: Highlight both mismatched brackets in SyntaxError - #162071
Badi-Rodriguez wants to merge 4 commits into
Conversation
|
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. |
|
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 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 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. |
Summary
Improve the display of
SyntaxErrorfor mismatched brackets on the same source line by highlighting both the unmatched opening delimiter and the mismatched closing delimiter, as requested in gh-98278._opening_offsetattribute on the raisedSyntaxError.tracebackoutput, including colorized output and source lines containing Unicode characters of different display widths.SyntaxError.offset.Tests
Local validation completed prior to opening this PR:
patchcheckpassed.git diff --checkpassed.Issue: gh-98278.