This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author yan12125
Recipients eric.smith, martin.panter, yan12125
Date 2016-12-23.12:01:00
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1482494461.91.0.191852219403.issue29051@psf.upfronthosting.co.za>
In-reply-to
Content
Here are the two examples I found confusing when playing with f-strings. The first one involves with a NameError:

$ cat test2
f'''
{
FOO
}
'''

$ python3.7m test2
Traceback (most recent call last):
  File "test2", line 5, in <module>
    '''
NameError: name 'FOO' is not defined


It would be better if the error reporter points to the actual line of the error:

$ python3.7m test2
Traceback (most recent call last):
  File "test2", line 3, in <module>
    FOO
NameError: name 'FOO' is not defined

The second one involves a SyntaxError:

$ cat test2 
f'''
{
a b c
}
'''

$ python3.7m test2
  File "<fstring>", line 2
    a b c
      ^
SyntaxError: invalid syntax

It would be better if the line number is relative to the file instead of the expression in f-strings:

$ python3.7m test2
  File "test2", line 3
    a b c
      ^
SyntaxError: invalid syntax

By the way, external checkers like pyflakes also suffers. They rely on ASTs. Nodes in f-strings have their lineno relative to the {...} expression instead of the whole code string. For example:

import ast

code = '''
f'{LOL}'
'''

for node in ast.walk(ast.parse(code, "<stdin>", "exec")):
    if isinstance(node, ast.Name):
        print(node.lineno)


Prints 1 instead of 2.

Another by the way, ruby reports correct line numbers:

$ cat test3 
"
#{
FOO
}
"

$ ruby test3 
test3:3:in `<main>': uninitialized constant FOO (NameError)


$ cat test3 
"
#{
@@
}
"

$ ruby test3
test3:3: `@@' without identifiers is not allowed as a class variable name
test3:3: syntax error, unexpected end-of-input


Added the author and the primary reviewer of issue24965.
History
Date User Action Args
2016-12-23 12:01:02yan12125setrecipients: + yan12125, eric.smith, martin.panter
2016-12-23 12:01:01yan12125setmessageid: <1482494461.91.0.191852219403.issue29051@psf.upfronthosting.co.za>
2016-12-23 12:01:01yan12125linkissue29051 messages
2016-12-23 12:01:00yan12125create