Skip to content

Commit 068594e

Browse files
authored
fastparse: improve error reporting for f-string syntax errors (#8970)
1 parent 793cf18 commit 068594e

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

mypy/fastparse.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,11 @@ def parse(source: Union[str, bytes],
169169
tree.path = fnam
170170
tree.is_stub = is_stub_file
171171
except SyntaxError as e:
172+
if sys.version_info < (3, 9) and e.filename == "<fstring>":
173+
# In Python 3.8 and earlier, syntax errors in f-strings have lineno relative to the
174+
# start of the f-string. This would be misleading, as mypy will report the error as the
175+
# lineno within the file.
176+
e.lineno = None
172177
errors.report(e.lineno if e.lineno is not None else -1, e.offset, e.msg, blocker=True,
173178
code=codes.SYNTAX)
174179
tree = MypyFile([], [], False, {})

0 commit comments

Comments
 (0)