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

Update pre-commit hooks #1026

Merged
merged 3 commits into from
May 11, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
exclude: ^(examples|tools|doc)
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
rev: v4.4.0
hooks:
- id: check-yaml
- id: debug-statements
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/asottile/reorder_python_imports
rev: v2.6.0
rev: v3.9.0
hooks:
- id: reorder-python-imports
args: [--application-directories, '.:src', --py37-plus]
- repo: https://github.com/psf/black
rev: 22.3.0
rev: 23.3.0
hooks:
- id: black
args: [--line-length=79, --target-version=py37]
- repo: https://github.com/asottile/pyupgrade
rev: v2.29.0
rev: v3.3.2
hooks:
- id: pyupgrade
args: [--py37-plus]
- repo: https://github.com/jorisroovers/gitlint
rev: v0.16.0
rev: v0.19.1
hooks:
- id: gitlint
#- repo: https://github.com/pre-commit/mirrors-mypy
Expand Down
1 change: 0 additions & 1 deletion bandit/cli/baseline.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ def main():
)

with baseline_setup() as t:

bandit_tmpfile = f"{t}/{baseline_tmp_file}"

steps = [
Expand Down
1 change: 0 additions & 1 deletion bandit/core/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@


class BanditManager:

scope = []

def __init__(
Expand Down
1 change: 0 additions & 1 deletion bandit/core/meta_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@


class BanditMetaAst:

nodes = collections.OrderedDict()

def __init__(self):
Expand Down
2 changes: 1 addition & 1 deletion bandit/core/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def _get_issue_counts(scores):
"""
issue_counts = {}
for score in scores:
for (criteria, _) in constants.CRITERIA:
for criteria, _ in constants.CRITERIA:
for i, rank in enumerate(constants.RANKING):
label = f"{criteria}.{rank}"
if label not in issue_counts:
Expand Down
2 changes: 1 addition & 1 deletion bandit/formatters/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def report(manager, fileobj, sev_level, conf_level, template=None):
"""

machine_output = {"results": [], "errors": []}
for (fname, reason) in manager.get_skipped():
for fname, reason in manager.get_skipped():
machine_output["errors"].append({"filename": fname, "reason": reason})

results = manager.get_issue_list(
Expand Down
2 changes: 1 addition & 1 deletion bandit/formatters/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def report(manager, fileobj, sev_level, conf_level, lines=-1):
"""

machine_output = {"results": [], "errors": []}
for (fname, reason) in manager.get_skipped():
for fname, reason in manager.get_skipped():
machine_output["errors"].append({"filename": fname, "reason": reason})

results = manager.get_issue_list(
Expand Down
2 changes: 1 addition & 1 deletion bandit/formatters/screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def get_verbose_details(manager):
def get_metrics(manager):
bits = []
bits.append(header("\nRun metrics:"))
for (criteria, _) in constants.CRITERIA:
for criteria, _ in constants.CRITERIA:
bits.append(f"\tTotal issues (by {criteria.lower()}):")
for rank in constants.RANKING:
bits.append(
Expand Down
2 changes: 1 addition & 1 deletion bandit/formatters/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def get_verbose_details(manager):
def get_metrics(manager):
bits = []
bits.append("\nRun metrics:")
for (criteria, _) in constants.CRITERIA:
for criteria, _ in constants.CRITERIA:
bits.append(f"\tTotal issues (by {criteria.lower()}):")
for rank in constants.RANKING:
bits.append(
Expand Down
2 changes: 1 addition & 1 deletion bandit/formatters/xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"""
import logging
import sys
from xml.etree import cElementTree as ET
from xml.etree import ElementTree as ET

from bandit.core import docs_utils

Expand Down
2 changes: 1 addition & 1 deletion bandit/formatters/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def report(manager, fileobj, sev_level, conf_level, lines=-1):
"""

machine_output = {"results": [], "errors": []}
for (fname, reason) in manager.get_skipped():
for fname, reason in manager.get_skipped():
machine_output["errors"].append({"filename": fname, "reason": reason})

results = manager.get_issue_list(
Expand Down
1 change: 0 additions & 1 deletion bandit/plugins/injection_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,6 @@ def start_process_with_partial_path(context, config):
or context.call_function_name_qual in config["shell"]
or context.call_function_name_qual in config["no_shell"]
):

node = context.node.args[0]
# some calls take an arg list, check the first part
if isinstance(node, ast.List):
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def check_metrics(self, example_script, expect):
self.assertEqual(expect[k], m["_totals"][k])
# test issue counts
if "issues" in expect:
for (criteria, default) in C.CRITERIA:
for criteria, default in C.CRITERIA:
for rank in C.RANKING:
label = f"{criteria}.{rank}"
expected = 0
Expand Down
16 changes: 8 additions & 8 deletions tests/unit/cli/test_baseline.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def test_bandit_baseline(self):
git_repo.index.commit("Initial commit")
os.chdir(repo_directory)

with open("bandit.yaml", "wt") as fd:
with open("bandit.yaml", "w") as fd:
fd.write(config)

# create three branches, first has only benign, second adds malicious,
Expand Down Expand Up @@ -111,7 +111,7 @@ def test_bandit_baseline(self):
git_repo.head.reset(working_tree=True)

for f in branch["files"]:
with open(f, "wt") as fd:
with open(f, "w") as fd:
fd.write(contents[f])

git_repo.index.add(branch["files"])
Expand All @@ -138,7 +138,7 @@ def test_main_git_command_failure(self):
os.chdir(repo_directory)

additional_content = "additional_file.py"
with open(additional_content, "wt") as fd:
with open(additional_content, "w") as fd:
fd.write(self.temp_file_contents)
git_repo.index.add([additional_content])
git_repo.index.commit("Additional Content")
Expand Down Expand Up @@ -173,7 +173,7 @@ def test_main_subprocess_error(self):
os.chdir(repo_directory)

additional_content = "additional_file.py"
with open(additional_content, "wt") as fd:
with open(additional_content, "w") as fd:
fd.write(self.temp_file_contents)
git_repo.index.add([additional_content])
git_repo.index.commit("Additional Content")
Expand Down Expand Up @@ -215,7 +215,7 @@ def test_initialize_git_command_failure(self):
os.chdir(repo_directory)

additional_content = "additional_file.py"
with open(additional_content, "wt") as fd:
with open(additional_content, "w") as fd:
fd.write(self.temp_file_contents)
git_repo.index.add([additional_content])
git_repo.index.commit("Additional Content")
Expand All @@ -237,7 +237,7 @@ def test_initialize_dirty_repo(self):
os.chdir(repo_directory)

# make the git repo 'dirty'
with open("dirty_file.py", "wt") as fd:
with open("dirty_file.py", "w") as fd:
fd.write(self.temp_file_contents)
git_repo.index.add(["dirty_file.py"])

Expand All @@ -258,7 +258,7 @@ def test_initialize_existing_report_file(self):

# create an existing version of output report file
existing_report = f"{baseline.report_basename}.txt"
with open(existing_report, "wt") as fd:
with open(existing_report, "w") as fd:
fd.write(self.temp_file_contents)

return_value = baseline.initialize()
Expand Down Expand Up @@ -292,7 +292,7 @@ def test_initialize_existing_temp_file(self):

# create an existing version of temporary output file
existing_temp_file = baseline.baseline_tmp_file
with open(existing_temp_file, "wt") as fd:
with open(existing_temp_file, "w") as fd:
fd.write(self.temp_file_contents)

return_value = baseline.initialize()
Expand Down
24 changes: 12 additions & 12 deletions tests/unit/cli/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def test_get_options_from_ini_no_ini_path_multi_bandit_files(self):
)
bandit_files = [bandit_config_one, bandit_config_two]
for bandit_file in bandit_files:
with open(bandit_file, "wt") as fd:
with open(bandit_file, "w") as fd:
fd.write(bandit_config_content)
self.assertRaisesRegex(
SystemExit,
Expand Down Expand Up @@ -199,7 +199,7 @@ def test_main_handle_ini_options(self):
# Test that bandit handles cmdline args from a bandit.yaml file
temp_directory = self.useFixture(fixtures.TempDir()).path
os.chdir(temp_directory)
with open("bandit.yaml", "wt") as fd:
with open("bandit.yaml", "w") as fd:
fd.write(bandit_config_content)
with mock.patch(
"bandit.cli.main._get_options_from_ini"
Expand All @@ -225,7 +225,7 @@ def test_main_unknown_tests(self):
# Test that bandit exits when an invalid test ID is provided
temp_directory = self.useFixture(fixtures.TempDir()).path
os.chdir(temp_directory)
with open("bandit.yaml", "wt") as fd:
with open("bandit.yaml", "w") as fd:
fd.write(bandit_config_content)
# assert a SystemExit with code 2
self.assertRaisesRegex(SystemExit, "2", bandit.main)
Expand All @@ -237,7 +237,7 @@ def test_main_unknown_skip_tests(self):
# Test that bandit exits when an invalid test ID is provided to skip
temp_directory = self.useFixture(fixtures.TempDir()).path
os.chdir(temp_directory)
with open("bandit.yaml", "wt") as fd:
with open("bandit.yaml", "w") as fd:
fd.write(bandit_config_content)
# assert a SystemExit with code 2
self.assertRaisesRegex(SystemExit, "2", bandit.main)
Expand All @@ -249,7 +249,7 @@ def test_main_profile_not_found(self):
# Test that bandit exits when an invalid profile name is provided
temp_directory = self.useFixture(fixtures.TempDir()).path
os.chdir(temp_directory)
with open("bandit.yaml", "wt") as fd:
with open("bandit.yaml", "w") as fd:
fd.write(bandit_config_content)
# assert a SystemExit with code 2
with mock.patch("bandit.cli.main.LOG.error") as err_mock:
Expand All @@ -267,9 +267,9 @@ def test_main_baseline_ioerror(self):
# baseline data
temp_directory = self.useFixture(fixtures.TempDir()).path
os.chdir(temp_directory)
with open("bandit.yaml", "wt") as fd:
with open("bandit.yaml", "w") as fd:
fd.write(bandit_config_content)
with open("base.json", "wt") as fd:
with open("base.json", "w") as fd:
fd.write(bandit_baseline_content)
with mock.patch(
"bandit.core.manager.BanditManager.populate_baseline"
Expand All @@ -295,9 +295,9 @@ def test_main_invalid_output_format(self):
# Test that bandit exits when an invalid output format is selected
temp_directory = self.useFixture(fixtures.TempDir()).path
os.chdir(temp_directory)
with open("bandit.yaml", "wt") as fd:
with open("bandit.yaml", "w") as fd:
fd.write(bandit_config_content)
with open("base.json", "wt") as fd:
with open("base.json", "w") as fd:
fd.write(bandit_baseline_content)
# assert a SystemExit with code 2
self.assertRaisesRegex(SystemExit, "2", bandit.main)
Expand All @@ -309,7 +309,7 @@ def test_main_exit_with_results(self):
# Test that bandit exits when there are results
temp_directory = self.useFixture(fixtures.TempDir()).path
os.chdir(temp_directory)
with open("bandit.yaml", "wt") as fd:
with open("bandit.yaml", "w") as fd:
fd.write(bandit_config_content)
with mock.patch(
"bandit.core.manager.BanditManager.results_count"
Expand All @@ -325,7 +325,7 @@ def test_main_exit_with_no_results(self):
# Test that bandit exits when there are no results
temp_directory = self.useFixture(fixtures.TempDir()).path
os.chdir(temp_directory)
with open("bandit.yaml", "wt") as fd:
with open("bandit.yaml", "w") as fd:
fd.write(bandit_config_content)
with mock.patch(
"bandit.core.manager.BanditManager.results_count"
Expand All @@ -342,7 +342,7 @@ def test_main_exit_with_results_and_with_exit_zero_flag(self):
# Test that bandit exits with 0 on results and zero flag
temp_directory = self.useFixture(fixtures.TempDir()).path
os.chdir(temp_directory)
with open("bandit.yaml", "wt") as fd:
with open("bandit.yaml", "w") as fd:
fd.write(bandit_config_content)
with mock.patch(
"bandit.core.manager.BanditManager.results_count"
Expand Down
1 change: 0 additions & 1 deletion tests/unit/core/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@ def test_blacklist_error(self):
"blacklist_imports",
"blacklist_imports_func",
]:

self.config._config = {"profiles": {"test": {"include": [name]}}}
try:
self.config.validate("")
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/core/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ def test_run_tests_keyboardinterrupt(self):
# Test that bandit manager exits when there is a keyboard interrupt
temp_directory = self.useFixture(fixtures.TempDir()).path
some_file = os.path.join(temp_directory, "some_code_file.py")
with open(some_file, "wt") as fd:
with open(some_file, "w") as fd:
fd.write("some_code = x + 1")
self.manager.files_list = [some_file]
with mock.patch(
Expand Down
1 change: 0 additions & 1 deletion tests/unit/core/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,6 @@ def test_deepgetattr(self):
self.assertRaises(AttributeError, b_utils.deepgetattr, a.b, "z")

def test_parse_ini_file(self):

tests = [
{
"content": "[bandit]\nexclude=/abc,/def",
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/formatters/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def setUp(self):
# mock up the metrics
for key in ["_totals", "binding.py"]:
self.manager.metrics.data[key] = {"loc": 4, "nosec": 2}
for (criteria, default) in constants.CRITERIA:
for criteria, default in constants.CRITERIA:
for rank in constants.RANKING:
self.manager.metrics.data[key][f"{criteria}.{rank}"] = 0

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/formatters/test_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# SPDX-License-Identifier: Apache-2.0
import collections
import tempfile
from xml.etree import cElementTree as ET
from xml.etree import ElementTree as ET

import testtools

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/formatters/test_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def setUp(self):
# mock up the metrics
for key in ["_totals", "binding.py"]:
self.manager.metrics.data[key] = {"loc": 4, "nosec": 2}
for (criteria, default) in constants.CRITERIA:
for criteria, default in constants.CRITERIA:
for rank in constants.RANKING:
self.manager.metrics.data[key][f"{criteria}.{rank}"] = 0

Expand Down