]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-134718: Fix ast.dump() for empty non-default values (GH-134926)
authorSerhiy Storchaka <storchaka@gmail.com>
Fri, 30 May 2025 14:25:07 +0000 (17:25 +0300)
committerGitHub <noreply@github.com>
Fri, 30 May 2025 14:25:07 +0000 (17:25 +0300)
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 b9791bf52d3e08a20060ea7022f5da9641a19fb9..2f11683ecf7c681d4c76cb0c234587370aea08d5 100644 (file)
@@ -147,18 +147,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 46745cfa8f832560f50c9a33b7ef773331a87e36..59263012bc144019ef1eb8db8cf4186ab71a3a41 100644 (file)
@@ -1543,18 +1543,42 @@ 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='')",
             full="Constant(value='')",
         )
 
+        check_node(
+            ast.Interpolation(value=ast.Constant(42), str=None, conversion=-1),
+            empty="Interpolation(value=Constant(value=42), str=None, conversion=-1)",
+            full="Interpolation(value=Constant(value=42), str=None, conversion=-1)",
+        )
+
+        check_node(
+            ast.Interpolation(value=ast.Constant(42), str=[], conversion=-1),
+            empty="Interpolation(value=Constant(value=42), str=[], conversion=-1)",
+            full="Interpolation(value=Constant(value=42), str=[], conversion=-1)",
+        )
+
         check_text(
             "def a(b: int = 0, *, c): ...",
             empty="Module(body=[FunctionDef(name='a', args=arguments(args=[arg(arg='b', annotation=Name(id='int', ctx=Load()))], kwonlyargs=[arg(arg='c')], kw_defaults=[None], defaults=[Constant(value=0)]), body=[Expr(value=Constant(value=Ellipsis))])])",
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.