classification
Title: [3.7.5 x86_64 Linux] f-string parsing results in EOL
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Hobson.Lane, eric.smith, yingw787, zach.ware
Priority: normal Keywords:

Created on 2019-10-23 19:09 by yingw787, last changed 2019-10-29 22:03 by eric.smith. This issue is now closed.

Messages (6)
msg355254 - (view) Author: Ying Wang (yingw787) * Date: 2019-10-23 19:09
Hey,

I encountered an interesting bug when trying to do string parsing using f-strings. I am currently under the impression that within the curly braces is any expression that can be successfully evaluated in a REPL. Here's the error:

```bash
yingw787@yingw787-Oryx-Pro:~/src/gpudb-dev-v6.2.0/kio/kio/tests/regression/_data/csv$ python
Python 3.7.5 (default, Oct 15 2019, 21:38:37) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> blah = '"hi"'
>>> blah
'"hi"'
>>> f'{blah.strip('"')}'
  File "<stdin>", line 1
    f'{blah.strip('"')}'
                       ^
SyntaxError: EOL while scanning string literal
>>> '{0}'.format(blah.strip('"'))
'hi'
>>> 
```

I can use '.format()' for now, but it might be nice to align f-string expr behavior w/ .format(). Please let me know if you need more reproduction steps, or if you need a helping hand :)

Thanks
Ying
msg355255 - (view) Author: Zachary Ware (zach.ware) * (Python committer) Date: 2019-10-23 19:17
This is because you're trying to use the string's delimiting character (') in the string itself, which won't work for fairly apparent reasons :).  Try removing the `f` prefix and see what happens.

You can get around this by using a triple-quoted string which will allow you to use both quote characters within the string:

>>> blah = '"hi"'
>>> f'''{blah.strip('"')}'''
'hi'
msg355256 - (view) Author: Hobs (Hobson.Lane) Date: 2019-10-23 19:21
Not a bug. If you use quotes inside an f-string (or any other kind of
string) they need to be escaped.
--Hobson

On Wed, Oct 23, 2019 at 12:09 PM Ying Wang <report@bugs.python.org> wrote:

>
> New submission from Ying Wang <yingw787.personal@gmail.com>:
>
> Hey,
>
> I encountered an interesting bug when trying to do string parsing using
> f-strings. I am currently under the impression that within the curly braces
> is any expression that can be successfully evaluated in a REPL. Here's the
> error:
>
> ```bash
> yingw787@yingw787-Oryx-Pro:~/src/gpudb-dev-v6.2.0/kio/kio/tests/regression/_data/csv$
> python
> Python 3.7.5 (default, Oct 15 2019, 21:38:37)
> [GCC 5.4.0 20160609] on linux
> Type "help", "copyright", "credits" or "license" for more information.
> >>> blah = '"hi"'
> >>> blah
> '"hi"'
> >>> f'{blah.strip('"')}'
>   File "<stdin>", line 1
>     f'{blah.strip('"')}'
>                        ^
> SyntaxError: EOL while scanning string literal
> >>> '{0}'.format(blah.strip('"'))
> 'hi'
> >>>
> ```
>
> I can use '.format()' for now, but it might be nice to align f-string expr
> behavior w/ .format(). Please let me know if you need more reproduction
> steps, or if you need a helping hand :)
>
> Thanks
> Ying
>
> ----------
> components: Interpreter Core
> messages: 355254
> nosy: yingw787
> priority: normal
> severity: normal
> status: open
> title: [3.7.5 x86_64 Linux] f-string parsing results in EOL
> type: behavior
> versions: Python 3.7
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue38568>
> _______________________________________
> _______________________________________________
> New-bugs-announce mailing list
> New-bugs-announce@python.org
> https://mail.python.org/mailman/listinfo/new-bugs-announce
>
msg355259 - (view) Author: Hobs (Hobson.Lane) Date: 2019-10-23 19:28
It is a limitation of f-strings, though. We're not allowed to use
backslashes within the f-string expression in curly braces. So to do what
you want you have to create another variable containing the quote character:

```
>>> blah = '"hi"'
>>> blah
'"hi"'
>>> q = '"'
>>> f'{blah.strip(q)}'
'hi'
```
--Hobson

On Wed, Oct 23, 2019 at 12:21 PM Hobson Lane <hobson@totalgood.com> wrote:

> Not a bug. If you use quotes inside an f-string (or any other kind of
> string) they need to be escaped.
> --Hobson
>
>
> On Wed, Oct 23, 2019 at 12:09 PM Ying Wang <report@bugs.python.org> wrote:
>
>>
>> New submission from Ying Wang <yingw787.personal@gmail.com>:
>>
>> Hey,
>>
>> I encountered an interesting bug when trying to do string parsing using
>> f-strings. I am currently under the impression that within the curly braces
>> is any expression that can be successfully evaluated in a REPL. Here's the
>> error:
>>
>> ```bash
>> yingw787@yingw787-Oryx-Pro:~/src/gpudb-dev-v6.2.0/kio/kio/tests/regression/_data/csv$
>> python
>> Python 3.7.5 (default, Oct 15 2019, 21:38:37)
>> [GCC 5.4.0 20160609] on linux
>> Type "help", "copyright", "credits" or "license" for more information.
>> >>> blah = '"hi"'
>> >>> blah
>> '"hi"'
>> >>> f'{blah.strip('"')}'
>>   File "<stdin>", line 1
>>     f'{blah.strip('"')}'
>>                        ^
>> SyntaxError: EOL while scanning string literal
>> >>> '{0}'.format(blah.strip('"'))
>> 'hi'
>> >>>
>> ```
>>
>> I can use '.format()' for now, but it might be nice to align f-string
>> expr behavior w/ .format(). Please let me know if you need more
>> reproduction steps, or if you need a helping hand :)
>>
>> Thanks
>> Ying
>>
>> ----------
>> components: Interpreter Core
>> messages: 355254
>> nosy: yingw787
>> priority: normal
>> severity: normal
>> status: open
>> title: [3.7.5 x86_64 Linux] f-string parsing results in EOL
>> type: behavior
>> versions: Python 3.7
>>
>> _______________________________________
>> Python tracker <report@bugs.python.org>
>> <https://bugs.python.org/issue38568>
>> _______________________________________
>> _______________________________________________
>> New-bugs-announce mailing list
>> New-bugs-announce@python.org
>> https://mail.python.org/mailman/listinfo/new-bugs-announce
>>
>
msg355260 - (view) Author: Ying Wang (yingw787) * Date: 2019-10-23 19:29
Oh cool, I didn't know triple quotes can be used as part of f-strings! Thanks for the explanation!
msg355684 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2019-10-29 22:03
> It is a limitation of f-strings, though. We're not allowed to use
> backslashes within the f-string expression in curly braces.

I've considered relaxing these restrictions, although it's very complicated. The parser would need to become aware of what's inside an f-string (the braces, the expressions, the optional format spec, the doubled braces, escaped characters, etc.). See PEP 536 for a rough outline. The = format for "debugging" with f-strings (see issue 36817) also makes this more complicated.
History
Date User Action Args
2019-10-29 22:03:15eric.smithsetnosy: + eric.smith
messages: + msg355684
2019-10-23 19:29:24yingw787setmessages: + msg355260
2019-10-23 19:28:14Hobson.Lanesetmessages: + msg355259
2019-10-23 19:21:55Hobson.Lanesetnosy: + Hobson.Lane
messages: + msg355256
2019-10-23 19:17:37zach.waresetstatus: open -> closed

nosy: + zach.ware
messages: + msg355255

resolution: not a bug
stage: resolved
2019-10-23 19:09:14yingw787create