>>> import ast
>>> for node in 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')
Consider the following script:
Running this script on 3.11 gets you this output:
Running this script on CPython
main, however, gets you this output:The reason for this is that the implementation of PEP-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.AsyncFunctionDefandast.ClassDefare all different on 3.12 compared to what they were on 3.11.`__match_args__` attributes on 3.11:
`__match_args__` attributes on 3.12:
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 if
type_paramshad been added as the final item in the__match_args__tuples, but at the moment it comes in second place.Cc. @JelleZijlstra for PEP-695. Also curious if @brandtbucher has any thoughts (for pattern-matching expertise) or @isidentical (for
ast-module expertise).Linked PRs