Skip to content

Commit eb0caa1

Browse files
AA-TurnerAdam Turner
andauthored
Respect JSON output mode for syntax errors (#21386)
Fixes #21370. A --------- Co-authored-by: Adam Turner <turner@hudson-trading.com>
1 parent 485f523 commit eb0caa1

3 files changed

Lines changed: 19 additions & 3 deletions

File tree

mypy/build.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,12 @@ def build_inner(
493493

494494
source_set = BuildSourceSet(sources)
495495
cached_read = fscache.read
496-
errors = Errors(options, read_source=lambda path: read_py_file(path, cached_read))
496+
error_formatter = None if options.output is None else OUTPUT_CHOICES.get(options.output)
497+
errors = Errors(
498+
options,
499+
read_source=lambda path: read_py_file(path, cached_read),
500+
error_formatter=error_formatter,
501+
)
497502
# Record import errors so that they can be replayed by the workers.
498503
if workers:
499504
errors.global_watcher = True
@@ -516,7 +521,7 @@ def build_inner(
516521
plugin=plugin,
517522
plugins_snapshot=snapshot,
518523
errors=errors,
519-
error_formatter=None if options.output is None else OUTPUT_CHOICES.get(options.output),
524+
error_formatter=error_formatter,
520525
flush_errors=flush_errors,
521526
fscache=fscache,
522527
stdout=stdout,

mypy/errors.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,13 +500,15 @@ def __init__(
500500
*,
501501
read_source: Callable[[str], list[str] | None] | None = None,
502502
hide_error_codes: bool | None = None,
503+
error_formatter: ErrorFormatter | None = None,
503504
) -> None:
504505
self.options = options
505506
self.hide_error_codes = (
506507
hide_error_codes if hide_error_codes is not None else options.hide_error_codes
507508
)
508509
# We use fscache to read source code when showing snippets.
509510
self.read_source = read_source
511+
self.error_formatter = error_formatter
510512
self.initialize()
511513

512514
def initialize(self) -> None:
@@ -1129,7 +1131,9 @@ def new_messages(self) -> list[str]:
11291131
for path in self.error_info_map.keys():
11301132
if path not in self.flushed_files:
11311133
error_tuples = self.file_messages(path)
1132-
msgs.extend(self.format_messages(path, error_tuples))
1134+
msgs.extend(
1135+
self.format_messages(path, error_tuples, formatter=self.error_formatter)
1136+
)
11331137
return msgs
11341138

11351139
def targets(self) -> set[str]:

test-data/unit/outputjson.test

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,10 @@ bar('42')
4242
{"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"}
4343
{"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"}
4444
{"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"}
45+
46+
[case testOutputJsonSyntaxError]
47+
# flags: --output=json
48+
klass foo
49+
[out]
50+
{"file": "main", "line": 2, "column": 7, "end_line": 2, "end_column": 8, "message": "Invalid syntax", "hint": null, "code": "syntax", "severity": "error"}
51+
!!! Mypy crashed !!!

0 commit comments

Comments
 (0)