From e0b9c1eb2457f3f7f2df6f5b5bba13d4f0de22e7 Mon Sep 17 00:00:00 2001 From: lianxiaozhi <316426572+lianxiaozhi@users.noreply.github.com> Date: Tue, 10 Nov 2026 21:27:32 +0800 Subject: [PATCH 1/2] Fix --pretty crash on EOF syntax errors --- mypy/errors.py | 2 +- test-data/unit/cmdline.test | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/mypy/errors.py b/mypy/errors.py index e95904ce355e0..89e73ae6af587 100644 --- a/mypy/errors.py +++ b/mypy/errors.py @@ -1055,7 +1055,7 @@ def format_messages_default( a.append(s) if self.options.pretty: # Add source code fragment and a location marker. - if severity == "error" and source_lines and line > 0: + if severity == "error" and source_lines and 0 < line <= len(source_lines): source_line = source_lines[line - 1] source_line_expanded = source_line.expandtabs() min_column = len(source_line) - len(source_line.lstrip()) diff --git a/test-data/unit/cmdline.test b/test-data/unit/cmdline.test index 45e7da1845128..2acc7b500b564 100644 --- a/test-data/unit/cmdline.test +++ b/test-data/unit/cmdline.test @@ -771,6 +771,13 @@ some_file.py:1: error: Expected an identifier [syntax] ^ == Return code: 2 +[case testShowSourceCodeSnippetsUnexpectedEOF] +# cmd: mypy --pretty --show-error-codes _program.py +typing.Optional[typing.Union[A, B[T]] +[out] +_program.py:3: error: Unexpected EOF while parsing [syntax] +== Return code: 2 + [case testTabRenderingUponError] # cmd: mypy --pretty tabs.py [file tabs.py] From f9d7c5d0ed05c9cd82e761235b94b639328f7b39 Mon Sep 17 00:00:00 2001 From: lianxiaozhi <316426572+lianxiaozhi@users.noreply.github.com> Date: Tue, 17 Nov 2026 23:14:30 +0800 Subject: [PATCH 2/2] Move EOF formatting regression to a unit test --- mypy/test/testformatter.py | 10 ++++++++++ test-data/unit/cmdline.test | 7 ------- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/mypy/test/testformatter.py b/mypy/test/testformatter.py index 9f8bb5d824089..e19f2e282a356 100644 --- a/mypy/test/testformatter.py +++ b/mypy/test/testformatter.py @@ -2,10 +2,20 @@ from unittest import TestCase, main +from mypy.errors import Errors +from mypy.options import Options from mypy.util import split_words, trim_source_line class FancyErrorFormattingTestCases(TestCase): + def test_pretty_error_past_end_of_file(self) -> None: + options = Options() + options.pretty = True + errors = Errors(options, read_source=lambda _: ["x = (1,"], hide_error_codes=True) + errors.set_file("main", None, options) + errors.report(2, 1, "Unexpected EOF while parsing") + assert errors.new_messages() == ["main:2: error: Unexpected EOF while parsing"] + def test_trim_source(self) -> None: assert trim_source_line("0123456789abcdef", max_len=16, col=5, min_width=2) == ( "0123456789abcdef", diff --git a/test-data/unit/cmdline.test b/test-data/unit/cmdline.test index 2acc7b500b564..45e7da1845128 100644 --- a/test-data/unit/cmdline.test +++ b/test-data/unit/cmdline.test @@ -771,13 +771,6 @@ some_file.py:1: error: Expected an identifier [syntax] ^ == Return code: 2 -[case testShowSourceCodeSnippetsUnexpectedEOF] -# cmd: mypy --pretty --show-error-codes _program.py -typing.Optional[typing.Union[A, B[T]] -[out] -_program.py:3: error: Unexpected EOF while parsing [syntax] -== Return code: 2 - [case testTabRenderingUponError] # cmd: mypy --pretty tabs.py [file tabs.py]