From 068594eb38fc550d834b7e61745177c423f2b224 Mon Sep 17 00:00:00 2001 From: Shantanu Date: Tue, 9 Jun 2020 11:02:26 -0700 Subject: [PATCH] fastparse: improve error reporting for f-string syntax errors (#8970) --- mypy/fastparse.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/mypy/fastparse.py b/mypy/fastparse.py index 2aac8412f657..2dafbf4e1454 100644 --- a/mypy/fastparse.py +++ b/mypy/fastparse.py @@ -169,6 +169,11 @@ def parse(source: Union[str, bytes], tree.path = fnam tree.is_stub = is_stub_file except SyntaxError as e: + if sys.version_info < (3, 9) and e.filename == "": + # In Python 3.8 and earlier, syntax errors in f-strings have lineno relative to the + # start of the f-string. This would be misleading, as mypy will report the error as the + # lineno within the file. + e.lineno = None errors.report(e.lineno if e.lineno is not None else -1, e.offset, e.msg, blocker=True, code=codes.SYNTAX) tree = MypyFile([], [], False, {})