Skip to content

Commit d811584

Browse files
committed
bpo-43521: Allow ast.unparse with empty sets and NaN
1 parent 6086ae7 commit d811584

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

Lib/ast.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,8 +1199,13 @@ def _write_docstring(self, node):
11991199

12001200
def _write_constant(self, value):
12011201
if isinstance(value, (float, complex)):
1202-
# Substitute overflowing decimal literal for AST infinities.
1203-
self.write(repr(value).replace("inf", _INFSTR))
1202+
# Substitute overflowing decimal literal for AST infinities,
1203+
# and inf - inf for NaNs.
1204+
self.write(
1205+
repr(value)
1206+
.replace("inf", _INFSTR)
1207+
.replace("nan", f"({_INFSTR}-{_INFSTR})")
1208+
)
12041209
elif self._avoid_backslashes and isinstance(value, str):
12051210
self._write_str_avoiding_backslashes(value)
12061211
else:
@@ -1273,10 +1278,13 @@ def visit_IfExp(self, node):
12731278
self.traverse(node.orelse)
12741279

12751280
def visit_Set(self, node):
1276-
if not node.elts:
1277-
raise ValueError("Set node should have at least one item")
1278-
with self.delimit("{", "}"):
1279-
self.interleave(lambda: self.write(", "), self.traverse, node.elts)
1281+
if node.elts:
1282+
with self.delimit("{", "}"):
1283+
self.interleave(lambda: self.write(", "), self.traverse, node.elts)
1284+
else:
1285+
# `{}` would be interpreted as a dictionary literal, and
1286+
# `set` might be shadowed. Thus:
1287+
self.write('{*()}')
12801288

12811289
def visit_Dict(self, node):
12821290
def write_key_value_pair(k, v):

Lib/test/test_unparse.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,12 @@ def test_huge_float(self):
199199
self.check_ast_roundtrip("1e1000j")
200200
self.check_ast_roundtrip("-1e1000j")
201201

202+
def test_nan(self):
203+
self.assertASTEqual(
204+
ast.parse(ast.unparse(ast.Constant(value=float('nan')))),
205+
ast.parse('1e1000 - 1e1000')
206+
)
207+
202208
def test_min_int(self):
203209
self.check_ast_roundtrip(str(-(2 ** 31)))
204210
self.check_ast_roundtrip(str(-(2 ** 63)))
@@ -252,6 +258,12 @@ def test_annotations(self):
252258
def test_set_literal(self):
253259
self.check_ast_roundtrip("{'a', 'b', 'c'}")
254260

261+
def test_empty_set(self):
262+
self.assertASTEqual(
263+
ast.parse(ast.unparse(ast.Set(elts=[]))),
264+
ast.parse('{*()}')
265+
)
266+
255267
def test_set_comprehension(self):
256268
self.check_ast_roundtrip("{x for x in range(5)}")
257269

@@ -326,9 +338,6 @@ def test_invalid_fstring_conversion(self):
326338
def test_invalid_fstring_backslash(self):
327339
self.check_invalid(ast.FormattedValue(value=ast.Constant(value="\\\\")))
328340

329-
def test_invalid_set(self):
330-
self.check_invalid(ast.Set(elts=[]))
331-
332341
def test_invalid_yield_from(self):
333342
self.check_invalid(ast.YieldFrom(value=None))
334343

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
``ast.unparse`` can now render NaNs and empty sets.

0 commit comments

Comments
 (0)