-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Match CPython's SyntaxError range for unparenthesized except types
#8661
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
Draft
zzarbttoo
wants to merge
1
commit into
RustPython:main
Choose a base branch
from
zzarbttoo:fix/8496-except-as-error-range
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🤖 get_repo_knowledge executed:
get_repo_knowledge RustPython/RustPython /tmp/coderabbit-repo-knowledge/rustpython-rustpython-3b4170e8/learningsLength of output: 11534
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 16691
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 10241
🌐 Web query:
CPython test_syntax.py multiple exception types must be parenthesized when using as end_offset💡 Result:
The CPython error "multiple exception types must be parenthesized" is a syntax error that occurs when legacy Python 2-style exception handling (e.g.,
except Exception, e:) is used in Python 3 [1][2][3]. In Python 3, multiple exception types must be grouped in a tuple using parentheses (e.g.,except (Exception1, Exception2) as e:) [1][2]. Regarding your query aboutasandend_offset: 1. Error Message Evolution: The specific error message "multiple exception types must be parenthesized when using 'as'" was introduced in recent CPython versions to distinguish between plain missing parentheses and incorrect syntax when theaskeyword is present [4]. 2. CPython Testing (test_syntax.py): In the CPython test suite (Lib/test/test_syntax.py), this error is tested by asserting that invalidexceptorexcept*statements raise aSyntaxErrorat specific locations [5][6]. Theend_offsetin these tests helps verify that the error points correctly to the range of the invalid syntax, particularly when complextry/exceptstatements—or combinations ofexceptandexcept*—are involved [6]. 3. Recent Changes: As of Python 3.14 (per PEP 758), it is now permissible to omit parentheses around groups of exceptions inexceptandexcept*clauses, provided that theasbinding is not used [7][8]. If anasbinding is present, parentheses remain mandatory, and the error message specifically reminds the user that they are required "when using 'as'" [4][8]. This error typically arises when running outdated code (often written for Python 2) on a modern Python 3 interpreter [1][3]. To resolve it, you must update the syntax to useasfor exception aliasing and ensure multiple exception types are enclosed in a tuple [1][2].Citations:
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 663
🌐 Web query:
site:github.com/python/cpython Lib/test/test_syntax.py "multiple exception types must be parenthesized when using 'as'" "end_offset"💡 Result:
The error message "multiple exception types must be parenthesized" is a syntax error in Python that occurs when listing multiple exception types in an except clause without enclosing them in parentheses [1]. Historically, Python required parentheses for multiple exceptions in an except statement (e.g., except (ExceptionA, ExceptionB):) [1]. Using a comma-separated list without parentheses was flagged with the SyntaxError: multiple exception types must be parenthesized [1]. More recently, starting in Python 3.14, the language began allowing unparenthesized multiple exception types in certain contexts as part of evolving syntax [2][3]. Because of this, modern static analysis tools like Ruff may flag unparenthesized exceptions as errors if they are configured for older versions of Python (e.g., Python 3.12) that do not yet support the newer, more permissive syntax [3]. The file Lib/test/test_syntax.py in the CPython source code is used to test that these and other syntax errors are correctly identified and raised by the interpreter [4][5]. The "end_offset" in your query likely refers to the column or character range (end_offset) tracked by the Python parser to highlight the specific area where the syntax error occurs, a feature improved in Python 3.10 and later to provide better error feedback [1].
Citations:
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 594
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 1724
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 24692
🌐 Web query:
site:github.com/python/cpython "end_offset" "except A, B, C as"💡 Result:
The term "end_offset" is not a keyword or standard syntax used in combination with "except A, B, C" in programming languages [1][2]. In the context of the Python programming language, "end_offset" is an attribute of the SyntaxError exception class (introduced in Python 3.10) [1][3] and a field used in the dis module for bytecode inspection [4]. It is not related to the "except" statement syntax [1][5]. If you are encountering a syntax error or a specific programming pattern involving "except A, B, C", please note the following regarding Python's exception handling: 1. Handling Multiple Exceptions: To catch multiple exception types, you use a tuple in the except clause [1]: try: # code that may raise exceptions pass except (A, B, C) as e: # handle exceptions A, B, or C pass 2. SyntaxError Context: The "end_offset" attribute is automatically populated by the Python parser when a SyntaxError is raised to indicate where the error ends in the source text [1][6]. It is not something you would typically use in an except clause [1]. If your query refers to a different language or a specific library's domain-specific language (DSL), please provide more context so the appropriate syntax can be identified.
Citations:
Advance
end_offsetpast the colon.invalid_except_stmt_endreturns the 1-based column of:. The caller assigns it directly toSyntaxError.end_offset, whose end column is exclusive. Returncolon - line_start + 2and update the helper documentation.Proposed fix
🤖 Prompt for AI Agents