From 857001b1c2c9e8214e94404006a7bf06d96bddd5 Mon Sep 17 00:00:00 2001 From: Adam Turner Date: Thu, 7 May 2026 15:03:16 +0100 Subject: [PATCH] Fix JSON output mode for syntax errors in parallel mode --- mypy/build_worker/worker.py | 7 ++++++- mypy/test/testoutput.py | 8 +++++++- test-data/unit/outputjson.test | 16 ++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/mypy/build_worker/worker.py b/mypy/build_worker/worker.py index 4a0bac99b440b..e9f7a026037e4 100644 --- a/mypy/build_worker/worker.py +++ b/mypy/build_worker/worker.py @@ -113,7 +113,12 @@ def main(argv: list[str]) -> None: fscache = FileSystemCache() fscache.set_package_root(options.package_root) 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, + ) ctx = ServerContext(options, disable_error_code, enable_error_code, errors, fscache) try: diff --git a/mypy/test/testoutput.py b/mypy/test/testoutput.py index 41f6881658c8c..e18302cbf92a0 100644 --- a/mypy/test/testoutput.py +++ b/mypy/test/testoutput.py @@ -8,6 +8,7 @@ import os import os.path +import re from mypy import api from mypy.defaults import PYTHON3_VERSION @@ -24,7 +25,12 @@ def run_case(self, testcase: DataDrivenTestCase) -> None: def test_output_json(testcase: DataDrivenTestCase) -> None: """Runs Mypy in a subprocess, and ensures that `--output=json` works as intended.""" - mypy_cmdline = ["--output=json"] + program_text = "\n".join(testcase.input) + flags_match = re.search("# flags: (.*)$", program_text, flags=re.MULTILINE) + if flags_match is not None: + mypy_cmdline = flags_match.group(1).split() + else: + mypy_cmdline = ["--output=json"] mypy_cmdline.append(f"--python-version={'.'.join(map(str, PYTHON3_VERSION))}") # Write the program to a file. diff --git a/test-data/unit/outputjson.test b/test-data/unit/outputjson.test index 89bcb99d3224c..ec716d97b8f1e 100644 --- a/test-data/unit/outputjson.test +++ b/test-data/unit/outputjson.test @@ -43,9 +43,25 @@ bar('42') {"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 testOutputJsonParallel] +# flags: --output=json --num-workers=2 +def foo() -> None: + pass + +foo(1) +[out] +{"file": "main", "line": 5, "column": 0, "end_line": 5, "end_column": 6, "message": "Too many arguments for \"foo\"", "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 !!! + +[case testOutputJsonSyntaxErrorParallel] +# flags: --output=json --num-workers=2 +break +[out] +{"file": "main", "line": 2, "column": 0, "end_line": 2, "end_column": 5, "message": "\"break\" outside loop", "hint": null, "code": null, "severity": "error"} +!!! Mypy crashed !!!