claude-dev-env 1.82.0 → 1.83.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CLAUDE.md +16 -13
- package/_shared/pr-loop/scripts/CLAUDE.md +1 -0
- package/_shared/pr-loop/scripts/README.md +1 -0
- package/_shared/pr-loop/scripts/pr_loop_shared_constants/CLAUDE.md +1 -0
- package/_shared/pr-loop/scripts/pr_loop_shared_constants/reviewer_availability_constants.py +12 -0
- package/_shared/pr-loop/scripts/pr_loop_shared_constants/terminology_sweep_constants.py +0 -2
- package/_shared/pr-loop/scripts/reviewer_availability.py +182 -0
- package/_shared/pr-loop/scripts/terminology_sweep.py +9 -33
- package/_shared/pr-loop/scripts/tests/CLAUDE.md +2 -0
- package/_shared/pr-loop/scripts/tests/test_reviewer_availability.py +159 -0
- package/_shared/pr-loop/scripts/tests/test_reviewer_availability_constants.py +36 -0
- package/_shared/pr-loop/scripts/tests/test_terminology_sweep.py +14 -4
- package/hooks/blocking/CLAUDE.md +2 -0
- package/hooks/blocking/code_rules_constants_config.py +159 -1
- package/hooks/blocking/code_rules_docstrings.py +312 -9
- package/hooks/blocking/code_rules_enforcer.py +29 -0
- package/hooks/blocking/code_rules_imports_logging.py +867 -1
- package/hooks/blocking/code_rules_naming_collection.py +141 -0
- package/hooks/blocking/code_rules_string_magic.py +68 -0
- package/hooks/blocking/pre_tool_use_dispatcher.py +3 -3
- package/hooks/blocking/reviewer_spawn_gate.py +182 -0
- package/hooks/blocking/stale_comment_reference_blocker.py +267 -0
- package/hooks/blocking/state_description_blocker.py +96 -5
- package/hooks/blocking/test_code_rules_config_duplicate_path_anchor.py +132 -0
- package/hooks/blocking/test_code_rules_enforcer_cap_meta.py +2 -0
- package/hooks/blocking/test_code_rules_enforcer_docstring_delegation_summary.py +385 -0
- package/hooks/blocking/test_code_rules_enforcer_join_separator_magic.py +67 -0
- package/hooks/blocking/test_code_rules_enforcer_module_docstring_roster.py +40 -0
- package/hooks/blocking/test_code_rules_enforcer_naive_datetime.py +213 -0
- package/hooks/blocking/test_code_rules_enforcer_referenced_underscore_loop.py +169 -0
- package/hooks/blocking/test_code_rules_js_bare_flag_return_directive.py +266 -0
- package/hooks/blocking/test_code_rules_js_sibling_return_object_key_drift.py +490 -0
- package/hooks/blocking/test_code_rules_logging_adjacent_literals.py +171 -0
- package/hooks/blocking/test_pre_tool_use_dispatcher.py +9 -3
- package/hooks/blocking/test_reviewer_spawn_gate.py +230 -0
- package/hooks/blocking/test_stale_comment_reference_blocker.py +236 -0
- package/hooks/blocking/test_state_description_blocker.py +135 -0
- package/hooks/hooks.json +5 -0
- package/hooks/hooks_constants/CLAUDE.md +3 -1
- package/hooks/hooks_constants/blocking_check_limits.py +43 -0
- package/hooks/hooks_constants/code_rules_enforcer_constants.py +41 -0
- package/hooks/hooks_constants/pre_tool_use_dispatcher_constants.py +4 -0
- package/hooks/hooks_constants/reviewer_spawn_gate_constants.py +41 -0
- package/hooks/hooks_constants/stale_comment_reference_blocker_constants.py +76 -0
- package/hooks/hooks_constants/state_description_blocker_constants.py +8 -0
- package/package.json +1 -1
- package/rules/CLAUDE.md +4 -1
- package/rules/claude-md-orphan-file.md +5 -0
- package/rules/docstring-prose-matches-implementation.md +10 -1
- package/rules/env-var-table-code-drift.md +5 -0
- package/rules/es-exe-file-search.md +17 -0
- package/rules/no-historical-clutter.md +12 -1
- package/rules/orphan-css-class.md +5 -0
- package/rules/package-inventory-stale-entry.md +10 -0
- package/rules/paired-test-coverage.md +5 -0
- package/rules/plain-illustrative-docstrings.md +5 -0
- package/rules/verify-before-asking.md +7 -0
- package/rules/verify-runtime-state.md +40 -0
- package/rules/windows-filesystem-safe.md +8 -0
- package/rules/workers-done-before-complete.md +33 -0
- package/rules/workflow-substitution-slots.md +5 -0
- package/skills/CLAUDE.md +1 -1
- package/skills/autoconverge/SKILL.md +10 -4
- package/skills/autoconverge/workflow/converge.contract.test.mjs +69 -0
- package/skills/autoconverge/workflow/converge.copilot-gate.test.mjs +54 -18
- package/skills/autoconverge/workflow/converge.mjs +97 -33
- package/skills/everything-search/SKILL.md +5 -0
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
import importlib.util
|
|
5
|
+
|
|
6
|
+
ENFORCER_PATH = Path(__file__).resolve().parent / "code_rules_enforcer.py"
|
|
7
|
+
specification = importlib.util.spec_from_file_location("code_rules_enforcer", ENFORCER_PATH)
|
|
8
|
+
code_rules_enforcer = importlib.util.module_from_spec(specification)
|
|
9
|
+
specification.loader.exec_module(code_rules_enforcer)
|
|
10
|
+
|
|
11
|
+
PRODUCTION_FILE_PATH = "packages/app/services/foo.py"
|
|
12
|
+
TEST_FILE_PATH = "packages/app/tests/test_foo.py"
|
|
13
|
+
CONFIG_FILE_PATH = "packages/app/config/constants.py"
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def test_should_flag_literal_delimiter_join_separator_in_function_body() -> None:
|
|
17
|
+
source = (
|
|
18
|
+
"def render(all_paths: list) -> str:\n"
|
|
19
|
+
" return ', '.join(str(each_path) for each_path in all_paths)\n"
|
|
20
|
+
)
|
|
21
|
+
issues = code_rules_enforcer.check_join_separator_string_magic(source, PRODUCTION_FILE_PATH)
|
|
22
|
+
assert any("join" in each_issue for each_issue in issues), (
|
|
23
|
+
f"Expected literal join separator flagged, got: {issues}"
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def test_should_not_flag_named_constant_join_separator() -> None:
|
|
28
|
+
source = (
|
|
29
|
+
"from config.constants import JOIN_DELIMITER\n"
|
|
30
|
+
"\n"
|
|
31
|
+
"def render(all_paths: list) -> str:\n"
|
|
32
|
+
" return JOIN_DELIMITER.join(all_paths)\n"
|
|
33
|
+
)
|
|
34
|
+
issues = code_rules_enforcer.check_join_separator_string_magic(source, PRODUCTION_FILE_PATH)
|
|
35
|
+
assert issues == [], f"Named constant separator must pass, got: {issues}"
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def test_should_not_flag_empty_string_join_concatenation() -> None:
|
|
39
|
+
source = "def concatenate(all_parts: list) -> str:\n return ''.join(all_parts)\n"
|
|
40
|
+
issues = code_rules_enforcer.check_join_separator_string_magic(source, PRODUCTION_FILE_PATH)
|
|
41
|
+
assert issues == [], (
|
|
42
|
+
f"Empty-string join concatenation is idiomatic and must pass, got: {issues}"
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def test_should_skip_join_separator_in_test_files() -> None:
|
|
47
|
+
source = "def test_render() -> None:\n assert ', '.join(['a', 'b']) == 'a, b'\n"
|
|
48
|
+
issues = code_rules_enforcer.check_join_separator_string_magic(source, TEST_FILE_PATH)
|
|
49
|
+
assert issues == [], f"Test files exempt, got: {issues}"
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def test_should_skip_join_separator_in_config_files() -> None:
|
|
53
|
+
source = "def build_message(all_names: list) -> str:\n return ', '.join(all_names)\n"
|
|
54
|
+
issues = code_rules_enforcer.check_join_separator_string_magic(source, CONFIG_FILE_PATH)
|
|
55
|
+
assert issues == [], f"Config files exempt, got: {issues}"
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def test_should_report_join_separator_line_number() -> None:
|
|
59
|
+
source = (
|
|
60
|
+
"def render(all_paths: list) -> str:\n"
|
|
61
|
+
" joined = '; '.join(all_paths)\n"
|
|
62
|
+
" return joined\n"
|
|
63
|
+
)
|
|
64
|
+
issues = code_rules_enforcer.check_join_separator_string_magic(source, PRODUCTION_FILE_PATH)
|
|
65
|
+
assert any("Line 2" in each_issue for each_issue in issues), (
|
|
66
|
+
f"Expected line 2 reported, got: {issues}"
|
|
67
|
+
)
|
|
@@ -93,6 +93,46 @@ def test_should_not_flag_module_docstring_naming_every_public_check() -> None:
|
|
|
93
93
|
assert issues == [], f"Docstring naming every check must not flag, got: {issues!r}"
|
|
94
94
|
|
|
95
95
|
|
|
96
|
+
def _registry_module_omitting_check_via_shared_tokens_only() -> str:
|
|
97
|
+
return (
|
|
98
|
+
'"""Bare string-literal magic, inline literal-collection, inline tuple '
|
|
99
|
+
'string-magic, and whitespace-indentation magic checks."""\n'
|
|
100
|
+
"\n"
|
|
101
|
+
"def check_string_literal_magic(content: str, file_path: str) -> list[str]:\n"
|
|
102
|
+
" return []\n"
|
|
103
|
+
"\n"
|
|
104
|
+
"def check_join_separator_string_magic(content: str, file_path: str) -> list[str]:\n"
|
|
105
|
+
" return []\n"
|
|
106
|
+
"\n"
|
|
107
|
+
"def check_inline_literal_collections(content: str, file_path: str) -> list[str]:\n"
|
|
108
|
+
" return []\n"
|
|
109
|
+
"\n"
|
|
110
|
+
"def check_inline_tuple_string_magic(content: str, file_path: str) -> list[str]:\n"
|
|
111
|
+
" return []\n"
|
|
112
|
+
"\n"
|
|
113
|
+
"def check_whitespace_indentation_magic(content: str, file_path: str) -> list[str]:\n"
|
|
114
|
+
" return []\n"
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def test_should_flag_check_named_only_by_tokens_shared_with_siblings() -> None:
|
|
119
|
+
issues = check_module_docstring_names_public_checks(
|
|
120
|
+
_registry_module_omitting_check_via_shared_tokens_only(), HOOK_INFRASTRUCTURE_PATH
|
|
121
|
+
)
|
|
122
|
+
assert any("check_join_separator_string_magic" in each for each in issues), (
|
|
123
|
+
"A check whose only summary-present tokens ('string', 'magic') are shared with "
|
|
124
|
+
f"sibling checks must flag as omitted, got: {issues!r}"
|
|
125
|
+
)
|
|
126
|
+
assert not any("check_string_literal_magic" in each for each in issues), (
|
|
127
|
+
f"A check the summary names ('string-literal magic') must not flag, got: {issues!r}"
|
|
128
|
+
)
|
|
129
|
+
assert not any("check_inline_literal_collections" in each for each in issues), (
|
|
130
|
+
"A check named by a stemmed token ('collection' for 'collections') must not "
|
|
131
|
+
f"flag, got: {issues!r}"
|
|
132
|
+
)
|
|
133
|
+
assert len(issues) == 1
|
|
134
|
+
|
|
135
|
+
|
|
96
136
|
def test_should_not_flag_module_with_a_single_public_check() -> None:
|
|
97
137
|
source = (
|
|
98
138
|
'"""Skip-decorator test-quality check."""\n'
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
"""Tests for the naive-datetime-construction check in code_rules_imports_logging.py.
|
|
2
|
+
|
|
3
|
+
The check catches the DST-fold-ambiguity class Copilot flagged on
|
|
4
|
+
python-automation PR #557: ``datetime.fromtimestamp(epoch)`` builds a datetime
|
|
5
|
+
with no timezone, and a later ``.astimezone()`` on that naive value has to guess
|
|
6
|
+
the local offset. The check flags the naive-producing constructors
|
|
7
|
+
(``fromtimestamp``/``utcfromtimestamp`` without ``tz=``, and ``utcnow``) and
|
|
8
|
+
leaves a ``tz=``-carrying call, and a plain ``now()``, alone.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import importlib.util
|
|
14
|
+
from pathlib import Path
|
|
15
|
+
from types import ModuleType
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _load_enforcer_module() -> ModuleType:
|
|
19
|
+
module_path = Path(__file__).parent / "code_rules_enforcer.py"
|
|
20
|
+
spec = importlib.util.spec_from_file_location("code_rules_enforcer", module_path)
|
|
21
|
+
assert spec is not None
|
|
22
|
+
assert spec.loader is not None
|
|
23
|
+
module = importlib.util.module_from_spec(spec)
|
|
24
|
+
spec.loader.exec_module(module)
|
|
25
|
+
return module
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
code_rules_enforcer = _load_enforcer_module()
|
|
29
|
+
|
|
30
|
+
PRODUCTION_FILE_PATH = "/project/src/export_verification.py"
|
|
31
|
+
TEST_FILE_PATH = "/project/src/test_export_verification.py"
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def check_naive_datetime_construction(content: str, file_path: str) -> list[str]:
|
|
35
|
+
return code_rules_enforcer.check_naive_datetime_construction(content, file_path)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def test_flags_fromtimestamp_without_timezone() -> None:
|
|
39
|
+
content = (
|
|
40
|
+
"from datetime import datetime\n"
|
|
41
|
+
"def describe(stamp: float) -> str:\n"
|
|
42
|
+
" return datetime.fromtimestamp(stamp).astimezone().isoformat()\n"
|
|
43
|
+
)
|
|
44
|
+
issues = check_naive_datetime_construction(content, PRODUCTION_FILE_PATH)
|
|
45
|
+
assert len(issues) == 1
|
|
46
|
+
assert "fromtimestamp" in issues[0]
|
|
47
|
+
assert "tz=" in issues[0]
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def test_flags_utcfromtimestamp_and_utcnow() -> None:
|
|
51
|
+
content = (
|
|
52
|
+
"from datetime import datetime\n"
|
|
53
|
+
"def build(stamp: float) -> tuple[object, object]:\n"
|
|
54
|
+
" return datetime.utcfromtimestamp(stamp), datetime.utcnow()\n"
|
|
55
|
+
)
|
|
56
|
+
issues = check_naive_datetime_construction(content, PRODUCTION_FILE_PATH)
|
|
57
|
+
assert len(issues) == 2
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def test_flags_module_qualified_datetime_call() -> None:
|
|
61
|
+
content = (
|
|
62
|
+
"import datetime\n"
|
|
63
|
+
"def describe(stamp: float) -> object:\n"
|
|
64
|
+
" return datetime.datetime.fromtimestamp(stamp)\n"
|
|
65
|
+
)
|
|
66
|
+
issues = check_naive_datetime_construction(content, PRODUCTION_FILE_PATH)
|
|
67
|
+
assert len(issues) == 1
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def test_allows_fromtimestamp_with_timezone_keyword() -> None:
|
|
71
|
+
content = (
|
|
72
|
+
"from datetime import datetime, timezone\n"
|
|
73
|
+
"def describe(stamp: float) -> str:\n"
|
|
74
|
+
" return datetime.fromtimestamp(stamp, tz=timezone.utc).isoformat()\n"
|
|
75
|
+
)
|
|
76
|
+
issues = check_naive_datetime_construction(content, PRODUCTION_FILE_PATH)
|
|
77
|
+
assert issues == []
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def test_allows_fromtimestamp_with_positional_timezone() -> None:
|
|
81
|
+
content = (
|
|
82
|
+
"from datetime import datetime, timezone\n"
|
|
83
|
+
"def describe(stamp: float) -> str:\n"
|
|
84
|
+
" return datetime.fromtimestamp(stamp, timezone.utc).isoformat()\n"
|
|
85
|
+
)
|
|
86
|
+
issues = check_naive_datetime_construction(content, PRODUCTION_FILE_PATH)
|
|
87
|
+
assert issues == []
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def test_allows_module_qualified_fromtimestamp_with_positional_timezone() -> None:
|
|
91
|
+
content = (
|
|
92
|
+
"import datetime\n"
|
|
93
|
+
"def describe(stamp: float) -> object:\n"
|
|
94
|
+
" return datetime.datetime.fromtimestamp(stamp, datetime.timezone.utc)\n"
|
|
95
|
+
)
|
|
96
|
+
issues = check_naive_datetime_construction(content, PRODUCTION_FILE_PATH)
|
|
97
|
+
assert issues == []
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def test_flags_utcfromtimestamp_with_second_positional_argument() -> None:
|
|
101
|
+
content = (
|
|
102
|
+
"from datetime import datetime\n"
|
|
103
|
+
"def describe(stamp: float, other: float) -> object:\n"
|
|
104
|
+
" return datetime.utcfromtimestamp(stamp, other)\n"
|
|
105
|
+
)
|
|
106
|
+
issues = check_naive_datetime_construction(content, PRODUCTION_FILE_PATH)
|
|
107
|
+
assert len(issues) == 1
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def test_ignores_unrelated_datetime_attribute_chain() -> None:
|
|
111
|
+
content = (
|
|
112
|
+
"def describe(foo: object, stamp: float) -> object:\n"
|
|
113
|
+
" return foo.datetime.fromtimestamp(stamp)\n"
|
|
114
|
+
)
|
|
115
|
+
issues = check_naive_datetime_construction(content, PRODUCTION_FILE_PATH)
|
|
116
|
+
assert issues == []
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def test_flags_fromtimestamp_with_none_timezone_keyword() -> None:
|
|
120
|
+
content = (
|
|
121
|
+
"from datetime import datetime\n"
|
|
122
|
+
"def describe(stamp: float) -> object:\n"
|
|
123
|
+
" return datetime.fromtimestamp(stamp, tz=None)\n"
|
|
124
|
+
)
|
|
125
|
+
issues = check_naive_datetime_construction(content, PRODUCTION_FILE_PATH)
|
|
126
|
+
assert len(issues) == 1
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def test_flags_fromtimestamp_with_none_positional_timezone() -> None:
|
|
130
|
+
content = (
|
|
131
|
+
"from datetime import datetime\n"
|
|
132
|
+
"def describe(stamp: float) -> object:\n"
|
|
133
|
+
" return datetime.fromtimestamp(stamp, None)\n"
|
|
134
|
+
)
|
|
135
|
+
issues = check_naive_datetime_construction(content, PRODUCTION_FILE_PATH)
|
|
136
|
+
assert len(issues) == 1
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def test_flags_utcfromtimestamp_even_with_timezone_keyword() -> None:
|
|
140
|
+
content = (
|
|
141
|
+
"from datetime import datetime, timezone\n"
|
|
142
|
+
"def describe(stamp: float) -> object:\n"
|
|
143
|
+
" return datetime.utcfromtimestamp(stamp, tz=timezone.utc)\n"
|
|
144
|
+
)
|
|
145
|
+
issues = check_naive_datetime_construction(content, PRODUCTION_FILE_PATH)
|
|
146
|
+
assert len(issues) == 1
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def test_utcnow_remediation_points_at_now_not_utcnow_tz() -> None:
|
|
150
|
+
content = (
|
|
151
|
+
"from datetime import datetime\n"
|
|
152
|
+
"def stamp() -> object:\n"
|
|
153
|
+
" return datetime.utcnow()\n"
|
|
154
|
+
)
|
|
155
|
+
issues = check_naive_datetime_construction(content, PRODUCTION_FILE_PATH)
|
|
156
|
+
assert len(issues) == 1
|
|
157
|
+
assert "now(tz=" in issues[0]
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def test_utcfromtimestamp_remediation_points_at_fromtimestamp() -> None:
|
|
161
|
+
content = (
|
|
162
|
+
"from datetime import datetime\n"
|
|
163
|
+
"def build(stamp: float) -> object:\n"
|
|
164
|
+
" return datetime.utcfromtimestamp(stamp)\n"
|
|
165
|
+
)
|
|
166
|
+
issues = check_naive_datetime_construction(content, PRODUCTION_FILE_PATH)
|
|
167
|
+
assert len(issues) == 1
|
|
168
|
+
assert "fromtimestamp(" in issues[0]
|
|
169
|
+
assert "tz=" in issues[0]
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def test_utcfromtimestamp_remediation_uses_generic_placeholder() -> None:
|
|
173
|
+
content = (
|
|
174
|
+
"from datetime import datetime\n"
|
|
175
|
+
"def build(epoch_seconds: float) -> object:\n"
|
|
176
|
+
" return datetime.utcfromtimestamp(epoch_seconds)\n"
|
|
177
|
+
)
|
|
178
|
+
issues = check_naive_datetime_construction(content, PRODUCTION_FILE_PATH)
|
|
179
|
+
assert len(issues) == 1
|
|
180
|
+
assert "..., tz=" in issues[0]
|
|
181
|
+
assert "stamp, tz=" not in issues[0]
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
def test_remediation_does_not_mandate_astimezone() -> None:
|
|
185
|
+
content = (
|
|
186
|
+
"from datetime import datetime\n"
|
|
187
|
+
"def stamp() -> object:\n"
|
|
188
|
+
" return datetime.utcnow()\n"
|
|
189
|
+
)
|
|
190
|
+
issues = check_naive_datetime_construction(content, PRODUCTION_FILE_PATH)
|
|
191
|
+
assert len(issues) == 1
|
|
192
|
+
assert "then .astimezone()" not in issues[0]
|
|
193
|
+
assert "only when local display" in issues[0]
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def test_allows_plain_now() -> None:
|
|
197
|
+
content = (
|
|
198
|
+
"from datetime import datetime\n"
|
|
199
|
+
"def stamp() -> str:\n"
|
|
200
|
+
" return datetime.now().isoformat()\n"
|
|
201
|
+
)
|
|
202
|
+
issues = check_naive_datetime_construction(content, PRODUCTION_FILE_PATH)
|
|
203
|
+
assert issues == []
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def test_exempts_test_files() -> None:
|
|
207
|
+
content = (
|
|
208
|
+
"from datetime import datetime\n"
|
|
209
|
+
"def describe(stamp: float) -> object:\n"
|
|
210
|
+
" return datetime.fromtimestamp(stamp)\n"
|
|
211
|
+
)
|
|
212
|
+
issues = check_naive_datetime_construction(content, TEST_FILE_PATH)
|
|
213
|
+
assert issues == []
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
import importlib.util
|
|
5
|
+
|
|
6
|
+
ENFORCER_PATH = Path(__file__).resolve().parent / "code_rules_enforcer.py"
|
|
7
|
+
specification = importlib.util.spec_from_file_location("code_rules_enforcer", ENFORCER_PATH)
|
|
8
|
+
code_rules_enforcer = importlib.util.module_from_spec(specification)
|
|
9
|
+
specification.loader.exec_module(code_rules_enforcer)
|
|
10
|
+
|
|
11
|
+
PRODUCTION_FILE_PATH = "packages/app/services/foo.py"
|
|
12
|
+
CONFTEST_FILE_PATH = "packages/app/tests/conftest.py"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def test_should_flag_referenced_underscore_loop_variable_in_conftest() -> None:
|
|
16
|
+
source = (
|
|
17
|
+
"import sys\n"
|
|
18
|
+
"\n"
|
|
19
|
+
"for _foreign_module_name in ['a', 'b']:\n"
|
|
20
|
+
" del sys.modules[_foreign_module_name]\n"
|
|
21
|
+
)
|
|
22
|
+
issues = code_rules_enforcer.check_referenced_underscore_loop_variable(
|
|
23
|
+
source, CONFTEST_FILE_PATH
|
|
24
|
+
)
|
|
25
|
+
assert any("_foreign_module_name" in each_issue for each_issue in issues), (
|
|
26
|
+
f"Expected referenced underscore loop variable flagged, got: {issues}"
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def test_should_flag_referenced_underscore_loop_variable_in_production() -> None:
|
|
31
|
+
source = (
|
|
32
|
+
"import sys\n"
|
|
33
|
+
"\n"
|
|
34
|
+
"def purge(all_names: list) -> None:\n"
|
|
35
|
+
" for _name in all_names:\n"
|
|
36
|
+
" del sys.modules[_name]\n"
|
|
37
|
+
)
|
|
38
|
+
issues = code_rules_enforcer.check_referenced_underscore_loop_variable(
|
|
39
|
+
source, PRODUCTION_FILE_PATH
|
|
40
|
+
)
|
|
41
|
+
assert any("_name" in each_issue for each_issue in issues), (
|
|
42
|
+
f"Expected referenced underscore loop variable flagged, got: {issues}"
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def test_should_not_flag_unreferenced_underscore_throwaway() -> None:
|
|
47
|
+
source = "def spin(count: int) -> None:\n for _unused in range(count):\n pass\n"
|
|
48
|
+
issues = code_rules_enforcer.check_referenced_underscore_loop_variable(
|
|
49
|
+
source, PRODUCTION_FILE_PATH
|
|
50
|
+
)
|
|
51
|
+
assert issues == [], f"Genuinely unused underscore throwaway must pass, got: {issues}"
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def test_should_not_flag_bare_underscore_target() -> None:
|
|
55
|
+
source = "def spin(count: int) -> None:\n for _ in range(count):\n return None\n"
|
|
56
|
+
issues = code_rules_enforcer.check_referenced_underscore_loop_variable(
|
|
57
|
+
source, PRODUCTION_FILE_PATH
|
|
58
|
+
)
|
|
59
|
+
assert issues == [], f"Bare '_' target must pass, got: {issues}"
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def test_should_not_flag_named_loop_variable_without_underscore() -> None:
|
|
63
|
+
source = (
|
|
64
|
+
"import sys\n\nfor each_module_name in ['a', 'b']:\n del sys.modules[each_module_name]\n"
|
|
65
|
+
)
|
|
66
|
+
issues = code_rules_enforcer.check_referenced_underscore_loop_variable(
|
|
67
|
+
source, CONFTEST_FILE_PATH
|
|
68
|
+
)
|
|
69
|
+
assert issues == [], f"each_-prefixed loop variable without underscore must pass, got: {issues}"
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def test_should_not_flag_underscore_loop_variable_shadowed_by_comprehension() -> None:
|
|
73
|
+
source = (
|
|
74
|
+
"def collect(all_names: list, other: list) -> list:\n"
|
|
75
|
+
" all_other: list = []\n"
|
|
76
|
+
" for _name in all_names:\n"
|
|
77
|
+
" all_other = [_name for _name in other]\n"
|
|
78
|
+
" return all_other\n"
|
|
79
|
+
)
|
|
80
|
+
issues = code_rules_enforcer.check_referenced_underscore_loop_variable(
|
|
81
|
+
source, PRODUCTION_FILE_PATH
|
|
82
|
+
)
|
|
83
|
+
assert issues == [], (
|
|
84
|
+
f"Comprehension rebinding the loop name shadows the outer read, must pass, got: {issues}"
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def test_should_flag_underscore_loop_variable_read_inside_non_rebinding_comprehension() -> None:
|
|
89
|
+
source = (
|
|
90
|
+
"def collect(all_names: list, other: list) -> list:\n"
|
|
91
|
+
" matches: list = []\n"
|
|
92
|
+
" for _name in all_names:\n"
|
|
93
|
+
" matches = [each_row for each_row in other if each_row == _name]\n"
|
|
94
|
+
" return matches\n"
|
|
95
|
+
)
|
|
96
|
+
issues = code_rules_enforcer.check_referenced_underscore_loop_variable(
|
|
97
|
+
source, PRODUCTION_FILE_PATH
|
|
98
|
+
)
|
|
99
|
+
assert any("_name" in each_issue for each_issue in issues), (
|
|
100
|
+
f"A comprehension reading the outer loop name without rebinding it must flag, got: {issues}"
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def test_should_not_flag_underscore_loop_variable_shadowed_by_lambda_parameter() -> None:
|
|
105
|
+
source = (
|
|
106
|
+
"def build(all_names: list) -> list:\n"
|
|
107
|
+
" all_callables: list = []\n"
|
|
108
|
+
" for _name in all_names:\n"
|
|
109
|
+
" all_callables.append(lambda _name: _name)\n"
|
|
110
|
+
" return all_callables\n"
|
|
111
|
+
)
|
|
112
|
+
issues = code_rules_enforcer.check_referenced_underscore_loop_variable(
|
|
113
|
+
source, PRODUCTION_FILE_PATH
|
|
114
|
+
)
|
|
115
|
+
assert issues == [], (
|
|
116
|
+
f"A lambda parameter shadowing the loop name owns its body, must pass, got: {issues}"
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def test_should_not_flag_underscore_loop_variable_shadowed_by_nested_def_parameter() -> None:
|
|
121
|
+
source = (
|
|
122
|
+
"def build(all_names: list) -> None:\n"
|
|
123
|
+
" for _handler in all_names:\n"
|
|
124
|
+
" def inner(_handler: object) -> object:\n"
|
|
125
|
+
" return _handler\n"
|
|
126
|
+
)
|
|
127
|
+
issues = code_rules_enforcer.check_referenced_underscore_loop_variable(
|
|
128
|
+
source, PRODUCTION_FILE_PATH
|
|
129
|
+
)
|
|
130
|
+
assert issues == [], (
|
|
131
|
+
f"A nested-def parameter shadowing the loop name owns its body, must pass, got: {issues}"
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def test_should_not_flag_outer_underscore_loop_variable_shadowed_by_nested_loop() -> None:
|
|
136
|
+
source = (
|
|
137
|
+
"import sys\n"
|
|
138
|
+
"\n"
|
|
139
|
+
"def purge(first: list, second: list) -> None:\n"
|
|
140
|
+
" for _name in first:\n"
|
|
141
|
+
" for _name in second:\n"
|
|
142
|
+
" del sys.modules[_name]\n"
|
|
143
|
+
)
|
|
144
|
+
issues = code_rules_enforcer.check_referenced_underscore_loop_variable(
|
|
145
|
+
source, PRODUCTION_FILE_PATH
|
|
146
|
+
)
|
|
147
|
+
assert not any("Line 4" in each_issue for each_issue in issues), (
|
|
148
|
+
f"Outer loop whose name a nested loop rebinds is never read in the outer scope, "
|
|
149
|
+
f"got: {issues}"
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def test_should_flag_underscore_loop_variable_read_before_nested_shadowing_loop() -> None:
|
|
154
|
+
source = (
|
|
155
|
+
"import sys\n"
|
|
156
|
+
"\n"
|
|
157
|
+
"def purge(first: list, second: list) -> None:\n"
|
|
158
|
+
" for _name in first:\n"
|
|
159
|
+
" del sys.modules[_name]\n"
|
|
160
|
+
" for _name in second:\n"
|
|
161
|
+
" pass\n"
|
|
162
|
+
)
|
|
163
|
+
issues = code_rules_enforcer.check_referenced_underscore_loop_variable(
|
|
164
|
+
source, PRODUCTION_FILE_PATH
|
|
165
|
+
)
|
|
166
|
+
assert any("Line 4" in each_issue for each_issue in issues), (
|
|
167
|
+
f"The outer loop body reads its own name before the nested loop rebinds it, "
|
|
168
|
+
f"must flag, got: {issues}"
|
|
169
|
+
)
|