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 more fields in messages #713

Merged
merged 1 commit into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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 result
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
13 changes: 11 additions & 2 deletions prospector/tools/bandit/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import TYPE_CHECKING, Any, Optional

from bandit.cli.main import _get_profile, _init_extensions
from bandit.core import docs_utils
from bandit.core.config import BanditConfig
from bandit.core.constants import RANKING
from bandit.core.manager import BanditManager
Expand Down Expand Up @@ -66,7 +67,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)
msg = Message("bandit", result.test_id, loc, result.text)
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, doc_url=docs_utils.get_url(result.test_id))
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
10 changes: 8 additions & 2 deletions 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 All @@ -31,7 +31,13 @@ def handle_message(self, msg: PylintMessage) -> None:
else:
msg_symbol = msg_data[0].symbol

message = Message("pylint", msg_symbol, loc, msg.msg)
message = Message(
"pylint",
msg_symbol,
loc,
msg.msg,
doc_url=f"https://pylint.readthedocs.io/en/latest/user_guide/messages/{msg.category}/{msg.symbol}.html",
)
self._messages.append(message)

def get_messages(self) -> list[Message]:
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")),
Copy link
Member

@carlio carlio Jan 19, 2025

Choose a reason for hiding this comment

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

I think this could be a bit cleaner -

message.get("fix", {}) instead of message.get("fix") or {}

but also moving the whole thing onto a separate line just to make it clearer

Copy link
Member Author

Choose a reason for hiding this comment

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

Your proposition is my initial code, but it's not working because fix is not missing but is None!

)
)
return messages
Loading