Skip to content

Commit

Permalink
Merge pull request #693 from prospector-dev/bandit-config
Browse files Browse the repository at this point in the history
Be able to pass options to Bandit
  • Loading branch information
sbrunner authored Oct 25, 2024
2 parents a38e2ab + a59c82d commit 5547a6c
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .prospector.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,8 @@ pydocstyle:
- D205
- D400
- D401

bandit:
run: true
disable:
- B101 # Use of assert detected.
2 changes: 2 additions & 0 deletions docs/profiles.rst
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,8 @@ The available options are:
+----------------+------------------------+----------------------------------------------+
| bandit | confidence | report only issues of a given confidence |
+----------------+------------------------+----------------------------------------------+
| bandit | -anything-other- | Options pass to Bandit directly |
+----------------+------------------------+----------------------------------------------+
| pyright | level | Minimum diagnostic level (error or warning) |
+----------------+------------------------+----------------------------------------------+
| pyright | project | Path to location of configuration file |
Expand Down
2 changes: 1 addition & 1 deletion prospector/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ def use_external_config(self, _: Any) -> bool:
# global config, but this could be extended in the future
return not self.config.no_external_config

def tool_options(self, tool_name: str) -> dict[str, str]:
def tool_options(self, tool_name: str) -> dict[str, Any]:
tool = getattr(self.profile, tool_name, None)
if tool is None:
return {}
Expand Down
2 changes: 1 addition & 1 deletion prospector/formatters/xunit.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from xml.dom.minidom import Document
from xml.dom.minidom import Document # nosec

from prospector.formatters.base import Formatter

Expand Down
30 changes: 17 additions & 13 deletions prospector/tools/bandit/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING, Any, Optional

from bandit.cli.main import _get_profile, _init_extensions
from bandit.core.config import BanditConfig
Expand All @@ -14,35 +14,39 @@


class BanditTool(ToolBase):
def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
self.manager = None
self.profile = None
self.config_file = None
self.agg_type = "file"
self.severity = 0
self.confidence = 0
manager: Optional[BanditManager] = None
profile: Optional[str] = None
config_file: Optional[str] = None
agg_type = "file"
severity = 0
confidence = 0

def configure(self, prospector_config: "ProspectorConfig", _: Any) -> None:
options = prospector_config.tool_options("bandit")

if "profile" in options:
self.profile = options["profile"] # type: ignore[assignment]
self.profile = options.pop("profile")

if "config" in options:
self.config_file = options["config"] # type: ignore[assignment]
self.config_file = options.pop("config")

if "severity" in options:
self.severity = options["severity"] # type: ignore[assignment]
self.severity = options.pop("severity")
if not 0 <= self.severity <= 2:
raise ValueError(f"severity {self.severity!r} must be between 0 and 2")

if "confidence" in options:
self.confidence = options["confidence"] # type: ignore[assignment]
self.confidence = options.pop("confidence")
if not 0 <= self.confidence <= 2:
raise ValueError(f"confidence {self.confidence!r} must be between 0 and 2")

b_conf = BanditConfig(config_file=self.config_file)
disabled_messages = prospector_config.get_disabled_messages("bandit")
if disabled_messages:
b_conf.config.setdefault("skips", []).extend(disabled_messages)
if options:
b_conf.config.update(options)
b_conf.validate(path="<prospector config>")
profile = _get_profile(b_conf, self.profile, self.config_file)
extension_mgr = _init_extensions()
extension_mgr.validate_profile(profile)
Expand Down
2 changes: 1 addition & 1 deletion prospector/tools/mccabe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def configure(self, prospector_config: "ProspectorConfig", _: Any) -> None:

options = prospector_config.tool_options("mccabe")
if "max-complexity" in options:
self.max_complexity = options["max-complexity"] # type: ignore[assignment]
self.max_complexity = options["max-complexity"]

def run(self, found_files: FileFinder) -> list[Message]:
messages = []
Expand Down
2 changes: 1 addition & 1 deletion prospector/tools/mypy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
def configure(self, prospector_config: "ProspectorConfig", _: Any) -> None:
options = prospector_config.tool_options("mypy")

self.use_dmypy = options.pop("use-dmypy", False) # type: ignore[assignment]
self.use_dmypy = options.pop("use-dmypy", False)

# For backward compatibility
if "follow-imports" not in options:
Expand Down
2 changes: 1 addition & 1 deletion prospector/tools/pyright/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
import subprocess
import subprocess # nosec
from collections.abc import Iterable
from typing import TYPE_CHECKING, Any, Optional

Expand Down

0 comments on commit 5547a6c

Please sign in to comment.