Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions lib/astunparse/unparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,7 @@ def _Str(self, tree):

format_conversions = {97: 'a', 114: 'r', 115: 's'}

def _FormattedValue(self, t):
# FormattedValue(expr value, int? conversion, expr? format_spec)
def _generic_FormattedValue(self, t):
self.write("{")
self.dispatch(t.value)
if t.conversion is not None and t.conversion != -1:
Expand All @@ -455,17 +454,33 @@ def _FormattedValue(self, t):
if t.format_spec is not None:
self.write(":")
if isinstance(t.format_spec, ast.Str):
# It's ast.Str in 3.6.0
self.write(t.format_spec.s)
elif isinstance(t.format_spec, ast.JoinedStr):
# It's ast.JoinedStr in 3.6.1+
for value in t.format_spec.values:
if isinstance(value, ast.Str):
self.write(value.s)
else:
self.dispatch(value)
else:
self.dispatch(t.format_spec)
self.write("}")

def _FormattedValue(self, t):
# FormattedValue(expr value, int? conversion, expr? format_spec)
self.write("f'''")
self._generic_FormattedValue(t)
self.write("'''")

def _JoinedStr(self, t):
# JoinedStr(expr* values)
self.write("f'''")
for value in t.values:
if isinstance(value, ast.Str):
self.write(value.s)
elif isinstance(value, ast.FormattedValue):
self._generic_FormattedValue(value)
else:
self.dispatch(value)
self.write("'''")
Expand Down
7 changes: 7 additions & 0 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,13 @@ def test_raise_from(self):
def test_bytes(self):
self.check_roundtrip("b'123'")

@unittest.skipIf(sys.version_info < (3, 6), "Not supported < 3.6")
def test_formatted_value(self):
self.check_roundtrip('f"{value}"')
self.check_roundtrip('f"{value!s}"')
self.check_roundtrip('f"{value:4}"')
self.check_roundtrip('f"{value!s:4}"')

@unittest.skipIf(sys.version_info < (3, 6), "Not supported < 3.6")
def test_joined_str(self):
self.check_roundtrip('f"{key}={value!s}"')
Expand Down