Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add Error format support, and JSON output option#11396

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
JelleZijlstra merged 48 commits intopython:masterfromtusharsadhwani:output-json
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
Show all changes
48 commits
Select commitHold shift + click to select a range
393820c
Add -O/--output CLI option
tusharsadhwaniOct 24, 2021
282bd28
Initial formatter setup
tusharsadhwaniOct 25, 2021
ccda5b0
Make error_formatter an optional argument
tusharsadhwaniOct 27, 2021
c849a77
Fix type annotation
tusharsadhwaniOct 27, 2021
fd2feab
Fix whitespace
tusharsadhwaniOct 27, 2021
b188001
Remove whitespace
tusharsadhwaniOct 27, 2021
51c1acc
Merge branch 'master' of https://github.com/tusharsadhwani/mypy into …
tusharsadhwaniOct 27, 2021
9177dab
Merge branch 'python:master' into output-json
tushar-deepsourceJan 19, 2022
bc5ceac
Add hint property to errors
tushar-deepsourceJan 19, 2022
9d29ab0
Fix lint issues
tusharsadhwaniJan 19, 2022
bd6d48d
Merge branch 'master' into output-json
tusharsadhwaniFeb 22, 2023
ba8d17f
Fix import and typing issues
tusharsadhwaniFeb 22, 2023
a2bc04d
Fix error tuple signature
tusharsadhwaniFeb 22, 2023
35974e4
Import Optional
tusharsadhwaniFeb 23, 2023
1e5ec91
Run black
tusharsadhwaniFeb 23, 2023
723219f
Run black on another file
tusharsadhwaniFeb 23, 2023
2228c0a
Run isort
tusharsadhwaniFeb 23, 2023
33d81b0
Run isort on build.py
tusharsadhwaniFeb 23, 2023
63001ea
Merge branch 'master' into output-json
tusharsadhwaniApr 19, 2023
d27be7e
Merge branch 'master' into output-json
tusharsadhwaniApr 20, 2023
efe5c5d
Merge branch 'master' into output-json
tusharsadhwaniApr 27, 2023
1872ae6
Add tests for json output
tusharsadhwaniApr 27, 2023
3abc9cb
Suggestions from code review, and negative test
tusharsadhwaniApr 27, 2023
6c9ab11
Add default value of None
tusharsadhwaniApr 27, 2023
627ed8e
Default output to None in options as well
tusharsadhwaniApr 27, 2023
e425cbe
Fix failing tests
tusharsadhwaniApr 27, 2023
47f1b07
improve docstring
tusharsadhwaniApr 27, 2023
e00ad4a
type cast
tusharsadhwaniApr 27, 2023
c1fb6a2
Another explicit type cast
tusharsadhwaniApr 27, 2023
aafe3aa
remove unused import
tusharsadhwaniApr 27, 2023
fae3215
create formatter object
tusharsadhwaniApr 27, 2023
89ad1d3
Add custom end to end test
tusharsadhwaniApr 27, 2023
7a3f736
unused import
tusharsadhwaniApr 27, 2023
6d46f75
trailing whitespace
tusharsadhwaniApr 27, 2023
e71a372
try fixing windows
tusharsadhwaniApr 28, 2023
8cca203
fix windows separator issue
tusharsadhwaniApr 28, 2023
79e16a8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot]Apr 28, 2023
8bf4890
unused import
tusharsadhwaniApr 28, 2023
5899f26
Merge branch 'master' into output-json
tusharsadhwaniApr 28, 2023
880b8f3
Merge branch 'master' into output-json
tusharsadhwaniMay 5, 2023
0aafadf
Pass error tuples to format_messages
tusharsadhwaniMay 10, 2023
4cab249
Merge branch 'master' into output-json
tusharsadhwaniMay 10, 2023
7fe71c3
Merge branch 'master' into output-json
tusharsadhwaniMay 13, 2023
ad8f1d6
Merge branch 'master' into output-json
tusharsadhwaniMay 9, 2024
e2fd45e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot]May 9, 2024
4b03c5c
ruff lints
tusharsadhwaniMay 9, 2024
e0e6896
address comments
tusharsadhwaniMay 10, 2024
a0dc6d1
use severity
tusharsadhwaniMay 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
PrevPrevious commit
NextNext commit
Initial formatter setup
  • Loading branch information
@tusharsadhwani
tusharsadhwani committedOct 25, 2021
commit282bd281089af718d48f00dfc2683e60e17c6a1c
8 changes: 7 additions & 1 deletionmypy/build.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -33,6 +33,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 DownExpand Up@@ -243,6 +244,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 DownExpand Up@@ -577,6 +579,7 @@ def __init__(self, data_dir: str,
plugin: Plugin,
plugins_snapshot: Dict[str, str],
errors: Errors,
error_formatter: 'Optional[ErrorFormatter]',
flush_errors: Callable[[List[str], bool], None],
fscache: FileSystemCache,
stdout: TextIO,
Expand All@@ -589,6 +592,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 DownExpand Up@@ -3154,7 +3158,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
25 changes: 25 additions & 0 deletionsmypy/error_formatter.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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.

MaybestartLine,startColumn, which would leave room to later addendLine,endColumn. This information is useful for IDEs to know how what span to highlight.

Copy link
ContributorAuthor

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 tostartLine andstartColumn can be made when the feature exists.

Copy link
Contributor

@intgrintgrOct 28, 2021
edited
Loading

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. AndstartLine,startColumn would already be accurate right now, because mypy currently points out the start of the span.

tusharsadhwani, agoncharov-reef, and antonagestam reacted with thumbs up emoji
Copy link
ContributorAuthor

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 deletionsmypy/errors.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
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 DownExpand 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: 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 passerror_tuples intoformat_messages

tusharsadhwani reacted with thumbs up emoji
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

[8]ページ先頭

©2009-2025 Movatter.jp