Skip to content

Commit 593fe3d

Browse files
committed
Implement review suggestions and fix leftovers
1 parent 49f5087 commit 593fe3d

File tree

8 files changed

+39
-25
lines changed

8 files changed

+39
-25
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,5 @@ pyrightconfig.json
177177
# End of https://www.toptal.com/developers/gitignore/api/python
178178

179179
tests/results/*
180+
.python-version
181+
uv.lock

.pre-commit-config.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ repos:
105105
args:
106106
- --fix
107107
- id: ruff-format
108-
exclude: /terraform_docs_replace(_test)?\.py$
109108

110109
- repo: https://github.com/pre-commit/mirrors-mypy.git
111110
rev: v1.15.0

ruff.toml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ convention = "pep257"
1818
select = ["ALL"]
1919
preview = true
2020
ignore = [
21-
"CPY001", # Missing copyright notice at top of file
21+
"CPY001", # Skip copyright notice requirement at top of files
22+
"S404", # Allow importing 'subprocess' module to call external tools needed by these hooks
2223
]
2324

2425
[lint.isort]
@@ -29,11 +30,10 @@ lines-after-imports = 2
2930
parametrize-values-type = "tuple"
3031

3132
[lint.per-file-ignores]
32-
# Ignore in the `tests/` directory.
33+
# Exceptions for test files
3334
"tests/**.py" = [
34-
"S101", # Use of `assert` detected
35-
"PLC2701", # We need import marked as internal files for testing
35+
"S101", # Allow use of `assert` in test files
36+
"PLC2701", # Allow importing internal files needed for testing
37+
"PLR6301", # Allow 'self' parameter in method definitions (required for test stubs)
38+
"ARG002", # Allow unused arguments in instance methods (required for test stubs)
3639
]
37-
38-
"src/pre_commit_terraform/terraform_docs_replace.py" = ["ALL"] # Deprecated hook
39-
"tests/pytest/terraform_docs_replace_test.py" = ["ALL"] # Tests for deprecated hook

src/pre_commit_terraform/_cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def invoke_cli_app(cli_args: list[str]) -> ReturnCodeType:
2828
root_cli_parser = initialize_argument_parser()
2929
parsed_cli_args = root_cli_parser.parse_args(cli_args)
3030
invoke_cli_app = cast_to(
31-
# FIXME: attempt typing per https://stackoverflow.com/a/75666611/595220 # noqa: TD001, TD002, TD003, FIX001, E501
31+
# FIXME: attempt typing per https://stackoverflow.com/a/75666611/595220 # noqa: TD001, TD002, TD003, FIX001, E501 All these suppressions caused by "FIXME" comment
3232
'CLIAppEntryPointCallableType',
3333
parsed_cli_args.invoke_cli_app,
3434
)

src/pre_commit_terraform/_errors.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,8 @@ class PreCommitTerraformRuntimeError(
1212
"""An exception representing a runtime error condition."""
1313

1414

15-
class PreCommitTerraformExit(PreCommitTerraformBaseError, SystemExit): # noqa: N818 FIXME
15+
# N818 - The name mimics the built-in SystemExit and is meant to have exactly
16+
# the same semantics. For this reason, it shouldn't have Error in the name to
17+
# maintain resemblance.
18+
class PreCommitTerraformExit(PreCommitTerraformBaseError, SystemExit): # noqa: N818
1619
"""An exception for terminating execution from deep app layers."""

src/pre_commit_terraform/terraform_docs_replace.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1+
"""Terraform Docs Replace Hook.
2+
3+
This hook is deprecated and will be removed in the future.
4+
Please, use 'terraform_docs' hook instead.
5+
"""
6+
17
import os
28
import subprocess
39
import warnings
410
from argparse import ArgumentParser, Namespace
11+
from pathlib import Path
512
from typing import cast as cast_to
613

714
from ._structs import ReturnCode
@@ -68,7 +75,7 @@ def invoke_cli_app(parsed_cli_args: Namespace) -> ReturnCodeType:
6875
if os.path.realpath(filename) not in dirs and (
6976
filename.endswith(('.tf', '.tfvars'))
7077
):
71-
dirs.append(os.path.dirname(filename))
78+
dirs.append(str(Path(filename).parent))
7279

7380
retval = ReturnCode.OK
7481

@@ -89,8 +96,13 @@ def invoke_cli_app(parsed_cli_args: Namespace) -> ReturnCodeType:
8996
),
9097
),
9198
)
92-
subprocess.check_call(' '.join(proc_args), shell=True)
93-
except subprocess.CalledProcessError as e:
94-
print(e)
99+
# S602 - 'shell=True' is insecure, but this hook is deprecated and
100+
# we don't want to spent time on testing fixes for it
101+
subprocess.check_call(' '.join(proc_args), shell=True) # noqa: S602
102+
# PERF203 - try-except shouldn't be in a loop, but it's deprecated
103+
# hook, so leave as is
104+
except subprocess.CalledProcessError as e: # noqa: PERF203
105+
# T201 - Leave print statement as is, as this is deprecated hook
106+
print(e) # noqa: T201
95107
retval = ReturnCode.ERROR
96108
return retval

tests/pytest/_cli_test.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ def test_known_interrupts(
5151
class CustomCmdStub:
5252
CLI_SUBCOMMAND_NAME = 'sentinel'
5353

54-
def populate_argument_parser( # noqa: PLR6301
54+
def populate_argument_parser(
5555
self,
56-
subcommand_parser: ArgumentParser, # noqa: ARG002
56+
subcommand_parser: ArgumentParser,
5757
) -> None:
5858
return None
5959

60-
def invoke_cli_app(self, parsed_cli_args: Namespace) -> ReturnCodeType: # noqa: PLR6301, ARG002
60+
def invoke_cli_app(self, parsed_cli_args: Namespace) -> ReturnCodeType:
6161
raise raised_error
6262

6363
monkeypatch.setattr(
@@ -81,16 +81,13 @@ def test_app_exit(
8181
class CustomCmdStub:
8282
CLI_SUBCOMMAND_NAME = 'sentinel'
8383

84-
def populate_argument_parser( # noqa: PLR6301
84+
def populate_argument_parser(
8585
self,
86-
subcommand_parser: ArgumentParser, # noqa: ARG002
86+
subcommand_parser: ArgumentParser,
8787
) -> None:
8888
return None
8989

90-
def invoke_cli_app(
91-
self,
92-
parsed_cli_args: Namespace, # noqa: ARG002
93-
) -> ReturnCodeType:
90+
def invoke_cli_app(self, parsed_cli_args: Namespace) -> ReturnCodeType:
9491
raise PreCommitTerraformExit(self.CLI_SUBCOMMAND_NAME)
9592

9693
monkeypatch.setattr(

tests/pytest/terraform_docs_replace_test.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,6 @@ def test_control_flow_negative(
120120
)
121121

122122
assert invoke_cli_app(parsed_cli_args) == ReturnCode.ERROR
123-
124-
check_call_mock.assert_called_once_with(expected_cmd, shell=True)
123+
# S604 - 'shell=True' is insecure, but this hook is deprecated and we don't
124+
# want to spent time on testing fixes for it
125+
check_call_mock.assert_called_once_with(expected_cmd, shell=True) # noqa: S604

0 commit comments

Comments
 (0)