mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
[3.14] gh-138774: use value to ast.unparse code when str is None in ast.Interpolation (GH-139415) (#140503)
gh-138774: use `value` to `ast.unparse` code when `str` is `None` in `ast.Interpolation` (GH-139415)
(cherry picked from commit 4d0849426f)
Co-authored-by: George Ogden <38294960+George-Ogden@users.noreply.github.com>
This commit is contained in:
parent
d1a434f7b2
commit
564bb00b27
4 changed files with 102 additions and 12 deletions
|
|
@ -658,9 +658,9 @@ def _unparse_interpolation_value(self, inner):
|
|||
unparser.set_precedence(_Precedence.TEST.next(), inner)
|
||||
return unparser.visit(inner)
|
||||
|
||||
def _write_interpolation(self, node, is_interpolation=False):
|
||||
def _write_interpolation(self, node, use_str_attr=False):
|
||||
with self.delimit("{", "}"):
|
||||
if is_interpolation:
|
||||
if use_str_attr:
|
||||
expr = node.str
|
||||
else:
|
||||
expr = self._unparse_interpolation_value(node.value)
|
||||
|
|
@ -678,7 +678,8 @@ def visit_FormattedValue(self, node):
|
|||
self._write_interpolation(node)
|
||||
|
||||
def visit_Interpolation(self, node):
|
||||
self._write_interpolation(node, is_interpolation=True)
|
||||
# If `str` is set to `None`, use the `value` to generate the source code.
|
||||
self._write_interpolation(node, use_str_attr=node.str is not None)
|
||||
|
||||
def visit_Name(self, node):
|
||||
self.write(node.id)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue