claude-dev-env 2.27.0 → 2.28.1
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/.agents/skills/hitl/SKILL.md +45 -0
- package/.agents/skills-archived/pr-loop-cloud-transport/reference/identity-and-hooks.md +1 -1
- package/audit-rubrics/category_rubrics/category-o-docstring-vs-impl-drift.md +16 -29
- package/audit-rubrics/prompts/category-o-docstring-vs-impl-drift.md +2 -2
- package/bin/ever-shipped-skills.mjs +1 -0
- package/docs/CODE_RULES.md +3 -3
- package/docs/agent-spawn-protocol.md +1 -1
- package/docs/references/prose-style-enforcement.md +5 -9
- package/hooks/blocking/AGENTS.md +3 -13
- package/hooks/blocking/code_rules_docstrings.py +10 -2138
- package/hooks/blocking/code_rules_enforcer.py +0 -121
- package/hooks/blocking/code_rules_imports_logging.py +1 -236
- package/hooks/blocking/code_rules_shared.py +23 -0
- package/hooks/blocking/code_rules_test_layout.py +8 -8
- package/hooks/blocking/config/prose_style_enforcement_constants.py +4 -2
- package/hooks/blocking/config/test_prose_style_enforcement_constants.py +5 -1
- package/hooks/blocking/precommit_code_rules_gate.py +8 -43
- package/hooks/blocking/state_description_blocker.py +1 -7
- package/hooks/blocking/test_code_rules_enforcer_cap_meta.py +0 -1
- package/hooks/blocking/test_code_rules_enforcer_dispatch_wiring.py +0 -8
- package/hooks/blocking/test_code_rules_enforcer_module_docstring_roster.py +11 -112
- package/hooks/blocking/test_code_rules_enforcer_narrow_edit.py +0 -1
- package/hooks/blocking/test_code_rules_enforcer_split_entry_1.py +1 -18
- package/hooks/blocking/test_code_rules_shared.py +12 -0
- package/hooks/blocking/test_precommit_code_rules_gate.py +32 -179
- package/hooks/blocking/test_precommit_code_rules_gate_native_owner.py +0 -1
- package/hooks/blocking/test_state_description_blocker.py +6 -4
- package/hooks/blocking/test_stop_dispatcher.py +5 -29
- package/hooks/hooks_constants/AGENTS.md +1 -6
- package/hooks/hooks_constants/bash_pre_tool_use_dispatcher_constants.py +0 -1
- package/hooks/hooks_constants/code_rules_enforcer_constants.py +3 -0
- package/hooks/hooks_constants/messages.py +0 -2
- package/hooks/hooks_constants/precommit_code_rules_gate_constants.py +3 -17
- package/hooks/hooks_constants/stop_dispatcher_constants.py +0 -2
- package/hooks/hooks_constants/test_bash_pre_tool_use_dispatcher_constants.py +0 -1
- package/hooks/hooks_constants/test_code_rules_enforcer_constants.py +7 -0
- package/hooks/hooks_constants/test_messages.py +5 -3
- package/hooks/hooks_constants/test_stop_dispatcher_constants.py +0 -2
- package/package.json +1 -1
- package/rules/AGENTS.md +3 -6
- package/rules/claims-as-quotes.md +0 -10
- package/rules/code-standards.md +6 -6
- package/rules/explore-thoroughly.md +0 -1
- package/rules/failure-blast-radius.md +0 -8
- package/rules/falsify-before-green.md +0 -8
- package/rules/file-global-constants.md +2 -2
- package/rules/filesystem-search.md +1 -1
- package/rules/git-workflow.md +1 -9
- package/rules/hedging-claims.md +2 -6
- package/rules/long-horizon-autonomy.md +1 -1
- package/rules/measurement-denominators.md +0 -9
- package/rules/research-mode.md +0 -6
- package/rules/verify-before-asking.md +0 -5
- package/rules/verify-runtime-state.md +0 -5
- package/scripts/codex_compat_materializer.py +99 -24
- package/scripts/sync_to_cursor/rules.py +0 -10
- package/scripts/tests/test_codex_compat_materializer.py +70 -19
- package/scripts/tests/test_engine.py +0 -1
- package/scripts/tests/test_rules.py +0 -1
- package/scripts/tests/test_sync_to_cursor.py +0 -1
- package/system-prompts/software-engineer.xml +3 -3
- package/hooks/blocking/code_rules_dead_argparse_argument.py +0 -554
- package/hooks/blocking/code_rules_dead_config_field.py +0 -568
- package/hooks/blocking/code_rules_dead_dataclass_field.py +0 -348
- package/hooks/blocking/code_rules_dead_module_constant.py +0 -757
- package/hooks/blocking/code_rules_dead_split_branch.py +0 -225
- package/hooks/blocking/code_rules_mock_completeness.py +0 -295
- package/hooks/blocking/code_rules_scope_binding.py +0 -151
- package/hooks/blocking/code_rules_unused_imports.py +0 -197
- package/hooks/blocking/hedging_language_blocker.py +0 -221
- package/hooks/blocking/intent_only_ending_blocker.py +0 -148
- package/hooks/blocking/test_code_rules_enforcer_dead_argparse_argument.py +0 -534
- package/hooks/blocking/test_code_rules_enforcer_dead_config_field.py +0 -846
- package/hooks/blocking/test_code_rules_enforcer_dead_dataclass_field.py +0 -507
- package/hooks/blocking/test_code_rules_enforcer_dead_module_constant.py +0 -679
- package/hooks/blocking/test_code_rules_enforcer_dead_module_constant_alias.py +0 -133
- package/hooks/blocking/test_code_rules_enforcer_dead_module_constant_read_cap.py +0 -103
- package/hooks/blocking/test_code_rules_enforcer_dead_split_branch.py +0 -105
- package/hooks/blocking/test_code_rules_enforcer_docstring_args_span_scope.py +0 -425
- package/hooks/blocking/test_code_rules_enforcer_docstring_cardinal_family.py +0 -176
- package/hooks/blocking/test_code_rules_enforcer_docstring_delegation_summary.py +0 -385
- package/hooks/blocking/test_code_rules_enforcer_docstring_fallback_branch.py +0 -398
- package/hooks/blocking/test_code_rules_enforcer_docstring_field_runmode_outcome.py +0 -129
- package/hooks/blocking/test_code_rules_enforcer_docstring_inline_literal_claim.py +0 -93
- package/hooks/blocking/test_code_rules_enforcer_docstring_length_constant_superlative.py +0 -198
- package/hooks/blocking/test_code_rules_enforcer_docstring_mark_glyph_enumeration.py +0 -262
- package/hooks/blocking/test_code_rules_enforcer_docstring_no_consumer.py +0 -93
- package/hooks/blocking/test_code_rules_enforcer_docstring_no_network.py +0 -115
- package/hooks/blocking/test_code_rules_enforcer_docstring_raises_largezipfile.py +0 -226
- package/hooks/blocking/test_code_rules_enforcer_docstring_returns_plural_cardinality.py +0 -207
- package/hooks/blocking/test_code_rules_enforcer_docstring_step_dispatch.py +0 -262
- package/hooks/blocking/test_code_rules_enforcer_docstring_type_checking_gate.py +0 -164
- package/hooks/blocking/test_code_rules_enforcer_docstring_unguarded_payload.py +0 -188
- package/hooks/blocking/test_code_rules_enforcer_import_block_sort.py +0 -157
- package/hooks/blocking/test_code_rules_enforcer_split_mocks_1.py +0 -303
- package/hooks/blocking/test_code_rules_enforcer_split_mocks_2.py +0 -111
- package/hooks/blocking/test_code_rules_enforcer_unused_imports.py +0 -656
- package/hooks/blocking/test_hedging_language_blocker.py +0 -261
- package/hooks/blocking/test_intent_only_ending_blocker.py +0 -209
- package/hooks/hooks_constants/dead_argparse_argument_constants.py +0 -28
- package/hooks/hooks_constants/dead_config_field_constants.py +0 -39
- package/hooks/hooks_constants/dead_dataclass_field_constants.py +0 -25
- package/hooks/hooks_constants/dead_module_constant_constants.py +0 -30
- package/hooks/hooks_constants/hedging_uncertainty_constants.py +0 -42
- package/hooks/hooks_constants/test_dispatcher_constants_docstrings.py +0 -44
- package/rules/conservative-action.md +0 -17
- package/rules/context7.md +0 -8
- package/rules/few-words.md +0 -3
- package/rules/parallel-tools.md +0 -23
|
@@ -1,554 +0,0 @@
|
|
|
1
|
-
"""Dead argparse-argument check: an optional CLI flag whose value is never read."""
|
|
2
|
-
|
|
3
|
-
import ast
|
|
4
|
-
import sys
|
|
5
|
-
from pathlib import Path
|
|
6
|
-
|
|
7
|
-
_blocking_directory = str(Path(__file__).resolve().parent)
|
|
8
|
-
_hooks_directory = str(Path(__file__).resolve().parent.parent)
|
|
9
|
-
if _blocking_directory not in sys.path:
|
|
10
|
-
sys.path.insert(0, _blocking_directory)
|
|
11
|
-
if _hooks_directory not in sys.path:
|
|
12
|
-
sys.path.insert(0, _hooks_directory)
|
|
13
|
-
|
|
14
|
-
from code_rules_shared import ( # noqa: E402
|
|
15
|
-
is_migration_file,
|
|
16
|
-
is_test_file,
|
|
17
|
-
)
|
|
18
|
-
|
|
19
|
-
from hooks_constants.dead_argparse_argument_constants import ( # noqa: E402
|
|
20
|
-
ACTION_KEYWORD_NAME,
|
|
21
|
-
ADD_ARGUMENT_METHOD_NAME,
|
|
22
|
-
ALL_PARSE_METHOD_NAMES,
|
|
23
|
-
ALL_SUPPRESSED_ACTION_NAMES,
|
|
24
|
-
ATTRGETTER_FUNCTION_NAME,
|
|
25
|
-
DEAD_ARGPARSE_ARGUMENT_GUIDANCE,
|
|
26
|
-
DEST_KEYWORD_NAME,
|
|
27
|
-
DEST_WORD_JOINER,
|
|
28
|
-
DEST_WORD_SEPARATOR,
|
|
29
|
-
EXPORTED_NAMES_ATTRIBUTE,
|
|
30
|
-
GETATTR_FUNCTION_NAME,
|
|
31
|
-
GETATTR_NAME_ARGUMENT_MINIMUM,
|
|
32
|
-
LONG_OPTION_PREFIX,
|
|
33
|
-
MAX_DEAD_ARGPARSE_ARGUMENT_ISSUES,
|
|
34
|
-
NAMESPACE_DICT_ATTRIBUTE_NAME,
|
|
35
|
-
NAMESPACE_KEYWORD_NAME,
|
|
36
|
-
OPTION_PREFIX,
|
|
37
|
-
)
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
def _string_constant_literal(node: ast.expr) -> str | None:
|
|
41
|
-
if isinstance(node, ast.Constant) and isinstance(node.value, str):
|
|
42
|
-
return node.value
|
|
43
|
-
return None
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
def _keyword_string_literal(call_node: ast.Call, keyword_name: str) -> str | None:
|
|
47
|
-
"""Return the string literal of a keyword argument, or None when absent."""
|
|
48
|
-
for each_keyword in call_node.keywords:
|
|
49
|
-
if each_keyword.arg == keyword_name:
|
|
50
|
-
return _string_constant_literal(each_keyword.value)
|
|
51
|
-
return None
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
def _keyword_value_node(call_node: ast.Call, keyword_name: str) -> ast.expr | None:
|
|
55
|
-
"""Return the value node of a keyword argument, or None when absent."""
|
|
56
|
-
for each_keyword in call_node.keywords:
|
|
57
|
-
if each_keyword.arg == keyword_name:
|
|
58
|
-
return each_keyword.value
|
|
59
|
-
return None
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
def _option_string_arguments(call_node: ast.Call) -> list[str]:
|
|
63
|
-
"""Return the leading positional string-literal arguments of an add_argument call."""
|
|
64
|
-
option_strings: list[str] = []
|
|
65
|
-
for each_argument in call_node.args:
|
|
66
|
-
literal_value = _string_constant_literal(each_argument)
|
|
67
|
-
if literal_value is None:
|
|
68
|
-
break
|
|
69
|
-
option_strings.append(literal_value)
|
|
70
|
-
return option_strings
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
def _dest_from_option_string(option_string: str) -> str:
|
|
74
|
-
return option_string.lstrip(OPTION_PREFIX).replace(DEST_WORD_SEPARATOR, DEST_WORD_JOINER)
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
def _has_keyword_argument(call_node: ast.Call, keyword_name: str) -> bool:
|
|
78
|
-
"""Return whether the call passes a keyword argument with the given name."""
|
|
79
|
-
return any(each_keyword.arg == keyword_name for each_keyword in call_node.keywords)
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
def _argument_dest_name(call_node: ast.Call) -> str | None:
|
|
83
|
-
"""Return the dest name an optional add_argument call declares, or None.
|
|
84
|
-
|
|
85
|
-
A ``dest=`` keyword determines the dest outright: a string-literal value
|
|
86
|
-
(``dest="name"``) names it, while a non-literal ``dest=`` (a variable or
|
|
87
|
-
expression whose value the static scan cannot resolve) makes the real dest
|
|
88
|
-
unknowable, so the argument is skipped (None). Without a ``dest=`` keyword the
|
|
89
|
-
dest derives from the first long option (``--repo`` -> ``repo``, ``--dry-run``
|
|
90
|
-
-> ``dry_run``) and falls back to the first short option when no long option
|
|
91
|
-
is present. A bare positional argument (no leading dash) is a required
|
|
92
|
-
positional, never an optional flag, and yields None so the caller skips it.
|
|
93
|
-
"""
|
|
94
|
-
if _has_keyword_argument(call_node, DEST_KEYWORD_NAME):
|
|
95
|
-
return _keyword_string_literal(call_node, DEST_KEYWORD_NAME)
|
|
96
|
-
option_strings = _option_string_arguments(call_node)
|
|
97
|
-
if not option_strings:
|
|
98
|
-
return None
|
|
99
|
-
if not option_strings[0].startswith(OPTION_PREFIX):
|
|
100
|
-
return None
|
|
101
|
-
for each_option in option_strings:
|
|
102
|
-
if each_option.startswith(LONG_OPTION_PREFIX):
|
|
103
|
-
return _dest_from_option_string(each_option)
|
|
104
|
-
return _dest_from_option_string(option_strings[0])
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
def _has_suppressed_action(call_node: ast.Call) -> bool:
|
|
108
|
-
action_name = _keyword_string_literal(call_node, ACTION_KEYWORD_NAME)
|
|
109
|
-
return action_name in ALL_SUPPRESSED_ACTION_NAMES
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
def _is_add_argument_call(node: ast.AST) -> bool:
|
|
113
|
-
return (
|
|
114
|
-
isinstance(node, ast.Call)
|
|
115
|
-
and isinstance(node.func, ast.Attribute)
|
|
116
|
-
and node.func.attr == ADD_ARGUMENT_METHOD_NAME
|
|
117
|
-
)
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
def _argument_dest_definitions(tree: ast.Module) -> list[tuple[str, int]]:
|
|
121
|
-
"""Return (dest_name, line) for each optional add_argument call in the module.
|
|
122
|
-
|
|
123
|
-
Positional arguments (a bare name with no leading dash) and the argparse
|
|
124
|
-
auto-actions ``help`` and ``version`` are excluded, since their parsed value
|
|
125
|
-
is never a meaningful readable dest.
|
|
126
|
-
"""
|
|
127
|
-
dest_definitions: list[tuple[str, int]] = []
|
|
128
|
-
for each_node in ast.walk(tree):
|
|
129
|
-
if not _is_add_argument_call(each_node):
|
|
130
|
-
continue
|
|
131
|
-
assert isinstance(each_node, ast.Call)
|
|
132
|
-
if _has_suppressed_action(each_node):
|
|
133
|
-
continue
|
|
134
|
-
dest_name = _argument_dest_name(each_node)
|
|
135
|
-
if dest_name is None:
|
|
136
|
-
continue
|
|
137
|
-
dest_definitions.append((dest_name, each_node.lineno))
|
|
138
|
-
return dest_definitions
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
def _attribute_read_names(tree: ast.Module) -> set[str]:
|
|
142
|
-
"""Return every attribute name read or augmented-assigned in the module.
|
|
143
|
-
|
|
144
|
-
A read in Load context (``parsed_args.repo``) names the attribute, and an
|
|
145
|
-
augmented assignment (``parsed_args.count += 1``) reads the attribute before
|
|
146
|
-
writing it, so both count as a read of the dest name.
|
|
147
|
-
"""
|
|
148
|
-
read_names: set[str] = set()
|
|
149
|
-
for each_node in ast.walk(tree):
|
|
150
|
-
if isinstance(each_node, ast.Attribute) and isinstance(each_node.ctx, ast.Load):
|
|
151
|
-
read_names.add(each_node.attr)
|
|
152
|
-
if isinstance(each_node, ast.AugAssign) and isinstance(each_node.target, ast.Attribute):
|
|
153
|
-
read_names.add(each_node.target.attr)
|
|
154
|
-
return read_names
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
def _dynamic_read_names(tree: ast.Module) -> set[str]:
|
|
158
|
-
"""Return literal attribute names read via ``getattr`` and ``attrgetter``.
|
|
159
|
-
|
|
160
|
-
``getattr(namespace, "repo")`` names its attribute in the second positional
|
|
161
|
-
argument, while ``attrgetter("a", "b")`` names one attribute per positional
|
|
162
|
-
argument; each literal string contributes a read name.
|
|
163
|
-
"""
|
|
164
|
-
literal_names: set[str] = set()
|
|
165
|
-
for each_node in ast.walk(tree):
|
|
166
|
-
if not isinstance(each_node, ast.Call):
|
|
167
|
-
continue
|
|
168
|
-
function_name = None
|
|
169
|
-
if isinstance(each_node.func, ast.Name):
|
|
170
|
-
function_name = each_node.func.id
|
|
171
|
-
elif isinstance(each_node.func, ast.Attribute):
|
|
172
|
-
function_name = each_node.func.attr
|
|
173
|
-
if function_name == GETATTR_FUNCTION_NAME:
|
|
174
|
-
if len(each_node.args) >= GETATTR_NAME_ARGUMENT_MINIMUM:
|
|
175
|
-
literal_name = _string_constant_literal(each_node.args[1])
|
|
176
|
-
if literal_name is not None:
|
|
177
|
-
literal_names.add(literal_name)
|
|
178
|
-
elif function_name == ATTRGETTER_FUNCTION_NAME:
|
|
179
|
-
for each_argument in each_node.args:
|
|
180
|
-
literal_name = _string_constant_literal(each_argument)
|
|
181
|
-
if literal_name is not None:
|
|
182
|
-
literal_names.add(literal_name)
|
|
183
|
-
return literal_names
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
def _exported_names(tree: ast.Module) -> set[str]:
|
|
187
|
-
"""Return names listed in a module-level ``__all__`` literal."""
|
|
188
|
-
exported: set[str] = set()
|
|
189
|
-
for each_node in tree.body:
|
|
190
|
-
if not isinstance(each_node, ast.Assign):
|
|
191
|
-
continue
|
|
192
|
-
targets_all = any(
|
|
193
|
-
isinstance(each_target, ast.Name) and each_target.id == EXPORTED_NAMES_ATTRIBUTE
|
|
194
|
-
for each_target in each_node.targets
|
|
195
|
-
)
|
|
196
|
-
if not targets_all:
|
|
197
|
-
continue
|
|
198
|
-
if isinstance(each_node.value, (ast.List, ast.Tuple, ast.Set)):
|
|
199
|
-
for each_element in each_node.value.elts:
|
|
200
|
-
literal_name = _string_constant_literal(each_element)
|
|
201
|
-
if literal_name is not None:
|
|
202
|
-
exported.add(literal_name)
|
|
203
|
-
return exported
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
def _target_namespace_names(assign_target: ast.expr) -> set[str]:
|
|
207
|
-
"""Return names an assignment target binds the namespace to.
|
|
208
|
-
|
|
209
|
-
A bare ``ast.Name`` target (``parsed = parse_args()``) binds one namespace
|
|
210
|
-
name. A tuple- or list-unpack target (``parsed, remaining =
|
|
211
|
-
parse_known_args()``) binds the namespace to its first ``ast.Name`` element,
|
|
212
|
-
matching the documented ``(namespace, remaining)`` return shape.
|
|
213
|
-
"""
|
|
214
|
-
if isinstance(assign_target, ast.Name):
|
|
215
|
-
return {assign_target.id}
|
|
216
|
-
if isinstance(assign_target, (ast.Tuple, ast.List)):
|
|
217
|
-
for each_element in assign_target.elts:
|
|
218
|
-
if isinstance(each_element, ast.Name):
|
|
219
|
-
return {each_element.id}
|
|
220
|
-
return set()
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
def _is_parse_method_call(node: ast.AST | None) -> bool:
|
|
224
|
-
"""Return whether a node is a ``parse_args``/``parse_known_args`` method call.
|
|
225
|
-
|
|
226
|
-
The call's function is an attribute access (``parser.parse_args``) whose
|
|
227
|
-
attribute name is one of the tracked parse methods.
|
|
228
|
-
"""
|
|
229
|
-
return (
|
|
230
|
-
isinstance(node, ast.Call)
|
|
231
|
-
and isinstance(node.func, ast.Attribute)
|
|
232
|
-
and node.func.attr in ALL_PARSE_METHOD_NAMES
|
|
233
|
-
)
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
def _parse_result_binding_targets(node: ast.AST) -> list[ast.expr] | None:
|
|
237
|
-
"""Return the targets a statement binds a parse-method result to, or None.
|
|
238
|
-
|
|
239
|
-
A plain ``ast.Assign`` (``parsed = parse_args()``) or an annotated
|
|
240
|
-
``ast.AnnAssign`` (``parsed: Namespace = parse_args()``) whose value is a
|
|
241
|
-
parse-method call binds the namespace; the returned list holds its target nodes,
|
|
242
|
-
one for an annotated assignment and one or more for a plain assignment. A node
|
|
243
|
-
that is not such a binding, including an annotated assignment with no value
|
|
244
|
-
(``parsed: Namespace``), returns None.
|
|
245
|
-
"""
|
|
246
|
-
if isinstance(node, ast.Assign):
|
|
247
|
-
binding_targets: list[ast.expr] = node.targets
|
|
248
|
-
bound_value: ast.expr | None = node.value
|
|
249
|
-
elif isinstance(node, ast.AnnAssign):
|
|
250
|
-
binding_targets = [node.target]
|
|
251
|
-
bound_value = node.value
|
|
252
|
-
else:
|
|
253
|
-
return None
|
|
254
|
-
if not _is_parse_method_call(bound_value):
|
|
255
|
-
return None
|
|
256
|
-
return binding_targets
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
def _parse_call_namespace_names(tree: ast.Module) -> set[str]:
|
|
260
|
-
"""Return names bound to a plain or annotated ``parse_args``/``parse_known_args`` result."""
|
|
261
|
-
parse_call_names: set[str] = set()
|
|
262
|
-
for each_node in ast.walk(tree):
|
|
263
|
-
binding_targets = _parse_result_binding_targets(each_node)
|
|
264
|
-
if binding_targets is None:
|
|
265
|
-
continue
|
|
266
|
-
for each_target in binding_targets:
|
|
267
|
-
parse_call_names |= _target_namespace_names(each_target)
|
|
268
|
-
return parse_call_names
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
def _alias_namespace_names(tree: ast.Module, all_parse_call_names: set[str]) -> set[str]:
|
|
272
|
-
"""Return names bound by re-assigning an existing namespace variable.
|
|
273
|
-
|
|
274
|
-
A simple ``alias = parsed_arguments`` re-binding aliases the namespace, and a
|
|
275
|
-
chain (``second = alias``) aliases it again, so the set grows to a fixed point
|
|
276
|
-
before it is returned.
|
|
277
|
-
"""
|
|
278
|
-
all_namespace_names = set(all_parse_call_names)
|
|
279
|
-
keeps_growing = True
|
|
280
|
-
while keeps_growing:
|
|
281
|
-
keeps_growing = False
|
|
282
|
-
for each_node in ast.walk(tree):
|
|
283
|
-
if not isinstance(each_node, ast.Assign):
|
|
284
|
-
continue
|
|
285
|
-
if not isinstance(each_node.value, ast.Name):
|
|
286
|
-
continue
|
|
287
|
-
if each_node.value.id not in all_namespace_names:
|
|
288
|
-
continue
|
|
289
|
-
for each_target in each_node.targets:
|
|
290
|
-
if isinstance(each_target, ast.Name) and each_target.id not in all_namespace_names:
|
|
291
|
-
all_namespace_names.add(each_target.id)
|
|
292
|
-
keeps_growing = True
|
|
293
|
-
return all_namespace_names
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
def _namespace_keyword_argument_names(tree: ast.Module) -> set[str]:
|
|
297
|
-
"""Return names passed as the ``namespace=`` keyword to a parse-method call.
|
|
298
|
-
|
|
299
|
-
``parser.parse_args(namespace=options)`` populates the pre-existing
|
|
300
|
-
``options`` object, so its attribute reads name dests even though the parse
|
|
301
|
-
result is never bound; the keyword's ``ast.Name`` value names that namespace.
|
|
302
|
-
"""
|
|
303
|
-
keyword_names: set[str] = set()
|
|
304
|
-
for each_node in ast.walk(tree):
|
|
305
|
-
if not isinstance(each_node, ast.Call):
|
|
306
|
-
continue
|
|
307
|
-
function_node = each_node.func
|
|
308
|
-
if not isinstance(function_node, ast.Attribute):
|
|
309
|
-
continue
|
|
310
|
-
if function_node.attr not in ALL_PARSE_METHOD_NAMES:
|
|
311
|
-
continue
|
|
312
|
-
keyword_value = _keyword_value_node(each_node, NAMESPACE_KEYWORD_NAME)
|
|
313
|
-
if isinstance(keyword_value, ast.Name):
|
|
314
|
-
keyword_names.add(keyword_value.id)
|
|
315
|
-
return keyword_names
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
def _namespace_variable_names(tree: ast.Module) -> set[str]:
|
|
319
|
-
"""Return names bound to a parse result, a ``namespace=`` object, or an alias.
|
|
320
|
-
|
|
321
|
-
A direct binding plain or annotated (``parsed = parse_args()`` or
|
|
322
|
-
``parsed: Namespace = parse_args()``), the first element of a tuple-unpack of the
|
|
323
|
-
documented ``(namespace, remaining)`` return (``parsed, remaining =
|
|
324
|
-
parse_known_args()``), a ``namespace=`` keyword object
|
|
325
|
-
(``parse_args(namespace=options)``), and a simple re-binding chain
|
|
326
|
-
(``alias = parsed``) each name the same namespace.
|
|
327
|
-
"""
|
|
328
|
-
seed_names = _parse_call_namespace_names(tree) | _namespace_keyword_argument_names(tree)
|
|
329
|
-
return _alias_namespace_names(tree, seed_names)
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
def _attribute_value_name_ids(tree: ast.Module) -> set[int]:
|
|
333
|
-
"""Return ``id()`` of every Name that is the object of an attribute access.
|
|
334
|
-
|
|
335
|
-
The Name in ``namespace.repo`` is the ``.value`` of an ``ast.Attribute``, so it
|
|
336
|
-
is a per-attribute read rather than a use of the namespace object itself.
|
|
337
|
-
"""
|
|
338
|
-
return {
|
|
339
|
-
id(each_node.value)
|
|
340
|
-
for each_node in ast.walk(tree)
|
|
341
|
-
if isinstance(each_node, ast.Attribute) and isinstance(each_node.value, ast.Name)
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
def _namespace_keyword_value_name_ids(tree: ast.Module) -> set[int]:
|
|
346
|
-
"""Return ``id()`` of every Name passed as the ``namespace=`` keyword to a parse call.
|
|
347
|
-
|
|
348
|
-
``parse_args(namespace=options)`` binds the namespace at this position rather
|
|
349
|
-
than consuming it, so this Name is excluded from the escape scan the way an
|
|
350
|
-
attribute-read object is.
|
|
351
|
-
"""
|
|
352
|
-
keyword_value_ids: set[int] = set()
|
|
353
|
-
for each_node in ast.walk(tree):
|
|
354
|
-
if not isinstance(each_node, ast.Call):
|
|
355
|
-
continue
|
|
356
|
-
function_node = each_node.func
|
|
357
|
-
if not isinstance(function_node, ast.Attribute):
|
|
358
|
-
continue
|
|
359
|
-
if function_node.attr not in ALL_PARSE_METHOD_NAMES:
|
|
360
|
-
continue
|
|
361
|
-
keyword_value = _keyword_value_node(each_node, NAMESPACE_KEYWORD_NAME)
|
|
362
|
-
if isinstance(keyword_value, ast.Name):
|
|
363
|
-
keyword_value_ids.add(id(keyword_value))
|
|
364
|
-
return keyword_value_ids
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
def _namespace_used_as_value(tree: ast.Module, all_namespace_names: set[str]) -> bool:
|
|
368
|
-
"""Return whether a tracked namespace Name is used as a value rather than an attribute read.
|
|
369
|
-
|
|
370
|
-
A tracked namespace Name read in Load context anywhere in the module -- passed
|
|
371
|
-
to a call, returned, aliased, double-star unpacked, or nested inside a container
|
|
372
|
-
literal -- uses the namespace object itself and hides which attributes are read.
|
|
373
|
-
Two positions are excluded: the object of an attribute access (``namespace.repo``
|
|
374
|
-
is a per-attribute read) and a Name passed as the ``namespace=`` keyword to a
|
|
375
|
-
parse call (a binding site, not a consumption).
|
|
376
|
-
"""
|
|
377
|
-
excluded_name_ids = _attribute_value_name_ids(tree) | _namespace_keyword_value_name_ids(tree)
|
|
378
|
-
for each_node in ast.walk(tree):
|
|
379
|
-
if not isinstance(each_node, ast.Name):
|
|
380
|
-
continue
|
|
381
|
-
if not isinstance(each_node.ctx, ast.Load):
|
|
382
|
-
continue
|
|
383
|
-
if each_node.id not in all_namespace_names:
|
|
384
|
-
continue
|
|
385
|
-
if id(each_node) in excluded_name_ids:
|
|
386
|
-
continue
|
|
387
|
-
return True
|
|
388
|
-
return False
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
def _namespace_dict_accessed(tree: ast.Module, all_namespace_names: set[str]) -> bool:
|
|
392
|
-
"""Return whether a tracked namespace exposes all attributes via ``__dict__``."""
|
|
393
|
-
for each_node in ast.walk(tree):
|
|
394
|
-
if (
|
|
395
|
-
isinstance(each_node, ast.Attribute)
|
|
396
|
-
and each_node.attr == NAMESPACE_DICT_ATTRIBUTE_NAME
|
|
397
|
-
and isinstance(each_node.value, ast.Name)
|
|
398
|
-
and each_node.value.id in all_namespace_names
|
|
399
|
-
):
|
|
400
|
-
return True
|
|
401
|
-
return False
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
def _namespace_escapes(tree: ast.Module, all_namespace_names: set[str]) -> bool:
|
|
405
|
-
"""Return whether a namespace is consumed in a way that hides which dests are read.
|
|
406
|
-
|
|
407
|
-
A namespace whose attributes the static scan cannot enumerate makes any single
|
|
408
|
-
dest unprovably dead, so the check suppresses. Each of the following is such an
|
|
409
|
-
escape: a ``parse_args``/``parse_known_args`` bound method referenced as an
|
|
410
|
-
aliased value rather than called inline; a parse result bound to an attribute-
|
|
411
|
-
or subscript-target the scan cannot track; a parse result consumed inline within
|
|
412
|
-
a larger expression rather than bound to an assignment or a bare statement; a
|
|
413
|
-
tracked namespace read through ``__dict__``; and a tracked namespace Name used as
|
|
414
|
-
a value rather than an attribute read (passed to a call, returned, aliased,
|
|
415
|
-
double-star unpacked, or nested inside a container literal), excluding the object
|
|
416
|
-
of an attribute access and a Name passed as the ``namespace=`` keyword to a parse
|
|
417
|
-
call.
|
|
418
|
-
"""
|
|
419
|
-
if _parse_method_is_aliased(tree):
|
|
420
|
-
return True
|
|
421
|
-
if _parse_result_binds_untracked_target(tree):
|
|
422
|
-
return True
|
|
423
|
-
if _parse_call_consumed_inline(tree):
|
|
424
|
-
return True
|
|
425
|
-
if _namespace_dict_accessed(tree, all_namespace_names):
|
|
426
|
-
return True
|
|
427
|
-
return _namespace_used_as_value(tree, all_namespace_names)
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
def _parse_method_is_aliased(tree: ast.Module) -> bool:
|
|
431
|
-
"""Return whether a ``parse_args``/``parse_known_args`` method is referenced uncalled.
|
|
432
|
-
|
|
433
|
-
The bound method is aliased when its attribute (``parser.parse_args``) appears
|
|
434
|
-
as a value rather than as the ``func`` of an enclosing call — assigned to a
|
|
435
|
-
name or passed around — so the resulting namespace is produced by a call the
|
|
436
|
-
scan cannot follow back to ``parse_args``.
|
|
437
|
-
"""
|
|
438
|
-
all_called_function_ids = {
|
|
439
|
-
id(each_node.func) for each_node in ast.walk(tree) if isinstance(each_node, ast.Call)
|
|
440
|
-
}
|
|
441
|
-
for each_node in ast.walk(tree):
|
|
442
|
-
if not isinstance(each_node, ast.Attribute):
|
|
443
|
-
continue
|
|
444
|
-
if each_node.attr not in ALL_PARSE_METHOD_NAMES:
|
|
445
|
-
continue
|
|
446
|
-
if id(each_node) not in all_called_function_ids:
|
|
447
|
-
return True
|
|
448
|
-
return False
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
def _parse_result_binds_untracked_target(tree: ast.Module) -> bool:
|
|
452
|
-
"""Return whether a parse-method result binds to a target the scan cannot track.
|
|
453
|
-
|
|
454
|
-
A ``parse_args``/``parse_known_args`` result bound by a plain or annotated
|
|
455
|
-
assignment to a plain ``ast.Name`` target, or to a tuple/list whose first
|
|
456
|
-
element is a plain ``ast.Name``, is tracked as a namespace variable. A result
|
|
457
|
-
bound to an attribute target (``self.args``) or a subscript target binds the
|
|
458
|
-
namespace where the scan cannot follow its attribute reads, so it escapes.
|
|
459
|
-
"""
|
|
460
|
-
for each_node in ast.walk(tree):
|
|
461
|
-
binding_targets = _parse_result_binding_targets(each_node)
|
|
462
|
-
if binding_targets is None:
|
|
463
|
-
continue
|
|
464
|
-
for each_target in binding_targets:
|
|
465
|
-
if not _target_namespace_names(each_target):
|
|
466
|
-
return True
|
|
467
|
-
return False
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
def _parse_call_consumed_inline(tree: ast.Module) -> bool:
|
|
471
|
-
"""Return whether a parse-method result is consumed inline rather than bound to a target.
|
|
472
|
-
|
|
473
|
-
A ``parse_args``/``parse_known_args`` call is statically trackable only when its
|
|
474
|
-
result is the value of a plain assignment (``parsed = parser.parse_args()``), an
|
|
475
|
-
annotated assignment (``parsed: Namespace = parser.parse_args()``), or a bare
|
|
476
|
-
expression statement (``parser.parse_args(namespace=options)``, whose result is
|
|
477
|
-
discarded after populating the keyword object). Consumed inside a larger
|
|
478
|
-
expression instead -- returned directly, passed to ``vars``, double-star
|
|
479
|
-
unpacked, or bound by a walrus -- the namespace never reaches a tracked name, so
|
|
480
|
-
the check suppresses.
|
|
481
|
-
"""
|
|
482
|
-
statement_bound_call_ids: set[int] = set()
|
|
483
|
-
for each_node in ast.walk(tree):
|
|
484
|
-
if not isinstance(each_node, (ast.Assign, ast.AnnAssign, ast.Expr)):
|
|
485
|
-
continue
|
|
486
|
-
if _is_parse_method_call(each_node.value):
|
|
487
|
-
statement_bound_call_ids.add(id(each_node.value))
|
|
488
|
-
for each_node in ast.walk(tree):
|
|
489
|
-
if _is_parse_method_call(each_node) and id(each_node) not in statement_bound_call_ids:
|
|
490
|
-
return True
|
|
491
|
-
return False
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
def check_dead_argparse_arguments(
|
|
495
|
-
content: str, file_path: str, full_file_content: str | None = None
|
|
496
|
-
) -> list[str]:
|
|
497
|
-
"""Flag an optional argparse flag whose parsed value the same file never reads.
|
|
498
|
-
|
|
499
|
-
An optional ``add_argument("--flag", ...)`` is dead when its dest name never
|
|
500
|
-
appears as an attribute read, an augmented-assignment target, or a literal
|
|
501
|
-
``getattr``/``attrgetter`` access anywhere in the file, the name is not listed
|
|
502
|
-
in a module-level ``__all__``, and no parsed namespace escapes static view. A
|
|
503
|
-
namespace escapes when a ``parse_args``/``parse_known_args`` bound method is
|
|
504
|
-
referenced as an aliased value rather than called inline, when a parse result
|
|
505
|
-
binds to an attribute- or subscript-target the scan cannot track, when a parse
|
|
506
|
-
result is consumed inline within a larger expression rather than bound to an
|
|
507
|
-
assignment or a bare statement, when a tracked namespace is read through
|
|
508
|
-
``__dict__``, or when a tracked namespace Name is used as a value rather than an
|
|
509
|
-
attribute read (passed to a call, returned, aliased, double-star unpacked, or
|
|
510
|
-
nested inside a container literal), excluding the object of an attribute access
|
|
511
|
-
and a Name passed as the ``namespace=`` keyword to a parse call. A namespace
|
|
512
|
-
name is tracked when it binds a parse result, a tuple-unpacked
|
|
513
|
-
parse result, a ``namespace=`` keyword object, or an alias of one of these.
|
|
514
|
-
Positional arguments and the argparse ``help``/``version`` auto-actions are never
|
|
515
|
-
flagged. Whole-file analysis runs against ``full_file_content`` when supplied so
|
|
516
|
-
an Edit fragment is judged against the reconstructed post-edit file.
|
|
517
|
-
|
|
518
|
-
Args:
|
|
519
|
-
content: The new content under validation (Edit fragment or whole file).
|
|
520
|
-
file_path: The destination path, used for the test/migration exemptions.
|
|
521
|
-
full_file_content: The reconstructed post-edit whole-file content for an
|
|
522
|
-
Edit, or None for a Write where ``content`` is already the whole file.
|
|
523
|
-
|
|
524
|
-
Returns:
|
|
525
|
-
One violation message per dead optional argument, capped at the configured
|
|
526
|
-
maximum.
|
|
527
|
-
"""
|
|
528
|
-
if is_test_file(file_path):
|
|
529
|
-
return []
|
|
530
|
-
if is_migration_file(file_path):
|
|
531
|
-
return []
|
|
532
|
-
effective_content = content if full_file_content is None else full_file_content
|
|
533
|
-
try:
|
|
534
|
-
tree = ast.parse(effective_content)
|
|
535
|
-
except SyntaxError:
|
|
536
|
-
return []
|
|
537
|
-
dest_definitions = _argument_dest_definitions(tree)
|
|
538
|
-
if not dest_definitions:
|
|
539
|
-
return []
|
|
540
|
-
all_namespace_names = _namespace_variable_names(tree)
|
|
541
|
-
if _namespace_escapes(tree, all_namespace_names):
|
|
542
|
-
return []
|
|
543
|
-
read_names = _attribute_read_names(tree) | _dynamic_read_names(tree) | _exported_names(tree)
|
|
544
|
-
issues: list[str] = []
|
|
545
|
-
for each_dest_definition in dest_definitions:
|
|
546
|
-
dest_name, dest_line = each_dest_definition
|
|
547
|
-
if dest_name in read_names:
|
|
548
|
-
continue
|
|
549
|
-
issues.append(
|
|
550
|
-
f"Line {dest_line}: CLI argument {dest_name!r} - {DEAD_ARGPARSE_ARGUMENT_GUIDANCE}"
|
|
551
|
-
)
|
|
552
|
-
if len(issues) >= MAX_DEAD_ARGPARSE_ARGUMENT_ISSUES:
|
|
553
|
-
return issues
|
|
554
|
-
return issues
|