Skip to content

Commit e21a200

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 400b975 commit e21a200

3 files changed

Lines changed: 120 additions & 91 deletions

File tree

pylint/reporters/json_reporter.py

Lines changed: 55 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,20 @@
77
from __future__ import annotations
88

99
import json
10-
import sys
1110
from textwrap import shorten
12-
from typing import TYPE_CHECKING, TypedDict, TextIO
13-
14-
from pylint.constants import MSG_TYPES
11+
from typing import TYPE_CHECKING, TypedDict
1512

1613
import pylint
14+
from pylint.constants import MSG_TYPES
1715
from pylint.interfaces import CONFIDENCE_MAP, UNDEFINED
1816
from pylint.message import Message
1917
from pylint.reporters.base_reporter import BaseReporter
20-
from pylint.typing import MessageLocationTuple, MessageTypesFullName
18+
from pylint.typing import MessageLocationTuple
2119

2220
if TYPE_CHECKING:
2321
from pylint.lint.pylinter import PyLinter
24-
from pylint.reporters.ureports.nodes import Section
2522
from pylint.reporters import sarif_types
23+
from pylint.reporters.ureports.nodes import Section
2624

2725
# Since message-id is an invalid name we need to use the alternative syntax
2826
OldJsonExport = TypedDict(
@@ -218,41 +216,53 @@ def display_messages(self, layout: Section | None) -> None:
218216
output: sarif_types.Log = {
219217
"version": "2.1.0",
220218
"$schema": "https://docs.oasis-open.org/sarif/sarif/v2.1.0/cs01/schemas/sarif-schema-2.1.0.json",
221-
"runs": [{
222-
"tool": {
223-
"driver": {
224-
"name": "pylint",
225-
"fullName": f"pylint {pylint.__version__}",
226-
"version": pylint.__version__,
227-
# should be versioned but not all versions are kept so...
228-
"informationUri": "https://pylint.readthedocs.io/",
229-
"rules": [
230-
{
231-
"id": m.msgid,
232-
"name": m.symbol,
233-
"deprecatedIds": [msgid for msgid, _ in m.old_names],
234-
"deprecatedNames": [name for _, name in m.old_names],
235-
# per 3.19.19 shortDescription should be a
236-
# single sentence which can't be guaranteed,
237-
# however github requires it...
238-
"shortDescription": {'text': m.description.split(".", 1)[0]},
239-
# github requires that this is less than 1024 characters
240-
"fullDescription": {'text': shorten(m.description, 1024, placeholder="...")},
241-
"help": {"text": m.format_help()},
242-
"helpUri": f"https://pylint.readthedocs.io/en/stable/user_guide/messages/{MSG_TYPES[m.msgid[0]]}/{m.symbol}.html"
243-
# handle_message only gets the formatted message,
244-
# so to use `messageStrings` we'd need to
245-
# convert the templating and extract the args
246-
# out of the msg
247-
}
248-
for checker in self.linter.get_checkers()
249-
for m in checker.messages
250-
if m.symbol in self.linter.stats.by_msg
251-
]
252-
}
253-
},
254-
"results": [self.serialize(message) for message in self.messages],
255-
}]
219+
"runs": [
220+
{
221+
"tool": {
222+
"driver": {
223+
"name": "pylint",
224+
"fullName": f"pylint {pylint.__version__}",
225+
"version": pylint.__version__,
226+
# should be versioned but not all versions are kept so...
227+
"informationUri": "https://pylint.readthedocs.io/",
228+
"rules": [
229+
{
230+
"id": m.msgid,
231+
"name": m.symbol,
232+
"deprecatedIds": [
233+
msgid for msgid, _ in m.old_names
234+
],
235+
"deprecatedNames": [
236+
name for _, name in m.old_names
237+
],
238+
# per 3.19.19 shortDescription should be a
239+
# single sentence which can't be guaranteed,
240+
# however github requires it...
241+
"shortDescription": {
242+
"text": m.description.split(".", 1)[0]
243+
},
244+
# github requires that this is less than 1024 characters
245+
"fullDescription": {
246+
"text": shorten(
247+
m.description, 1024, placeholder="..."
248+
)
249+
},
250+
"help": {"text": m.format_help()},
251+
"helpUri": f"https://pylint.readthedocs.io/en/stable/user_guide/messages/{MSG_TYPES[m.msgid[0]]}/{m.symbol}.html",
252+
# handle_message only gets the formatted message,
253+
# so to use `messageStrings` we'd need to
254+
# convert the templating and extract the args
255+
# out of the msg
256+
}
257+
for checker in self.linter.get_checkers()
258+
for m in checker.messages
259+
if m.symbol in self.linter.stats.by_msg
260+
],
261+
}
262+
},
263+
"results": [self.serialize(message) for message in self.messages],
264+
}
265+
],
256266
}
257267
json.dump(output, self.out)
258268

@@ -268,7 +278,7 @@ def serialize(message: Message) -> sarif_types.Result:
268278
location: sarif_types.Location = {
269279
"physicalLocation": {
270280
"artifactLocation": {
271-
"uri": message.path.replace('\\', '/'),
281+
"uri": message.path.replace("\\", "/"),
272282
},
273283
"region": region,
274284
},
@@ -288,9 +298,10 @@ def serialize(message: Message) -> sarif_types.Result:
288298
"partialFingerprints": {
289299
# encoding the node path seems like it would be useful to dedup alerts?
290300
"nodePath/v1": "",
291-
}
301+
},
292302
}
293303

