Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion mypy/build_worker/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 7 additions & 1 deletion mypy/test/testoutput.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import os
import os.path
import re

from mypy import api
from mypy.defaults import PYTHON3_VERSION
Expand All @@ -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.
Expand Down
16 changes: 16 additions & 0 deletions test-data/unit/outputjson.test
Original file line number Diff line number Diff line change
Expand Up @@ -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 !!!
Loading