-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy patherror_formatter.py
More file actions
39 lines (29 loc) · 1.14 KB
/
error_formatter.py
File metadata and controls
39 lines (29 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""Defines the different custom formats in which mypy can output."""
import json
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from mypy.errors import MypyError
class ErrorFormatter(ABC):
"""Base class to define how errors are formatted before being printed."""
@abstractmethod
def report_error(self, error: "MypyError") -> str:
raise NotImplementedError
class JSONFormatter(ErrorFormatter):
"""Formatter for basic JSON output format."""
def report_error(self, error: "MypyError") -> str:
"""Prints out the errors as simple, static JSON lines."""
return json.dumps(
{
"file": error.file_path,
"line": error.line,
"column": error.column,
"end_line": error.end_line,
"end_column": error.end_column,
"message": error.message,
"hint": None if len(error.hints) == 0 else "\n".join(error.hints),
"code": error.errorcode,
"severity": error.severity,
}
)
OUTPUT_CHOICES = {"json": JSONFormatter()}