304+
294305
CATEGORY_MAP: dict[str, sarif_types.ResultLevel] = {
295306
"convention": "note",
296307
"refactor": "note",
@@ -301,6 +312,7 @@ def serialize(message: Message) -> sarif_types.Result:
301312
"fatal": "error",
302313
}
303314

315+
304316
def register(linter: PyLinter) -> None:
305317
linter.register_reporter(JSONReporter)
306318
linter.register_reporter(JSON2Reporter)

pylint/reporters/sarif_types.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
As such, this module provides the subset of the SARIF schema necessary for
66
pylint's output, translated to Python types.
77
"""
8+
89
from __future__ import annotations
910

10-
from typing import TypedDict, Literal
11+
from typing import Literal, TypedDict
1112

1213

1314
class Run(TypedDict):
@@ -22,9 +23,11 @@ class Run(TypedDict):
2223
"Log",
2324
{
2425
"version": Literal["2.1.0"],
25-
"$schema": Literal["https://docs.oasis-open.org/sarif/sarif/v2.1.0/cs01/schemas/sarif-schema-2.1.0.json"],
26+
"$schema": Literal[
27+
"https://docs.oasis-open.org/sarif/sarif/v2.1.0/cs01/schemas/sarif-schema-2.1.0.json"
28+
],
2629
"runs": list[Run],
27-
}
30+
},
2831
)
2932

3033

@@ -106,6 +109,7 @@ class PhysicalLocation(TypedDict):
106109
# not required per spec, required by github
107110
region: Region
108111

112+
109113
class ArtifactLocation(TypedDict, total=False):
110114
uri: str
111115
#: id of base URI for resolving relative `uri`
@@ -117,7 +121,9 @@ class LogicalLocation(TypedDict, total=False):
117121
name: str
118122
fullyQualifiedName: str
119123
#: schema is `str` with a bunch of *suggested* terms, of which this is a subset
120-
kind: Literal['function', 'member', 'module', 'parameter', 'returnType', 'type', 'variable']
124+
kind: Literal[
125+
"function", "member", "module", "parameter", "returnType", "type", "variable"
126+
]
121127

122128

123129
class Region(TypedDict):

tests/reporters/unittest_json_reporter.py

Lines changed: 55 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
import pytest
1515

16-
from pylint import checkers, __version__
16+
from pylint import __version__, checkers
1717
from pylint.interfaces import HIGH, UNDEFINED
1818
from pylint.lint import PyLinter
1919
from pylint.message import Message
@@ -264,6 +264,7 @@ def test_json2_result_with_broken_score() -> None:
264264
report_result = json.loads(output.getvalue())
265265
assert "division by zero" in report_result["statistics"]["score"]
266266

267+
267268
def test_simple_sarif():
268269
output = StringIO()
269270
reporter = SARIFReporter(output)
@@ -273,53 +274,63 @@ def test_simple_sarif():
273274
linter.open()
274275
linter.set_current_module("0123")
275276
linter.add_message(
276-
"line-too-long",
277-
line=1,
278-
args=(1, 2),
279-
end_lineno=1,
280-
end_col_offset=4
277+
"line-too-long", line=1, args=(1, 2), end_lineno=1, end_col_offset=4
281278
)
282279
reporter.display_messages(None)
283280
assert json.loads(output.getvalue()) == {
284281
"version": "2.1.0",
285282
"$schema": "https://docs.oasis-open.org/sarif/sarif/v2.1.0/cs01/schemas/sarif-schema-2.1.0.json",
286-
"runs": [{
287-
"tool": {
288-
"driver": {
289-
"name": "pylint",
290-
"fullName": f"pylint {__version__}",
291-
"version": __version__,
292-
"informationUri": "https://pylint.readthedocs.io/",
293-
"rules": [{
294-
"id": "C0301",
295-
"deprecatedIds": [],
296-
"name": "line-too-long",
297-
"deprecatedNames": [],
298-
"shortDescription": {"text": "Used when a line is longer than a given number of characters"},
299-
"fullDescription": {"text": "Used when a line is longer than a given number of characters."},
300-
"help": {"text": ":line-too-long (C0301): *Line too long (%s/%s)*\n Used when a line is longer than a given number of characters."},
301-
"helpUri": "https://pylint.readthedocs.io/en/stable/user_guide/messages/convention/line-too-long.html",
302-
}],
303-
},
304-
},
305-
"results": [{
306-
"ruleId": "C0301",
307-
"message": {"text": "Line too long (1/2)"},
308-
"level": "note",
309-
"locations": [{
310-
"physicalLocation": {
311-
"artifactLocation": {
312-
"uri": "0123",
313-
},
314-
"region": {
315-
"startLine": 1,
316-
"startColumn": 1,
317-
"endLine": 1,
318-
"endColumn": 5,
319-
},
283+
"runs": [
284+
{
285+
"tool": {
286+
"driver": {
287+
"name": "pylint",
288+
"fullName": f"pylint {__version__}",
289+
"version": __version__,
290+
"informationUri": "https://pylint.readthedocs.io/",
291+
"rules": [
292+
{
293+
"id": "C0301",
294+
"deprecatedIds": [],
295+
"name": "line-too-long",
296+
"deprecatedNames": [],
297+
"shortDescription": {
298+
"text": "Used when a line is longer than a given number of characters"
299+
},
300+
"fullDescription": {
301+
"text": "Used when a line is longer than a given number of characters."
302+
},
303+
"help": {
304+
"text": ":line-too-long (C0301): *Line too long (%s/%s)*\n Used when a line is longer than a given number of characters."
305+
},
306+
"helpUri": "https://pylint.readthedocs.io/en/stable/user_guide/messages/convention/line-too-long.html",
307+
}
308+
],
320309
},
321-
}],
322-
"partialFingerprints": {"nodePath/v1": ""},
323-
}]
324-
}]
310+
},
311+
"results": [
312+
{
313+
"ruleId": "C0301",
314+
"message": {"text": "Line too long (1/2)"},
315+
"level": "note",
316+
"locations": [
317+
{
318+
"physicalLocation": {
319+
"artifactLocation": {
320+
"uri": "0123",
321+
},
322+
"region": {
323+
"startLine": 1,
324+
"startColumn": 1,
325+
"endLine": 1,
326+
"endColumn": 5,
327+
},
328+
},
329+
}
330+
],
331+
"partialFingerprints": {"nodePath/v1": ""},
332+
}
333+
],
334+
}
335+
],
325336
}

0 commit comments

Comments
 (0)