]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-118851: Default ctx arguments to AST constructors to Load() (#118854)
authorJelle Zijlstra <jelle.zijlstra@gmail.com>
Thu, 9 May 2024 22:30:14 +0000 (15:30 -0700)
committerGitHub <noreply@github.com>
Thu, 9 May 2024 22:30:14 +0000 (15:30 -0700)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Doc/library/ast.rst
Doc/whatsnew/3.13.rst
Lib/test/test_ast.py
Misc/NEWS.d/next/Library/2024-05-09-08-46-12.gh-issue-118851.aPAoJw.rst [new file with mode: 0644]
Parser/asdl_c.py
Python/Python-ast.c

index 24c56f17ebb002e68a0a2e4a4da15b8ee1981a97..d4ccf282a5d00adf6887a94af9af9a1788639f3a 100644 (file)
@@ -120,7 +120,8 @@ Node classes
 
    If a field that is optional in the grammar is omitted from the constructor,
    it defaults to ``None``. If a list field is omitted, it defaults to the empty
-   list. If any other field is omitted, a :exc:`DeprecationWarning` is raised
+   list. If a field of type :class:`!ast.expr_context` is omitted, it defaults to
+   :class:`Load() <ast.Load>`. If any other field is omitted, a :exc:`DeprecationWarning` is raised
    and the AST node will not have this field. In Python 3.15, this condition will
    raise an error.
 
@@ -596,8 +597,7 @@ Expressions
    * ``keywords`` holds a list of :class:`.keyword` objects representing
      arguments passed by keyword.
 
-   When creating a ``Call`` node, ``args`` and ``keywords`` are required, but
-   they can be empty lists.
+   The ``args`` and ``keywords`` arguments are optional and default to empty lists.
 
    .. doctest::
 
index 44555718184e19cf590d3e4e6aee2b1603815c16..9dab458b21009351cb773d7fbb1f43ca83c37dd6 100644 (file)
@@ -521,8 +521,10 @@ ast
 
   If an optional field on an AST node is not included as an argument when
   constructing an instance, the field will now be set to ``None``. Similarly,
-  if a list field is omitted, that field will now be set to an empty list.
-  (Previously, in both cases, the attribute would be missing on the newly
+  if a list field is omitted, that field will now be set to an empty list,
+  and if a :class:`!ast.expr_context` field is omitted, it defaults to
+  :class:`Load() <ast.Load>`.
+  (Previously, in all cases, the attribute would be missing on the newly
   constructed AST node instance.)
 
   If other arguments are omitted, a :exc:`DeprecationWarning` is emitted.
@@ -534,7 +536,7 @@ ast
   unless the class opts in to the new behavior by setting the attribute
   :attr:`ast.AST._field_types`.
 
-  (Contributed by Jelle Zijlstra in :gh:`105858` and :gh:`117486`.)
+  (Contributed by Jelle Zijlstra in :gh:`105858`, :gh:`117486`, and :gh:`118851`.)
 
 * :func:`ast.parse` now accepts an optional argument *optimize*
   which is passed on to the :func:`compile` built-in. This makes it
index f6e22d44406d9ee0bb7d8deef7638292b9163bf4..5422c861ffb5c0d1e880f8a3168e1a5281a4d320 100644 (file)
@@ -3036,6 +3036,23 @@ class ASTConstructorTests(unittest.TestCase):
         self.assertEqual(node.name, 'foo')
         self.assertEqual(node.decorator_list, [])
 
+    def test_expr_context(self):
+        name = ast.Name("x")
+        self.assertEqual(name.id, "x")
+        self.assertIsInstance(name.ctx, ast.Load)
+
+        name2 = ast.Name("x", ast.Store())
+        self.assertEqual(name2.id, "x")
+        self.assertIsInstance(name2.ctx, ast.Store)
+
+        name3 = ast.Name("x", ctx=ast.Del())
+        self.assertEqual(name3.id, "x")
+        self.assertIsInstance(name3.ctx, ast.Del)
+
+        with self.assertWarnsRegex(DeprecationWarning,
+                                   r"Name\.__init__ missing 1 required positional argument: 'id'"):
+            name3 = ast.Name()
+
     def test_custom_subclass_with_no_fields(self):
         class NoInit(ast.AST):
             pass
diff --git a/Misc/NEWS.d/next/Library/2024-05-09-08-46-12.gh-issue-118851.aPAoJw.rst b/Misc/NEWS.d/next/Library/2024-05-09-08-46-12.gh-issue-118851.aPAoJw.rst
new file mode 100644 (file)
index 0000000..d036d0c
--- /dev/null
@@ -0,0 +1,2 @@
+``ctx`` arguments to the constructors of :mod:`ast` node classes now default
+to :class:`ast.Load() <ast.Load>`. Patch by Jelle Zijlstra.
index 11d59faeb0d42ceb1c163d132a203a03b1ca0da8..9961d23629abc580e1f772970e3289614c8ba334 100755 (executable)
@@ -1022,6 +1022,13 @@ ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
                     goto set_remaining_cleanup;
                 }
             }
+            else if (type == state->expr_context_type) {
+                // special case for expr_context: default to Load()
+                res = PyObject_SetAttr(self, name, state->Load_singleton);
+                if (res < 0) {
+                    goto set_remaining_cleanup;
+                }
+            }
             else {
                 // simple field (e.g., identifier)
                 if (PyErr_WarnFormat(
index 4956d04f719de9933e1d021eee9ad7a7ba187d1f..7aa1c5119d8f2837298a0f9ad4288e4e70a0a271 100644 (file)
@@ -5221,6 +5221,13 @@ ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
                     goto set_remaining_cleanup;
                 }
             }
+            else if (type == state->expr_context_type) {
+                // special case for expr_context: default to Load()
+                res = PyObject_SetAttr(self, name, state->Load_singleton);
+                if (res < 0) {
+                    goto set_remaining_cleanup;
+                }
+            }
             else {
                 // simple field (e.g., identifier)
                 if (PyErr_WarnFormat(