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

PEP-695: Potentially breaking changes made to__match_args__ attributes of AST nodes #104799

Closed
Assignees
JelleZijlstra
Labels
3.12only security fixes3.13bugs and security fixestype-bugAn unexpected behavior, bug, or error
@AlexWaygood

Description

@AlexWaygood

Consider the following script:

importastdeftest(node):matchnode:caseast.FunctionDef("foo",ast.arguments(args=[ast.arg("bar")])):print('matched! :)')case _:print("Didn't match :(")source=ast.parse("def foo(bar): pass")node=source.body[0]assertisinstance(node,ast.FunctionDef)test(node)

Running this script on 3.11 gets you this output:

>python repro.pymatched! :)

Running this script on CPythonmain, however, gets you this output:

>python repro.pyDidn't match :(

The reason for this is that the implementation ofPEP-695 (new in Python 3.12) added a number of new AST nodes to Python, and as a result, the__match_args__ attributes ofast.FunctionDef,ast.AsyncFunctionDef andast.ClassDef are all different on 3.12 compared to what they were on 3.11.

`__match_args__` attributes on 3.11:
>>>import ast>>>for nodein ast.ClassDef, ast.FunctionDef, ast.AsyncFunctionDef:...print(node.__match_args__)...('name', 'bases', 'keywords', 'body', 'decorator_list')('name', 'args', 'body', 'decorator_list', 'returns', 'type_comment')('name', 'args', 'body', 'decorator_list', 'returns', 'type_comment')
`__match_args__` attributes on 3.12:
>>>import ast>>>for nodein ast.ClassDef, ast.FunctionDef, ast.AsyncFunctionDef:...print(node.__match_args__)...('name', 'type_params', 'bases', 'keywords', 'body', 'decorator_list')('name', 'type_params', 'args', 'body', 'decorator_list', 'returns', 'type_comment')('name', 'type_params', 'args', 'body', 'decorator_list', 'returns', 'type_comment')

This feels like it has the potential to be quite a breaking change for people using pattern-matching to parse ASTs. It would probably be okay iftype_params had been added as the final item in the__match_args__ tuples, but at the moment it comes in second place.

Cc.@JelleZijlstra forPEP-695. Also curious if@brandtbucher has any thoughts (for pattern-matching expertise) or@isidentical (forast-module expertise).

Linked PRs

Metadata

Metadata

Assignees

Labels

3.12only security fixes3.13bugs and security fixestype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions


    [8]ページ先頭

    ©2009-2025 Movatter.jp