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..89bcb99d3224c 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 +klass foo +[out] +{"file": "main", "line": 2, "column": 7, "end_line": 2, "end_column": 8, "message": "Invalid syntax", "hint": null, "code": "syntax", "severity": "error"} +!!! Mypy crashed !!!