Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e8e3419

Browse files
bpo-43521: Allow ast.unparse with empty sets and NaN (GH-24897)
Automerge-Triggered-By: GH:pablogsal (cherry picked from commit 08ff436) Co-authored-by: Kodi Arfer <[email protected]>
1 parent 3365e68 commit e8e3419

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
@@ -1196,8 +1196,13 @@ def _write_docstring(self, node):
11961196

11971197
def _write_constant(self, value):
11981198
if isinstance(value, (float, complex)):
1199-
# Substitute overflowing decimal literal for AST infinities.
1200-
self.write(repr(value).replace("inf", _INFSTR))
1199+
# Substitute overflowing decimal literal for AST infinities,
1200+
# and inf - inf for NaNs.
1201+
self.write(
1202+
repr(value)
1203+
.replace("inf", _INFSTR)
1204+
.replace("nan", f"({_INFSTR}-{_INFSTR})")
1205+
)
12011206
elif self._avoid_backslashes and isinstance(value, str):
12021207
self._write_str_avoiding_backslashes(value)
12031208
else:
@@ -1270,10 +1275,13 @@ def visit_IfExp(self, node):
12701275
self.traverse(node.orelse)
12711276

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

12781286
def visit_Dict(self, node):
12791287
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)