[Python-checkins] bpo-39158: ast.literal_eval() doesn't support empty sets (GH-17742)

Raymond Hettinger webhook-mailer at python.org
Fri Jan 3 00:21:27 EST 2020


https://github.com/python/cpython/commit/4fcf5c12a37a8d3d8d6303c44c223dbc8d568cfd
commit: 4fcf5c12a37a8d3d8d6303c44c223dbc8d568cfd
branch: master
author: Raymond Hettinger <rhettinger at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2020-01-02T22:21:18-07:00
summary:

bpo-39158: ast.literal_eval() doesn't support empty sets (GH-17742)

files:
A Misc/NEWS.d/next/Library/2019-12-29-15-44-38.bpo-39158.cxVoOR.rst
M Doc/library/ast.rst
M Lib/ast.py
M Lib/test/test_ast.py

diff --git a/Doc/library/ast.rst b/Doc/library/ast.rst
index 190d9286effb0..c380a81bee6d2 100644
--- a/Doc/library/ast.rst
+++ b/Doc/library/ast.rst
@@ -194,6 +194,9 @@ and classes for traversing abstract syntax trees:
    .. versionchanged:: 3.2
       Now allows bytes and set literals.
 
+   .. versionchanged:: 3.9
+      Now supports creating empty sets with ``'set()'``.
+
 
 .. function:: get_docstring(node, clean=True)
 
diff --git a/Lib/ast.py b/Lib/ast.py
index ece8b139e460e..495c0d618f12c 100644
--- a/Lib/ast.py
+++ b/Lib/ast.py
@@ -83,6 +83,9 @@ def _convert(node):
             return list(map(_convert, node.elts))
         elif isinstance(node, Set):
             return set(map(_convert, node.elts))
+        elif (isinstance(node, Call) and isinstance(node.func, Name) and
+              node.func.id == 'set' and node.args == node.keywords == []):
+            return set()
         elif isinstance(node, Dict):
             return dict(zip(map(_convert, node.keys),
                             map(_convert, node.values)))
diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py
index 51a7c1af1ffe7..55b91cfa23bec 100644
--- a/Lib/test/test_ast.py
+++ b/Lib/test/test_ast.py
@@ -891,6 +891,7 @@ def test_literal_eval(self):
         self.assertEqual(ast.literal_eval('(True, False, None)'), (True, False, None))
         self.assertEqual(ast.literal_eval('{1, 2, 3}'), {1, 2, 3})
         self.assertEqual(ast.literal_eval('b"hi"'), b"hi")
+        self.assertEqual(ast.literal_eval('set()'), set())
         self.assertRaises(ValueError, ast.literal_eval, 'foo()')
         self.assertEqual(ast.literal_eval('6'), 6)
         self.assertEqual(ast.literal_eval('+6'), 6)
diff --git a/Misc/NEWS.d/next/Library/2019-12-29-15-44-38.bpo-39158.cxVoOR.rst b/Misc/NEWS.d/next/Library/2019-12-29-15-44-38.bpo-39158.cxVoOR.rst
new file mode 100644
index 0000000000000..c41799bebaeb9
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-12-29-15-44-38.bpo-39158.cxVoOR.rst
@@ -0,0 +1 @@
+ast.literal_eval() now supports empty sets.



More information about the Python-checkins mailing list