bpo-43521: Allow ast.unparse with empty sets and NaN (GH-24897)

Automerge-Triggered-By: GH:pablogsal
(cherry picked from commit 08ff4369af)

Co-authored-by: Kodi Arfer <Kodiologist@users.noreply.github.com>
This commit is contained in:
Miss Islington (bot) 2021-03-18 15:51:47 -07:00 committed by GitHub
parent 3365e684a8
commit e8e341993e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 9 deletions

View file

@ -1196,8 +1196,13 @@ def _write_docstring(self, node):
def _write_constant(self, value):
if isinstance(value, (float, complex)):
# Substitute overflowing decimal literal for AST infinities.
self.write(repr(value).replace("inf", _INFSTR))
# Substitute overflowing decimal literal for AST infinities,
# and inf - inf for NaNs.
self.write(
repr(value)
.replace("inf", _INFSTR)
.replace("nan", f"({_INFSTR}-{_INFSTR})")
)
elif self._avoid_backslashes and isinstance(value, str):
self._write_str_avoiding_backslashes(value)
else:
@ -1270,10 +1275,13 @@ def visit_IfExp(self, node):
self.traverse(node.orelse)
def visit_Set(self, node):
if not node.elts:
raise ValueError("Set node should have at least one item")
with self.delimit("{", "}"):
self.interleave(lambda: self.write(", "), self.traverse, node.elts)
if node.elts:
with self.delimit("{", "}"):
self.interleave(lambda: self.write(", "), self.traverse, node.elts)
else:
# `{}` would be interpreted as a dictionary literal, and
# `set` might be shadowed. Thus:
self.write('{*()}')
def visit_Dict(self, node):
def write_key_value_pair(k, v):