Message283874
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. |
|
Date |
User |
Action |
Args |
2016-12-23 12:01:02 | yan12125 | set | recipients:
+ yan12125, eric.smith, martin.panter |
2016-12-23 12:01:01 | yan12125 | set | messageid: <1482494461.91.0.191852219403.issue29051@psf.upfronthosting.co.za> |
2016-12-23 12:01:01 | yan12125 | link | issue29051 messages |
2016-12-23 12:01:00 | yan12125 | create | |
|