]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-134718: Fix ast.dump() for empty non-default values (GH-134926) (GH-134936)
authorSerhiy Storchaka <storchaka@gmail.com>
Fri, 30 May 2025 15:48:48 +0000 (18:48 +0300)
committerGitHub <noreply@github.com>
Fri, 30 May 2025 15:48:48 +0000 (15:48 +0000)
(cherry picked from commit cc344e8dd0a6fdc83a032c229f9b3cf53f76a887)

Lib/ast.py
Lib/test/test_ast/test_ast.py
Misc/NEWS.d/next/Library/2025-05-30-13-07-29.gh-issue-134718.9Qvhxn.rst [new file with mode: 0644]

index 1a88d23c585202410e464e099dfe78bb72babe32..37b20206b8af4fda67c631fd010eda0008985511 100644 (file)
@@ -151,18 +151,16 @@ def dump(
                 if value is None and getattr(cls, name, ...) is None:
                     keywords = True
                     continue
-                if (
-                    not show_empty
-                    and (value is None or value == [])
-                    # Special cases:
-                    # `Constant(value=None)` and `MatchSingleton(value=None)`
-                    and not isinstance(node, (Constant, MatchSingleton))
-                ):
-                    args_buffer.append(repr(value))
-                    continue
-                elif not keywords:
-                    args.extend(args_buffer)
-                    args_buffer = []
+                if not show_empty:
+                    if value == []:
+                        field_type = cls._field_types.get(name, object)
+                        if getattr(field_type, '__origin__', ...) is list:
+                            if not keywords:
+                                args_buffer.append(repr(value))
+                            continue
+                    if not keywords:
+                        args.extend(args_buffer)
+                        args_buffer = []
                 value, simple = _format(value, level)
                 allsimple = allsimple and simple
                 if keywords:
index db2ea9f546eb1a8be32fb84a3a1ae713a1504688..7cedb13e923547be4ffef0e46c78e5fdb7709d5b 100644 (file)
@@ -1346,12 +1346,24 @@ Module(
             full="MatchSingleton(value=None)",
         )
 
+        check_node(
+            ast.MatchSingleton(value=[]),
+            empty="MatchSingleton(value=[])",
+            full="MatchSingleton(value=[])",
+        )
+
         check_node(
             ast.Constant(value=None),
             empty="Constant(value=None)",
             full="Constant(value=None)",
         )
 
+        check_node(
+            ast.Constant(value=[]),
+            empty="Constant(value=[])",
+            full="Constant(value=[])",
+        )
+
         check_node(
             ast.Constant(value=""),
             empty="Constant(value='')",
diff --git a/Misc/NEWS.d/next/Library/2025-05-30-13-07-29.gh-issue-134718.9Qvhxn.rst b/Misc/NEWS.d/next/Library/2025-05-30-13-07-29.gh-issue-134718.9Qvhxn.rst
new file mode 100644 (file)
index 0000000..922ab16
--- /dev/null
@@ -0,0 +1,2 @@
+:func:`ast.dump` now only omits ``None`` and ``[]`` values if they are
+default values.