Skip to content

Commit 092e4e0

Browse files
committed
add python-check-blanket-nosec
1 parent 9fa701e commit 092e4e0

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

.pre-commit-hooks.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
entry: '(?i)# noqa(?!: )'
55
language: pygrep
66
types: [python]
7+
- id: python-check-blanket-nosec
8+
name: check blanket nosec
9+
description: 'Enforce that `nosec` annotations always occur with specific codes. Sample annotations: `# nosec assert_used`, `# nosec B602, B607`'
10+
entry: '(?i)#\s*nosec:?\s*(?![^#])'
11+
language: pygrep
12+
types: [python]
713
- id: python-check-blanket-type-ignore
814
name: check blanket type ignore
915
description: 'Enforce that `# type: ignore` annotations always occur with specific codes. Sample annotations: `# type: ignore[attr-defined]`, `# type: ignore[attr-defined, name-defined]`'

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ For example, a hook which targets python will be called `python-...`.
2525

2626
[generated]: # (generated)
2727
- **`python-check-blanket-noqa`**: Enforce that `noqa` annotations always occur with specific codes. Sample annotations: `# noqa: F401`, `# noqa: F401,W203`
28+
- **`python-check-blanket-nosec`**: Enforce that `nosec` annotations always occur with specific codes. Sample annotations: `# nosec assert_used`, `# nosec B602, B607`
2829
- **`python-check-blanket-type-ignore`**: Enforce that `# type: ignore` annotations always occur with specific codes. Sample annotations: `# type: ignore[attr-defined]`, `# type: ignore[attr-defined, name-defined]`
2930
- **`python-check-mock-methods`**: Prevent common mistakes of `assert mck.not_called()`, `assert mck.called_once_with(...)` and `mck.assert_called`.
3031
- **`python-no-eval`**: A quick check for the `eval()` built-in function

tests/hooks_test.py

+28
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,34 @@ def test_python_use_type_annotations_negative(s):
3939
assert not HOOKS['python-use-type-annotations'].search(s)
4040

4141

42+
@pytest.mark.parametrize(
43+
's',
44+
(
45+
'# nosec',
46+
'# NOSEC',
47+
'# nosec: ',
48+
'# nosec ',
49+
),
50+
)
51+
def test_python_check_blanket_nosec_positive(s):
52+
assert HOOKS['python-check-blanket-nosec'].search(s)
53+
54+
55+
@pytest.mark.parametrize(
56+
's',
57+
(
58+
'x = 1',
59+
'# nosec:B401',
60+
'# nosec:B401',
61+
'# nosec:B401,B203',
62+
'# nosec: B401',
63+
'# nosec: B401, B203',
64+
),
65+
)
66+
def test_python_check_blanket_nosec_negative(s):
67+
assert not HOOKS['python-check-blanket-nosec'].search(s)
68+
69+
4270
@pytest.mark.parametrize(
4371
's',
4472
(

0 commit comments

Comments
 (0)