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 7 commits
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion mypy/build.py
Expand Up @@ -34,6 +34,7 @@
from mypy.checker import TypeChecker
from mypy.indirection import TypeIndirectionVisitor
from mypy.errors import Errors, CompileError, ErrorInfo, report_internal_error
from mypy.error_formatter import ErrorFormatter, JSONFormatter
from mypy.util import (
DecodeError, decode_python_encoding, is_sub_path, get_mypy_comments, module_prefix,
read_py_file, hash_digest, is_typeshed_file, is_stub_package_file, get_top_two_prefixes
Expand Down Expand Up @@ -245,6 +246,7 @@ def _build(sources: List[BuildSource],
plugin=plugin,
plugins_snapshot=snapshot,
errors=errors,
error_formatter=JSONFormatter() if options.output == 'json' else None,
flush_errors=flush_errors,
fscache=fscache,
stdout=stdout,
Expand Down Expand Up @@ -583,6 +585,7 @@ def __init__(self, data_dir: str,
fscache: FileSystemCache,
stdout: TextIO,
stderr: TextIO,
error_formatter: Optional['ErrorFormatter'] = None,
) -> None:
self.stats: Dict[str, Any] = {} # Values are ints or floats
self.stdout = stdout
Expand All @@ -591,6 +594,7 @@ def __init__(self, data_dir: str,
self.data_dir = data_dir
self.errors = errors
self.errors.set_ignore_prefix(ignore_prefix)
self.error_formatter = error_formatter
self.search_paths = search_paths
self.source_set = source_set
self.reports = reports
Expand Down Expand Up @@ -3156,7 +3160,9 @@ def process_stale_scc(graph: Graph, scc: List[str], manager: BuildManager) -> No
for id in stale:
graph[id].transitive_error = True
for id in stale:
manager.flush_errors(manager.errors.file_messages(graph[id].xpath), False)
errors = manager.errors.file_messages(
graph[id].xpath, formatter=manager.error_formatter)
manager.flush_errors(errors, False)
graph[id].write_cache()
graph[id].mark_as_rechecked()

Expand Down
26 changes: 26 additions & 0 deletions mypy/error_formatter.py
@@ -0,0 +1,26 @@
import json
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from mypy.errors import ErrorTuple


class ErrorFormatter(ABC):
"""Defines how errors are formatted before being printed."""
@abstractmethod
def report_error(self, error: 'ErrorTuple') -> str:
raise NotImplementedError


class JSONFormatter(ErrorFormatter):
def report_error(self, error: 'ErrorTuple') -> str:
file, line, column, severity, message, _, errorcode = error
return json.dumps({
'file': file,
'line': line,
'column': column,
Copy link
Contributor

Choose a reason for hiding this comment

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

line, column seems short-sighed.

Maybe startLine, startColumn, which would leave room to later add endLine, endColumn. This information is useful for IDEs to know how what span to highlight.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, good point.
But mypy currently only does line and column, and it might be very long before spans are added in. It could be argued that the change to startLine and startColumn can be made when the feature exists.

Copy link
Contributor

@intgr intgr Oct 28, 2021

Choose a reason for hiding this comment

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

Changing the field names later will break tools though. And startLine, startColumn would already be accurate right now, because mypy currently points out the start of the span.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll leave this convention for the separate --output=sarif format.

'severity': severity,
'message': message,
'code': None if errorcode is None else errorcode.code,
})
13 changes: 11 additions & 2 deletions mypy/errors.py
Expand Up @@ -6,6 +6,7 @@

from typing import Tuple, List, TypeVar, Set, Dict, Optional, TextIO, Callable
from typing_extensions import Final
from mypy.error_formatter import ErrorFormatter

from mypy.scope import Scope
from mypy.options import Options
Expand Down Expand Up @@ -575,19 +576,27 @@ def format_messages(self, error_info: List[ErrorInfo],
a.append(' ' * (DEFAULT_SOURCE_OFFSET + column) + '^')
return a

def file_messages(self, path: str) -> List[str]:
def file_messages(self, path: str, formatter: Optional[ErrorFormatter] = None) -> List[str]:
"""Return a string list of new error messages from a given file.

Use a form suitable for displaying to the user.
"""
if path not in self.error_info_map:
return []

error_info = self.error_info_map[path]
if formatter is not None:
error_info = [info for info in error_info if not info.hidden]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's not duplicate this and instead pass error_tuples into format_messages

errors = self.render_messages(self.sort_messages(error_info))
errors = self.remove_duplicates(errors)
return [formatter.report_error(err) for err in errors]

self.flushed_files.add(path)
source_lines = None
if self.pretty:
assert self.read_source
source_lines = self.read_source(path)
return self.format_messages(self.error_info_map[path], source_lines)
return self.format_messages(error_info, source_lines)

def new_messages(self) -> List[str]:
"""Return a string list of new error messages.
Expand Down
3 changes: 3 additions & 0 deletions mypy/main.py
Expand Up @@ -490,6 +490,9 @@ def add_invertible_flag(flag: str,
help="Show program's version number and exit",
stdout=stdout)

general_group.add_argument(
'-O', '--output', metavar='FORMAT', help="Set a custom output format")

config_group = parser.add_argument_group(
title='Config file',
description="Use a config file instead of command line arguments. "
Expand Down
3 changes: 3 additions & 0 deletions mypy/options.py
Expand Up @@ -302,6 +302,9 @@ def __init__(self) -> None:
# -1 means unlimited.
self.many_errors_threshold = defaults.MANY_ERRORS_THRESHOLD

# Sets output format
self.output = ""

# To avoid breaking plugin compatibility, keep providing new_semantic_analyzer
@property
def new_semantic_analyzer(self) -> bool:
Expand Down