Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Error format support, and JSON output option #11396

Merged
merged 48 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
393820c
Add -O/--output CLI option
tusharsadhwani Oct 24, 2021
282bd28
Initial formatter setup
tusharsadhwani Oct 25, 2021
ccda5b0
Make error_formatter an optional argument
tusharsadhwani Oct 27, 2021
c849a77
Fix type annotation
tusharsadhwani Oct 27, 2021
fd2feab
Fix whitespace
tusharsadhwani Oct 27, 2021
b188001
Remove whitespace
tusharsadhwani Oct 27, 2021
51c1acc
Merge branch 'master' of https://github.com/tusharsadhwani/mypy into …
tusharsadhwani Oct 27, 2021
9177dab
Merge branch 'python:master' into output-json
tushar-deepsource Jan 19, 2022
bc5ceac
Add hint property to errors
tushar-deepsource Jan 19, 2022
9d29ab0
Fix lint issues
tusharsadhwani Jan 19, 2022
bd6d48d
Merge branch 'master' into output-json
tusharsadhwani Feb 22, 2023
ba8d17f
Fix import and typing issues
tusharsadhwani Feb 22, 2023
a2bc04d
Fix error tuple signature
tusharsadhwani Feb 22, 2023
35974e4
Import Optional
tusharsadhwani Feb 23, 2023
1e5ec91
Run black
tusharsadhwani Feb 23, 2023
723219f
Run black on another file
tusharsadhwani Feb 23, 2023
2228c0a
Run isort
tusharsadhwani Feb 23, 2023
33d81b0
Run isort on build.py
tusharsadhwani Feb 23, 2023
63001ea
Merge branch 'master' into output-json
tusharsadhwani Apr 19, 2023
d27be7e
Merge branch 'master' into output-json
tusharsadhwani Apr 20, 2023
efe5c5d
Merge branch 'master' into output-json
tusharsadhwani Apr 27, 2023
1872ae6
Add tests for json output
tusharsadhwani Apr 27, 2023
3abc9cb
Suggestions from code review, and negative test
tusharsadhwani Apr 27, 2023
6c9ab11
Add default value of None
tusharsadhwani Apr 27, 2023
627ed8e
Default output to None in options as well
tusharsadhwani Apr 27, 2023
e425cbe
Fix failing tests
tusharsadhwani Apr 27, 2023
47f1b07
improve docstring
tusharsadhwani Apr 27, 2023
e00ad4a
type cast
tusharsadhwani Apr 27, 2023
c1fb6a2
Another explicit type cast
tusharsadhwani Apr 27, 2023
aafe3aa
remove unused import
tusharsadhwani Apr 27, 2023
fae3215
create formatter object
tusharsadhwani Apr 27, 2023
89ad1d3
Add custom end to end test
tusharsadhwani Apr 27, 2023
7a3f736
unused import
tusharsadhwani Apr 27, 2023
6d46f75
trailing whitespace
tusharsadhwani Apr 27, 2023
e71a372
try fixing windows
tusharsadhwani Apr 28, 2023
8cca203
fix windows separator issue
tusharsadhwani Apr 28, 2023
79e16a8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 28, 2023
8bf4890
unused import
tusharsadhwani Apr 28, 2023
5899f26
Merge branch 'master' into output-json
tusharsadhwani Apr 28, 2023
880b8f3
Merge branch 'master' into output-json
tusharsadhwani May 5, 2023
0aafadf
Pass error tuples to format_messages
tusharsadhwani May 10, 2023
4cab249
Merge branch 'master' into output-json
tusharsadhwani May 10, 2023
7fe71c3
Merge branch 'master' into output-json
tusharsadhwani May 13, 2023
ad8f1d6
Merge branch 'master' into output-json
tusharsadhwani May 9, 2024
e2fd45e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 9, 2024
4b03c5c
ruff lints
tusharsadhwani May 9, 2024
e0e6896
address comments
tusharsadhwani May 10, 2024
a0dc6d1
use severity
tusharsadhwani May 10, 2024
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
2 changes: 1 addition & 1 deletion mypy/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ def _build(
plugin=plugin,
plugins_snapshot=snapshot,
errors=errors,
error_formatter=OUTPUT_CHOICES.get(options.output),
error_formatter=None if options.output is None else OUTPUT_CHOICES.get(options.output),
flush_errors=flush_errors,
fscache=fscache,
stdout=stdout,
Expand Down
3 changes: 2 additions & 1 deletion mypy/error_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ def report_error(self, error: "MypyError") -> str:
"line": error.line,
"column": error.column,
"message": error.message,
"hint": error.hint or None,
"hint": None if len(error.hints) == 0 else "\n".join(error.hints),
"code": None if error.errorcode is None else error.errorcode.code,
"is_note": error.is_note,
}
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This module should define a dictionary str -> ErrorFormatter that can be used in build.py.


