Title: PEP-695: Potentially breaking changes made to `__match_args__` attributes of AST nodes · Issue #104799 · python/cpython · GitHub
Open Graph Title: PEP-695: Potentially breaking changes made to `__match_args__` attributes of AST nodes · Issue #104799 · python/cpython
X Title: PEP-695: Potentially breaking changes made to `__match_args__` attributes of AST nodes · Issue #104799 · python/cpython
Description: Consider the following script: import ast def test(node): match node: case ast.FunctionDef("foo", ast.arguments(args=[ast.arg("bar")])): print('matched! :)') case _: print("Didn't match :(") source = ast.parse("def foo(bar): pass") node ...
Open Graph Description: Consider the following script: import ast def test(node): match node: case ast.FunctionDef("foo", ast.arguments(args=[ast.arg("bar")])): print('matched! :)') case _: print("Didn't match :(") source...
X Description: Consider the following script: import ast def test(node): match node: case ast.FunctionDef("foo", ast.arguments(args=[ast.arg("bar")])): print('matched! :)') case _: pri...
Opengraph URL: https://github.com/python/cpython/issues/104799
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"PEP-695: Potentially breaking changes made to `__match_args__` attributes of AST nodes","articleBody":"Consider the following script:\r\n\r\n```py\r\nimport ast\r\n\r\ndef test(node):\r\n match node:\r\n case ast.FunctionDef(\"foo\", ast.arguments(args=[ast.arg(\"bar\")])):\r\n print('matched! :)')\r\n case _:\r\n print(\"Didn't match :(\")\r\n\r\n\r\nsource = ast.parse(\"def foo(bar): pass\")\r\nnode = source.body[0]\r\nassert isinstance(node, ast.FunctionDef)\r\ntest(node)\r\n```\r\n\r\nRunning this script on 3.11 gets you this output:\r\n\r\n```\r\n\u003epython repro.py\r\nmatched! :)\r\n```\r\n\r\nRunning this script on CPython `main`, however, gets you this output:\r\n\r\n```\r\n\u003epython repro.py\r\nDidn't match :(\r\n```\r\n\r\nThe 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 of `ast.FunctionDef`, `ast.AsyncFunctionDef` and `ast.ClassDef` are all different on 3.12 compared to what they were on 3.11.\r\n\r\n\u003cdetails\u003e\r\n\u003csummary\u003e`__match_args__` attributes on 3.11:\u003c/summary\u003e\r\n\r\n```pycon\r\n\u003e\u003e\u003e import ast\r\n\u003e\u003e\u003e for node in ast.ClassDef, ast.FunctionDef, ast.AsyncFunctionDef:\r\n... print(node.__match_args__)\r\n...\r\n('name', 'bases', 'keywords', 'body', 'decorator_list')\r\n('name', 'args', 'body', 'decorator_list', 'returns', 'type_comment')\r\n('name', 'args', 'body', 'decorator_list', 'returns', 'type_comment')\r\n```\r\n\r\n\u003c/details\u003e\r\n\r\n\u003cdetails\u003e\r\n\u003csummary\u003e`__match_args__` attributes on 3.12:\u003c/summary\u003e\r\n\r\n```pycon\r\n\u003e\u003e\u003e import ast\r\n\u003e\u003e\u003e for node in ast.ClassDef, ast.FunctionDef, ast.AsyncFunctionDef:\r\n... print(node.__match_args__)\r\n...\r\n('name', 'type_params', 'bases', 'keywords', 'body', 'decorator_list')\r\n('name', 'type_params', 'args', 'body', 'decorator_list', 'returns', 'type_comment')\r\n('name', 'type_params', 'args', 'body', 'decorator_list', 'returns', 'type_comment')\r\n```\r\n\r\n\u003c/details\u003e\r\n\r\nThis 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.\r\n\r\nCc. @JelleZijlstra for PEP-695. Also curious if @brandtbucher has any thoughts (for pattern-matching expertise) or @isidentical (for `ast`-module expertise).\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-104828\n* gh-104834\n* gh-104974\n* gh-105213\n* gh-105846\n* gh-105862\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/AlexWaygood","@type":"Person","name":"AlexWaygood"},"datePublished":"2023-05-23T14:36:24.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":14},"url":"https://github.com/104799/cpython/issues/104799"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:74f5105f-9479-e922-b098-c129c709ca45 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | ED34:38BF1A:10347DE:15AD33F:69699324 |
| html-safe-nonce | 62443e0f579e3cf774fd162936c09c02f026f40315c4733df188ad7a8c292cea |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFRDM0OjM4QkYxQToxMDM0N0RFOjE1QUQzM0Y6Njk2OTkzMjQiLCJ2aXNpdG9yX2lkIjoiMzcxMjYxODg4ODMzMTYyOTM0OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 9c2b18b3d90ca4b957ed6ee866a873470df67d2a795d07945987591531143fb0 |
| hovercard-subject-tag | issue:1722216622 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/104799/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ed3370e4ce30dc0015f9fcdd9c8c25aa163d29cda23d82af5e9b78f9484d3fca/python/cpython/issues/104799 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ed3370e4ce30dc0015f9fcdd9c8c25aa163d29cda23d82af5e9b78f9484d3fca/python/cpython/issues/104799 |
| og:image:alt | Consider the following script: import ast def test(node): match node: case ast.FunctionDef("foo", ast.arguments(args=[ast.arg("bar")])): print('matched! :)') case _: print("Didn't match :(") source... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | AlexWaygood |
| hostname | github.com |
| expected-hostname | github.com |
| None | 3542e147982176a7ebaa23dfb559c8af16f721c03ec560c68c56b64a0f35e751 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | af80af7cc9e3de9c336f18b208a600950a3c187c |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width