Skip to content

Commit

Permalink
Add more fields in message
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Jan 19, 2025
1 parent 3273f5e commit 87bfa35
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ repos:
pyproject.toml|
)$
- repo: https://github.com/PyCQA/prospector
rev: v1.10.3
rev: v1.13.3
hooks:
- id: prospector
additional_dependencies:
Expand Down
11 changes: 10 additions & 1 deletion prospector/formatters/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,18 @@ def _message_to_dict(self, message: Message) -> dict[str, Any]:
"line": message.location.line,
"character": message.location.character,
}
return {
if message.location.line_end is not None and message.location.line_end != -1:
loc["lineEnd"] = message.location.line_end
if message.location.character_end is not None and message.location.character_end != -1:
loc["characterEnd"] = message.location.character_end
result = {
"source": message.source,
"code": message.code,
"location": loc,
"message": message.message,
"isFixable": message.is_fixable,
}
if message.doc_url:
result["docUrl"] = message.doc_url

return message
3 changes: 3 additions & 0 deletions prospector/formatters/pylint.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ def render_messages(self) -> list[str]:
else f"{template_code}: %(message)s"
)

message_str = message.message.strip()
if message.doc_url:
message_str += f" (See: {message.doc_url})"
output.append(
template
% {
Expand Down
16 changes: 15 additions & 1 deletion prospector/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ def __init__(
function: Optional[str],
line: Optional[int],
character: Optional[int],
line_end: Optional[int] = None,
character_end: Optional[int] = None,
):
if isinstance(path, Path):
self._path = path.absolute()
Expand All @@ -25,6 +27,8 @@ def __init__(
self.function = function or None
self.line = None if line == -1 else line
self.character = None if character == -1 else character
self.line_end = line_end
self.character_end = character_end

@property
def path(self) -> Optional[Path]:
Expand Down Expand Up @@ -69,11 +73,21 @@ def __lt__(self, other: "Location") -> bool:


class Message:
def __init__(self, source: str, code: str, location: Location, message: str):
def __init__(
self,
source: str,
code: str,
location: Location,
message: str,
doc_url: Optional[str] = None,
is_fixable: bool = False,
):
self.source = source
self.code = code
self.location = location
self.message = message
self.doc_url = doc_url
self.is_fixable = is_fixable

def __repr__(self) -> str:
return f"{self.source}-{self.code}"
Expand Down
10 changes: 9 additions & 1 deletion prospector/tools/bandit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,15 @@ def run(self, found_files: FileFinder) -> list[Message]:
results = self.manager.get_issue_list(sev_level=RANKING[self.severity], conf_level=RANKING[self.confidence])
messages = []
for result in results:
loc = Location(result.fname, None, "", int(result.lineno), 0)
loc = Location(
result.fname,
None,
"",
result.lineno,
result.col_offset,
line_end=result.linerange[-1] if result.linerange else result.lineno,
character_end=result.end_col_offset,
)
msg = Message("bandit", result.test_id, loc, result.text)
messages.append(msg)
return messages
2 changes: 1 addition & 1 deletion prospector/tools/mypy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def configure(self, prospector_config: "ProspectorConfig", _: Any) -> None:
self.options.append(f"--{name}-{v}")
continue

raise BadToolConfig("mypy", f"The option {name} has an unsupported balue type: {type(value)}")
raise BadToolConfig("mypy", f"The option {name} has an unsupported value type: {type(value)}")

def run(self, found_files: FileFinder) -> list[Message]:
paths = [str(path) for path in found_files.python_modules]
Expand Down
2 changes: 1 addition & 1 deletion prospector/tools/pylint/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __init__(self, message_store: MessageDefinitionStore) -> None:
self._messages: list[Message] = []

def handle_message(self, msg: PylintMessage) -> None:
loc = Location(msg.abspath, msg.module, msg.obj, msg.line, msg.column)
loc = Location(msg.abspath, msg.module, msg.obj, msg.line, msg.column, msg.end_line, msg.end_column)

# At this point pylint will give us the code but we want the
# more user-friendly symbol
Expand Down
6 changes: 4 additions & 2 deletions prospector/tools/ruff/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ def run(self, found_files: FileFinder) -> list[Message]:
return messages
for message in json.loads(completed_process.stdout):
sub_message = {}
if message.get("url"):
sub_message["See"] = message["url"]
if message.get("fix") and message["fix"].get("applicability"):
sub_message["Fix applicability"] = message["fix"]["applicability"]
message_str = message.get("message", "")
Expand All @@ -77,8 +75,12 @@ def run(self, found_files: FileFinder) -> list[Message]:
None,
line=message.get("location", {}).get("row"),
character=message.get("location", {}).get("column"),
line_end=message.get("end_location", {}).get("row"),
character_end=message.get("end_location", {}).get("column"),
),
message_str,
doc_url=message.get("url"),
is_fixable=bool((message.get("fix") or {}).get("applicability") in ("safe", "unsafe")),
)
)
return messages

0 comments on commit 87bfa35

Please sign in to comment.