[issue29051] Improve error reporting involving f-strings (PEP 498)
Chi Hsuan Yen
report at bugs.python.org
Fri Dec 23 07:01:01 EST 2016
New submission from Chi Hsuan Yen:
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.
----------
components: Interpreter Core
messages: 283874
nosy: Chi Hsuan Yen, eric.smith, martin.panter
priority: normal
severity: normal
status: open
title: Improve error reporting involving f-strings (PEP 498)
type: enhancement
versions: Python 3.6, Python 3.7
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue29051>
_______________________________________
More information about the Python-bugs-list
mailing list