Expand Down
16 changes: 8 additions & 8 deletions mypy/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1295,15 +1295,16 @@ def __init__(
line: int,
column: int,
message: str,
hint: str,
errorcode: ErrorCode | None,
is_note: bool = False,
) -> None:
self.file_path = file_path
self.line = line
self.column = column
self.message = message
self.hint = hint
self.errorcode = errorcode
self.is_note = is_note
self.hints: list[str] = []


# (file_path, line, column)
Expand All @@ -1324,16 +1325,15 @@ def create_errors(error_tuples: list[ErrorTuple]) -> list[MypyError]:
error_location = (file_path, line, column)
error = latest_error_at_location.get(error_location)
if error is None:
# No error tuple found for this hint. Ignoring it
# This is purely a note, with no error correlated to it
error = MypyError(file_path, line, column, message, errorcode, is_note=True)
errors.append(error)
continue

if error.hint == "":
error.hint = message
else:
error.hint += "\n" + message
error.hints.append(message)

else:
error = MypyError(file_path, line, column, message, "", errorcode)
error = MypyError(file_path, line, column, message, errorcode)
errors.append(error)
error_location = (file_path, line, column)
latest_error_at_location[error_location] = error
Expand Down
2 changes: 1 addition & 1 deletion mypy/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ def __init__(self) -> None:
self.force_union_syntax = False

# Sets custom output format
self.output = ""
self.output: str | None = None

def use_lowercase_names(self) -> bool:
if self.python_version >= (3, 9):
Expand Down
9 changes: 6 additions & 3 deletions test-data/unit/outputjson.test
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def foo() -> None:

foo(1)
[out]
{"file": "main", "line": 5, "column": 0, "message": "Too many arguments for \"foo\"", "hint": null, "code": "call-arg"}
{"file": "main", "line": 5, "column": 0, "message": "Too many arguments for \"foo\"", "hint": null, "code": "call-arg", "is_note": false}

[case testOutputJsonWithHint]
# flags: --output=json
Expand All @@ -32,10 +32,13 @@ def foo(x: int) -> None: ...
def foo(x: Optional[int] = None) -> None:
...

reveal_type(foo)

foo('42')

def bar() -> None: ...
bar('42')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a test case with reveal_type(); let's make sure that shows up in the output.

[out]
{"file": "main", "line": 12, "column": 0, "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"}
{"file": "main", "line": 15, "column": 0, "message": "Too many arguments for \"bar\"", "hint": null, "code": "call-arg"}
{"file": "main", "line": 12, "column": 12, "message": "Revealed type is \"Overload(def (), def (x: builtins.int))\"", "hint": null, "code": "misc", "is_note": true}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we change this to "severity": "note", mirroring mypy's Python code (and severity error for the errors)? That will also make it easier to extend to other kinds of severity in the future.

{"file": "main", "line": 14, "column": 0, "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", "is_note": false}
{"file": "main", "line": 17, "column": 0, "message": "Too many arguments for \"bar\"", "hint": null, "code": "call-arg", "is_note": false}
Loading