Skip to content

Commit 55f779a

Browse files
committed
add pre-commit and pyupgrade to CI
1 parent a0527a5 commit 55f779a

File tree

15 files changed

+54
-25
lines changed

15 files changed

+54
-25
lines changed

.github/workflows/bump-version.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
new_tag=$(echo $latest_tag | awk -F. -v a="$1" -v b="$2" -v c="$3" '{printf("%d.%d.%d", $1+a, $2+b , $3+1)}')
2525
echo "new tag: $new_tag"
2626
27-
printf "# pylint: disable=missing-module-docstring\n__version__ = '$new_tag'""" > $version_file
27+
printf "# pylint: disable=missing-module-docstring\n__version__ = \"$new_tag\"\n""" > $version_file
2828
2929
git commit -m "Bump to ${new_tag}" $version_file || echo "No changes to commit"
3030
git push origin

.github/workflows/ci.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,17 @@ permissions:
1313
contents: read
1414

1515
jobs:
16+
pre-commit:
17+
runs-on: ubuntu-latest
18+
steps:
19+
- uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4.0.0
20+
- uses: actions/setup-python@61a6322f88396a6271a6ee3565807d608ecaddd1 # v4.7.0
21+
with:
22+
python-version: '3.8' # needed for 'pyupgrade'
23+
- uses: pre-commit/action@646c83fcd040023954eafda54b4db0192ce70507 # v3.0.0
24+
1625
ci:
26+
needs: pre-commit
1727
runs-on: ubuntu-latest
1828
timeout-minutes: 15
1929
steps:
@@ -42,6 +52,7 @@ jobs:
4252
- run: invoke build.uninstall-package
4353

4454
python-version:
55+
needs: pre-commit
4556
if: github.event_name == 'pull_request'
4657
runs-on: ubuntu-latest
4758
timeout-minutes: 15

.pre-commit-config.yaml

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
- repo: git://github.com/antonbabenko/pre-commit-terraform
2-
rev: v1.44.0 # Get the latest from: https://github.com/antonbabenko/pre-commit-terraform/releases
3-
hooks:
4-
- id: terraform_fmt
5-
- id: terraform_docs
6-
# args: ['--sort-by-required', '--no-providers']
1+
repos:
2+
- repo: https://github.com/antonbabenko/pre-commit-terraform
3+
rev: v1.83.3
4+
hooks:
5+
- id: terraform_fmt
6+
# - id: terraform_docs
7+
# args: ['--sort-by-required', '--no-providers']
8+
- repo: https://github.com/asottile/pyupgrade
9+
rev: v3.11.0
10+
hooks:
11+
- id: pyupgrade
12+
args: ["--py37-plus"]

.pylintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ disable=raw-checker-failed,
8989
f-string-without-interpolation,
9090
logging-fstring-interpolation,
9191
unused-variable,
92+
broad-exception-raised,
9293

9394
# Enable the message, report, category or checker with the given id(s). You can
9495
# either give multiple identifier separated by comma (,) or put this option

policy_sentry/bin/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# pylint: disable=missing-module-docstring
2-
__version__ = '0.12.10'
2+
__version__ = "0.12.10"

policy_sentry/command/create_template.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515

1616
@click.command(
17-
context_settings=dict(max_content_width=160),
17+
context_settings={"max_content_width": 160},
1818
short_help="Create write-policy YML template files",
1919
)
2020
@click.option(

policy_sentry/command/query.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ def query_action_table(
157157
print_dict(output=output, fmt=fmt)
158158
else:
159159
print("All services in the database:\n")
160-
output = all_services # type:ignore[assignment] # it is a set here, which is ok
160+
# it is a set here, which is ok
161+
output = all_services # type:ignore[assignment]
161162
print_list(output=output, fmt=fmt)
162163
elif name is None and access_level and not resource_type:
163164
print(
@@ -250,7 +251,9 @@ def arn_table(
250251
query_arn_table(name, service, list_arn_types, fmt)
251252

252253

253-
def query_arn_table(name: str, service: str, list_arn_types: bool, fmt: str) -> list[str] | dict[str, str]:
254+
def query_arn_table(
255+
name: str, service: str, list_arn_types: bool, fmt: str
256+
) -> list[str] | dict[str, str]:
254257
"""Query the ARN Table from the Policy Sentry database. Use this one when leveraging Policy Sentry as a library."""
255258
if os.path.exists(LOCAL_DATASTORE_FILE_PATH):
256259
logger.info(
@@ -309,7 +312,9 @@ def condition_table(name: str, service: str, fmt: str, verbose: str | None) -> N
309312
query_condition_table(name, service, fmt)
310313

311314

312-
def query_condition_table(name: str, service: str, fmt: str = "json") -> list[str] | dict[str, str]:
315+
def query_condition_table(
316+
name: str, service: str, fmt: str = "json"
317+
) -> list[str] | dict[str, str]:
313318
"""Query the condition table from the Policy Sentry database.
314319
Use this one when leveraging Policy Sentry as a library."""
315320
if os.path.exists(LOCAL_DATASTORE_FILE_PATH):

policy_sentry/querying/conditions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def get_condition_keys_for_service(service_prefix: str) -> list[str]:
2626
List: A list of condition keys
2727
"""
2828
service_prefix_data = get_service_prefix_data(service_prefix)
29-
results = [condition for condition in service_prefix_data["conditions"]]
29+
results = list(service_prefix_data["conditions"])
3030
return results
3131

3232

policy_sentry/shared/awsdocs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def create_database(
183183
if not filename.startswith("list_"):
184184
continue
185185

186-
with open(os.path.join(BUNDLED_HTML_DIRECTORY_PATH, filename), "r") as f:
186+
with open(os.path.join(BUNDLED_HTML_DIRECTORY_PATH, filename)) as f:
187187
soup = BeautifulSoup(f.read(), "html.parser")
188188
main_content = soup.find(id="main-content")
189189
if not isinstance(main_content, Tag):
@@ -357,7 +357,7 @@ def create_database(
357357
cells = row.find_all("td")
358358

359359
if "[permission only]" in priv:
360-
priv = priv.split(" ")[0]
360+
priv = priv.split(" ", maxsplit=1)[0]
361361

362362
privilege_schema = {
363363
"privilege": priv,

policy_sentry/shared/constants.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@
2929
# Check for the existence of the local datastore first.
3030
if os.path.exists(LOCAL_DATASTORE_FILE_PATH):
3131
# If it exists, leverage that datastore instead of the one bundled with the python package
32-
logger.info(f"Leveraging the local IAM definition at the path: {LOCAL_DATASTORE_FILE_PATH} "
33-
f"To leverage the bundled definition instead, remove the folder $HOME/.policy_sentry/")
32+
logger.info(
33+
f"Leveraging the local IAM definition at the path: {LOCAL_DATASTORE_FILE_PATH} "
34+
f"To leverage the bundled definition instead, remove the folder $HOME/.policy_sentry/"
35+
)
3436
DATASTORE_FILE_PATH = LOCAL_DATASTORE_FILE_PATH
3537
else:
3638
# Otherwise, leverage the datastore inside the python package
@@ -39,7 +41,7 @@
3941

4042
# Overrides
4143
if "CUSTOM_ACCESS_OVERRIDES_FILE" in os.environ:
42-
CUSTOM_ACCESS_OVERRIDES_FILE=os.environ['CUSTOM_ACCESS_OVERRIDES_FILE']
44+
CUSTOM_ACCESS_OVERRIDES_FILE = os.environ["CUSTOM_ACCESS_OVERRIDES_FILE"]
4345
BUNDLED_ACCESS_OVERRIDES_FILE = os.path.join(
4446
os.path.abspath(os.path.dirname(__file__)), CUSTOM_ACCESS_OVERRIDES_FILE
4547
)

policy_sentry/shared/iam_data.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222

2323
@functools.lru_cache(maxsize=1)
2424
def get_iam_definition_schema_version() -> str:
25+
"""
26+
Returns the schema version of the IAM datastore
27+
"""
28+
2529
return cast(
2630
"str",
2731
iam_definition.get(

policy_sentry/util/file.py

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

requirements-dev.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# CI
2-
pre-commit==3.4.0
2+
pre-commit==2.21.0
33
# Unit Testing
44
pytest==7.4.0
55
pylint==2.17.5

terraform_module/ps-template/main.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ resource "local_file" "template" {
3434
}
3535

3636
resource "random_string" "random_template_name" {
37-
length = 24
38-
special = false
37+
length = 24
38+
special = false
3939
}
4040

4141
data "external" "policy" {

test/writing/test_sid_group_crud.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
)
1717
)
1818

19-
with open(crud_with_override_template, "r") as yaml_file:
19+
with open(crud_with_override_template) as yaml_file:
2020
crud_with_override_template_cfg = yaml.safe_load(yaml_file)
2121

2222

@@ -446,7 +446,7 @@ def test_exclude_actions_from_crud_output(self):
446446
"crud-with-exclude-actions.yml",
447447
)
448448
)
449-
with open(crud_with_exclude_actions, "r") as this_yaml_file:
449+
with open(crud_with_exclude_actions) as this_yaml_file:
450450
crud_with_exclude_actions_cfg = yaml.safe_load(this_yaml_file)
451451
sid_group.process_template(crud_with_exclude_actions_cfg)
452452
result = sid_group.get_rendered_policy(crud_with_exclude_actions_cfg)
@@ -557,7 +557,7 @@ def test_exclude_actions_empty_sid_from_crud_output(self):
557557
)
558558
)
559559

560-
with open(crud_with_exclude_actions_empty_sid, "r") as this_yaml_file:
560+
with open(crud_with_exclude_actions_empty_sid) as this_yaml_file:
561561
crud_with_exclude_actions_empty_sid_cfg = yaml.safe_load(this_yaml_file)
562562
# crud_with_exclude_actions_empty_sid_cfg = {
563563
# "mode": "crud",

0 commit comments

Comments
 (0)