asottile-archive / future-fstrings

A backport of fstrings to python<3.6

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Implicit continuations with f-strings cause SyntaxErrors

tulir opened this issue · comments

Line continuations that contain f-strings aren't properly transpiled.

Test script:

# -*- coding: future_fstrings -*-
a = 1
b = 2

# Escaped newline
c = f"a={a} " \
    f"b={b}"
print(c)

# Implied line continuation inside parentheses
print(f"a={a} "
      f"b={b}")

Output with Python 3.6:

a=1 b=2
a=1 b=2

Error with Python 3.5:

  File "asd.py", line 6
    "b={}".format((b))
         ^
SyntaxError: invalid syntax

Ah indeed, some more clever for implicit continuation needs to be implemented.

Thanks for the report! :)

If you want to hack on it, the problematic code is here:

https://github.com/asottile/future-fstrings/blob/61a97ceacb656c77b04f78d5c3c971a2cd1d804e/future_fstrings.py#L154-L158

This needs to continue to iterate forward until it finds a token that isn't either:

  • name(f) followed by string
  • a string
  • an escaped newline
  • whitespace
  • newline

There's some interesting edge cases here that are more fiddly:

print('foo' f'bar {1}')  # foobar 1
print(f'bar {1}' 'foo')  # bar 1foo
print(f'foo {1}' 'bar {1})  # foo 1bar {1}

@tulir please try out 0.4.2 which has a fix for this!

Everything seems to work now, thanks!