-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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
PEP-695: Potentially breaking changes made to __match_args__
attributes of AST nodes
#104799
Comments
Nice catch. Even though the AST has murky stability guarantees, I agree that the right thing to do here is to move the new arguments to the "end" (in the AST def, which generates both the constructor and |
Submitted a PR. There is still a compatibility break when you create a FunctionDef node. Consider this code: import ast
a = ast.arguments([], [], None, [], [], None, [])
f = ast.FunctionDef("hello", a, [ast.Pass()], [])
mod = ast.Module([f], [])
mod.lineno = mod.col_offset = 0
ast.fix_missing_locations(mod)
compile(mod, "x", "exec") On 3.11 this works. On main I get:
I'm not sure this is fixable without bigger changes to how we generate the AST classes. |
I suspect of 2 breaking changes match args vs building ast nodes, the latter is more common. Especially as matching ast node I would expect to commonly use node visitor. It does produce a good number of hits This is small fix to upgrade as I’d guess most of time empty type params is fix. And unsure AST typical compatibility expectation as seems like any new attribute would trigger this. Were there similar ast incompatibilities in last couple minor releases? |
#104834 is a suggested way to restore compatibility for creation of AST nodes. It's more invasive, though, so not sure it's a good idea. |
@hmc-cs-mdrissi, pattern-matching works very well in combination with an
^EDIT: That rationale is all misguided; @JelleZijlstra corrected me below. My preference would be:
|
That's not true. The AST node constructors accept almost anything, you only get errors later when you use the node, and then only if you're lucky.
|
Oh, sorry. I misread/misunderstood your previous comment. My bad — that seems worse, in that case :) But my opinion would still be "definitely do the easier thing, maybe also do the more invasive thing as well". |
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Seems like the fix changes the ABI. Please comment on #104974 if you have insights into whether we should change the ABI here. |
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
…onGH-104834) (cherry picked from commit 77d2579) Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com> Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
I think this is now fixed. Thanks @JelleZijlstra! |
Not sure if I should make a new issue for this as it seems related. While #104834 seems to have fixed the issue for compile and presumably match, The original version with compile now works, but using import ast
a = ast.arguments([], [], None, [], [], None, [])
f = ast.FunctionDef("hello", a, [ast.Pass()], [])
mod = ast.Module([f], [])
mod.lineno = mod.col_offset = 0
ast.fix_missing_locations(mod)
print(ast.unparse(mod)) 3.11.4
3.12.0b2 (3.12-dev from this morning also)
Adding |
Thanks @DavidCEllis, fixing this in #105846. |
…onGH-105846) (cherry picked from commit 957a974) Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Should be fixed now following #105846 — @DavidCEllis let us know if anything else isn't working as you'd expect! |
* main: pythongh-104799: PEP 695 backward compatibility for ast.unparse (python#105846) pythongh-105834: Add tests for calling `issubclass()` between two protocols (python#105835) CI: Remove docs build from Azure Pipelines (python#105823) pythongh-105844: Consistently use 'minor version' for X.Y versions (python#105851) Fix inaccuracies in "Assorted Topics" section of "Defining Extension Types" tutorial (python#104969) pythongh-105433: Add `pickle` tests for PEP695 (python#105443) bpo-44530: Document the change in MAKE_FUNCTION behavior (python#93189) pythonGH-103124: Multiline statement support for pdb (pythonGH-103125) pythonGH-105588: Add missing error checks to some obj2ast_* converters (pythonGH-105589)
I ran into this as a bunch of tests that were fine against some of the alphas were failing on the first beta. Looking up the error message took me to the first patch. Waiting and retesting on 3.12.0b2 had most of them passing again apart from the ones that used unparse. It looks like they all now pass against a new 3.12-dev build - Thanks @JelleZijlstra and @AlexWaygood ! |
Thanks! You might be interested in #105858 which will hopefully provide a more general solution; let me know on that issue if you have any feedback. |
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.AsyncFunctionDef
andast.ClassDef
are 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_params
had 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
The text was updated successfully, but these errors were encountered: