Skip to content

Commit 208d420

Browse files
committed
update ruff and mypy and fix issues
1 parent bd97236 commit 208d420

File tree

9 files changed

+23
-19
lines changed

9 files changed

+23
-19
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ repos:
1010
# - id: terraform_docs
1111
# args: ['--sort-by-required', '--no-providers']
1212
- repo: https://github.com/astral-sh/ruff-pre-commit
13-
rev: v0.5.2
13+
rev: v0.5.3
1414
hooks:
1515
- id: ruff
1616
files: ^policy_sentry/

policy_sentry/command/create_template.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ def create_template(output_file: str | Path, template_type: str, verbose: str) -
5454
filename = Path(output_file).resolve()
5555
if template_type == "actions":
5656
actions_template = create_actions_template()
57-
with open(filename, "a") as file_obj:
57+
with open(filename, "a", encoding="utf-8") as file_obj:
5858
for line in actions_template:
5959
file_obj.write(line)
6060

6161
if template_type == "crud":
6262
crud_template = create_crud_template()
63-
with open(filename, "a") as file_obj:
63+
with open(filename, "a", encoding="utf-8") as file_obj:
6464
for line in crud_template:
6565
file_obj.write(line)
6666

policy_sentry/command/write_policy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ def parse_args(self, ctx: Context, args: list[str]) -> list[str]:
5353
if a_tuple[0] in prefixes:
5454
if len(a_tuple) > 1:
5555
args[i] = a_tuple[0]
56-
args.insert(i + 1, a_tuple[0] + "_length=" + a_tuple[1])
56+
args.insert(i + 1, a_tuple[0] + "_length=" + a_tuple[1]) # noqa: B909
5757
else:
5858
# check if next argument is naked
5959
if len(args) > i + 1 and not args[i + 1].startswith("--"):
6060
value = args[i + 1]
61-
args[i + 1] = a_tuple[0] + "_length=" + value
61+
args[i + 1] = a_tuple[0] + "_length=" + value # noqa: B909
6262
return super().parse_args(ctx, args)
6363

6464

policy_sentry/shared/awsdocs.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ def update_html_docs_directory(html_docs_destination: str) -> None:
131131
logger.warning(a_e)
132132
logger.warning(script)
133133

134-
with open(os.path.join(html_docs_destination, page), "w") as file:
134+
with open(
135+
os.path.join(html_docs_destination, page), "w", encoding="utf-8"
136+
) as file:
135137
# file.write(str(soup.html))
136138
file.write(str(soup.prettify()))
137139
file.close()
@@ -449,6 +451,6 @@ def create_database(
449451
# schema.update(this_service_schema)
450452

451453
iam_definition_file = os.path.join(destination_directory, "iam-definition.json")
452-
with open(iam_definition_file, "w") as file:
454+
with open(iam_definition_file, "w", encoding="utf-8") as file:
453455
json.dump(schema, file, indent=2)
454-
logger.info("Wrote IAM definition file to path: ", iam_definition_file)
456+
logger.info(f"Wrote IAM definition file to path: {iam_definition_file}")

policy_sentry/util/file.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def read_yaml_file(filename: str | Path) -> dict[str, Any]:
2020
:param filename: name of the yaml file
2121
:return: dictionary of YAML file contents
2222
"""
23-
with open(filename) as yaml_file:
23+
with open(filename, encoding="utf-8") as yaml_file:
2424
try:
2525
cfg = cast("dict[str, Any]", yaml.safe_load(yaml_file))
2626
except yaml.YAMLError as exc:

policy_sentry/util/policy_files.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
from __future__ import annotations
44

5-
import json
65
import logging
76
from pathlib import Path
87
from typing import Any
98

9+
import orjson
10+
1011
from policy_sentry.querying.actions import get_action_data
1112

1213
logger = logging.getLogger(__name__)
@@ -69,11 +70,10 @@ def get_actions_from_json_policy_file(file: str | Path) -> list[str]:
6970

7071
# FIXME use a try/expect here to validate the json file. I would create a generic json
7172
try:
72-
with open(file) as json_file:
73-
# validation function/parser as there is a lot of json floating around
74-
# in this tool. [MJ]
75-
data = json.load(json_file)
76-
actions_list = get_actions_from_policy(data)
73+
# validation function/parser as there is a lot of json floating around
74+
# in this tool. [MJ]
75+
data = orjson.loads(Path(file).read_bytes())
76+
actions_list = get_actions_from_policy(data)
7777
except: # noqa: E722
7878
logger.debug("General Error at get_actions_from_json_policy_file.")
7979
actions_list = []

policy_sentry/writing/minimize.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ def minimize_statement_actions(
108108
continue
109109
# If the action name is not empty
110110
if prefix not in denied_prefixes and permission:
111-
if prefix not in desired_actions:
112-
prefix = f"{prefix}*"
113-
minimized_actions.add(prefix)
111+
minimized_actions.add(
112+
prefix if prefix in desired_actions else f"{prefix}*"
113+
)
114114
found_prefix = True
115115
break
116116

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ select = [
2828
"N",
2929
"PERF",
3030
"PIE",
31+
"PLE",
32+
"PLW",
3133
"RUF",
3234
"S",
3335
"SIM",

requirements-dev.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ coverage==7.6.0
66
# Integration tests and tasks
77
invoke==2.2.0
88
# Type hints
9-
mypy==1.10.1
9+
mypy==1.11.0
1010
types-pyyaml==6.0.12.20240311
1111
types-requests==2.32.0.20240712
1212
types-beautifulsoup4==4.12.0.20240511

0 commit comments

Comments
 (0)