From 6cee37718834fa7d0d51b1a7bb14f70ad0ec7ce8 Mon Sep 17 00:00:00 2001 From: Adam Turner Date: Thu, 30 Apr 2026 21:13:13 +0100 Subject: [PATCH 1/2] Respect JSON output mode for syntax errors --- mypy/build.py | 9 +++++++-- mypy/errors.py | 6 +++++- test-data/unit/outputjson.test | 7 +++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/mypy/build.py b/mypy/build.py index 5c1c6c62bffde..38242e4d2cafd 100644 --- a/mypy/build.py +++ b/mypy/build.py @@ -493,7 +493,12 @@ def build_inner( source_set = BuildSourceSet(sources) cached_read = fscache.read - errors = Errors(options, read_source=lambda path: read_py_file(path, cached_read)) + error_formatter = None if options.output is None else OUTPUT_CHOICES.get(options.output) + errors = Errors( + options, + read_source=lambda path: read_py_file(path, cached_read), + error_formatter=error_formatter, + ) # Record import errors so that they can be replayed by the workers. if workers: errors.global_watcher = True @@ -516,7 +521,7 @@ def build_inner( plugin=plugin, plugins_snapshot=snapshot, errors=errors, - error_formatter=None if options.output is None else OUTPUT_CHOICES.get(options.output), + error_formatter=error_formatter, flush_errors=flush_errors, fscache=fscache, stdout=stdout, diff --git a/mypy/errors.py b/mypy/errors.py index 70458e187f363..19f01205d5565 100644 --- a/mypy/errors.py +++ b/mypy/errors.py @@ -500,6 +500,7 @@ def __init__( *, read_source: Callable[[str], list[str] | None] | None = None, hide_error_codes: bool | None = None, + error_formatter: ErrorFormatter | None = None, ) -> None: self.options = options self.hide_error_codes = ( @@ -507,6 +508,7 @@ def __init__( ) # We use fscache to read source code when showing snippets. self.read_source = read_source + self.error_formatter = error_formatter self.initialize() def initialize(self) -> None: @@ -1129,7 +1131,9 @@ def new_messages(self) -> list[str]: for path in self.error_info_map.keys(): if path not in self.flushed_files: error_tuples = self.file_messages(path) - msgs.extend(self.format_messages(path, error_tuples)) + msgs.extend( + self.format_messages(path, error_tuples, formatter=self.error_formatter) + ) return msgs def targets(self) -> set[str]: diff --git a/test-data/unit/outputjson.test b/test-data/unit/outputjson.test index 39741b3d1a8bf..18048a33399ff 100644 --- a/test-data/unit/outputjson.test +++ b/test-data/unit/outputjson.test @@ -42,3 +42,10 @@ bar('42') {"file": "main", "line": 12, "column": 12, "end_line": 12, "end_column": 15, "message": "Revealed type is \"Overload(def (), def (x: int))\"", "hint": null, "code": "misc", "severity": "note"} {"file": "main", "line": 14, "column": 0, "end_line": 14, "end_column": 9, "message": "No overload variant of \"foo\" matches argument type \"str\"", "hint": "Possible overload variants:\n def foo() -> None\n def foo(x: int) -> None", "code": "call-overload", "severity": "error"} {"file": "main", "line": 17, "column": 0, "end_line": 17, "end_column": 9, "message": "Too many arguments for \"bar\"", "hint": null, "code": "call-arg", "severity": "error"} + +[case testOutputJsonSyntaxError] +# flags: --output=json +def foo< +[out] +{"file": "main", "line": 2, "column": 8, "end_line": 2, "end_column": 9, "message": "Expected '('", "hint": null, "code": "syntax", "severity": "error"} +!!! Mypy crashed !!! From 071f7df6a540be03205f7d8382094502d14843b0 Mon Sep 17 00:00:00 2001 From: Adam Turner Date: Thu, 30 Apr 2026 21:13:13 +0100 Subject: [PATCH 2/2] Fix test for 3.10 --- test-data/unit/outputjson.test | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test-data/unit/outputjson.test b/test-data/unit/outputjson.test index 18048a33399ff..89bcb99d3224c 100644 --- a/test-data/unit/outputjson.test +++ b/test-data/unit/outputjson.test @@ -45,7 +45,7 @@ bar('42') [case testOutputJsonSyntaxError] # flags: --output=json -def foo< +klass foo [out] -{"file": "main", "line": 2, "column": 8, "end_line": 2, "end_column": 9, "message": "Expected '('", "hint": null, "code": "syntax", "severity": "error"} +{"file": "main", "line": 2, "column": 7, "end_line": 2, "end_column": 8, "message": "Invalid syntax", "hint": null, "code": "syntax", "severity": "error"} !!! Mypy crashed !!!