Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Python: Add support for PEP-758 exception syntax#20990

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
tausbn wants to merge13 commits intomain
base:main
Choose a base branch
Loading
fromtausbn/python-support-relaxed-exception-groups

Conversation

@tausbn
Copy link
Contributor

@tausbntausbn commentedDec 8, 2025
edited
Loading

This PR is stacked on top of#20708, so that one should be reviewed first.


Seehttps://peps.python.org/pep-0758/ for more details.

We implement this by extending the syntax for exceptions and exception groups so that thetype field can now contain either an expression (which matches the old behaviour), or a comma-separated list of at least two elements (representing the new behaviour).

We model the latter case using a new node typeexception_list, which intsg-python is simply mapped to a tuple. This means it matches the existing behaviour (when the tuple is surrounded by parentheses) exactly, hence we don't need to change any other code.

As a consequence of this, however, we cannot directly parse the Python 2.7 syntaxexcept Foo, e: ... asexcept Foo as e: ..., as this would introduce an ambiguity in the grammar. Thus, we have removed support for the (deprecated) 2.7-style syntax, and only allowas to indicate binding of the exception. The syntaxexcept Foo, e: ... continues to be parsed (in particular, it's not suddenly a syntax error), but it will be parsed as if it wereexcept (Foo, e): ..., which may not give the correct results.

In principle we could extend the QL libraries to account for this case (specifically when analysing Python 2 code). In practice, however, I expect this to have a minor impact on results, and not worth the additional investment at this time.

- Extends the scanner with a new token kind representing the start of atemplate string. This is used to distinguish template strings fromregular strings (because only a template string will start with a`_template_string_start` external token).- Cleans up the logic surrounding interpolations (and the method names)so that format strings and template strings behave the same in thiscase.Finally, we add two new node types in the tree-sitter grammar:- `template_string` behaves like format strings, but is a distinct type(mainly so that an implicit concatenation between template strings andregular strings becomes a syntax error).- `concatenated_template_string` is the counterpart of`concatenated_string`.However, internally, the string parts of a template strings are just thesame `string_content` nodes that are used in regular format strings. Wewill disambiguate these inside `tsg-python`.
Adds three new AST nodes to the mix:- `TemplateString` represents a t-string in Python 3.14- `TemplateStringPart` represents one of the string constituents of at-string. (The interpolated expressions are represented as `Expr` nodes,just like f-strings.)- `JoinedTemplateString` represents an implicit concatenation oftemplate strings.Importantly, we _completely avoid_ the complicated construction wecurrently do for format strings (as well as the confusing nomenclature).No extra injection of empty strings (so that a template string is astrict alternation of strings and expressions). A `JoinedTemplateString`simply has a list of template string children, and a `TemplateString`has a list of "values" which may be either `Expr` or`TemplateStringPart` nodes.If we ever find that we actually want the more complicated interface forthese strings, then I would much rather we reconstruct this inside of QLrather than in the parser.
We do the usual thing. Downgrade scripts remove the relevant relations;upgrade scripts do nothing.
Not actually based on any measurements, just the usual 100/1000 stuff.
Seehttps://peps.python.org/pep-0758/ for more details.We implement this by extending the syntax for exceptions and exceptiongroups so that the `type` field can now contain either an expression(which matches the old behaviour), or a comma-separated list of at leasttwo elements (representing the new behaviour).We model the latter case using a new node type `exception_list`, whichin `tsg-python` is simply mapped to a tuple. This means it matches theexisting behaviour (when the tuple is surrounded by parentheses)exactly, hence we don't need to change any other code.As a consequence of this, however, we cannot directly parse the Python2.7 syntax `except Foo, e: ...` as `except Foo as e: ...`, as this wouldintroduce an ambiguity in the grammar. Thus, we have removed support forthe (deprecated) 2.7-style syntax, and only allow `as` to indicatebinding of the exception. The syntax `except Foo, e: ...` continues tobe parsed (in particular, it's not suddenly a syntax error), but it willbe parsed as if it were `except (Foo, e): ...`, which may not give thecorrect results.In principle we could extend the QL libraries to account for this case(specifically when analysing Python 2 code). In practice, however, Iexpect this to have a minor impact on results, and not worth theadditional investment at this time.
Note in particular that the `exceptions.py` test is unaffected.
@tausbntausbnforce-pushed thetausbn/python-support-relaxed-exception-groups branch from545241a to7f98e1eCompareDecember 16, 2025 22:58
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

Copilot code reviewCopilotAwaiting requested review from CopilotCopilot will automatically review once the pull request is marked ready for review

At least 1 approving review is required to merge this pull request.

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

@tausbn

[8]ページ先頭

©2009-2025 Movatter.jp