Skip to content

Commit

Permalink
set precedence on f-string expressions, resolves berkerpeksag#153
Browse files Browse the repository at this point in the history
  • Loading branch information
isidentical committed Dec 9, 2019
1 parent 3923913 commit cda6724
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions astor/code_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,7 @@ def recurse(node):
if getattr(value, 'expr_text', None):
self.write(value.expr_text)
else:
set_precedence(value, value.value)
self.visit(value.value)
if value.conversion != -1:
self.write('!%s' % chr(value.conversion))
Expand Down
1 change: 1 addition & 0 deletions astor/op_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
ExtSlice 1
comprehension_target 1
Tuple 0
FormattedValue 0
Comma 1
NamedExpr 1
Expand Down
13 changes: 13 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@
Release Notes
=============

0.8.1 - in development
----------------------

Bug fixes
~~~~~~~~~

* Fixed precedence issue for f-string expressions that caused
redundant parenthesis around expression.
(Reported by Ilya Kamenshchikov in `Issue 153`_ and fixed by Batuhan Taskaya in `PR 155`_.)

.. _`Issue 153`: https://github.com/berkerpeksag/astor/issues/153
.. _`PR 155`: https://github.com/berkerpeksag/astor/pull/155

0.8.0 - 2019-05-19
------------------

Expand Down
1 change: 1 addition & 0 deletions tests/test_code_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ def test_fstrings(self):
x = f'{int(x)}'
x = f'a{b:c}d'
x = f'a{b!s:c{d}e}f'
x = f'{x + y}'
x = f'""'
x = f'"\\''
"""
Expand Down

0 comments on commit cda6724

Please sign in to comment.