claude-dev-env 1.69.0 → 1.69.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.
|
@@ -20,10 +20,23 @@ The scan is deliberately conservative to keep false positives near zero:
|
|
|
20
20
|
- Field reads are collected as ``ast.Attribute.attr`` values (``obj.field``),
|
|
21
21
|
augmented-assignment targets (``cfg.field += 1`` reads ``field`` before
|
|
22
22
|
writing it), string literals (covers ``getattr(obj, "field")``),
|
|
23
|
-
keyword-argument names (covers
|
|
23
|
+
keyword-argument names on non-constructor calls (covers
|
|
24
24
|
``replace(cfg, debug_port=1)``), and match-pattern keyword attribute names
|
|
25
|
-
(``case Config(field=found)``).
|
|
26
|
-
|
|
25
|
+
(``case Config(field=found)``). Two field-write forms are excluded because they
|
|
26
|
+
name a field without consuming it: a keyword that writes a field in a constructor
|
|
27
|
+
of a known ``*Config`` dataclass defined under the scan root
|
|
28
|
+
(``ThemeUpdateConfig(debug_port=1)``, excluded per keyword node so a same-named
|
|
29
|
+
keyword on a ``replace`` call stays a read, and a factory function whose name
|
|
30
|
+
merely ends in ``"Config"`` is not excluded), and a self-referential attribute
|
|
31
|
+
read inside a ``*Config`` field's own default-value expression in the class body
|
|
32
|
+
whose attribute name equals the field being defined
|
|
33
|
+
(``debug_port: int = source.debug_port``) — a field written only these ways and
|
|
34
|
+
read by no module is the dead-config case this check exists to catch. A default
|
|
35
|
+
that sources a differently-named field on another object
|
|
36
|
+
(``timeout_ms: int = other_config.base_timeout``) leaves that read counted, so
|
|
37
|
+
``base_timeout`` stays a live consumer. Plain
|
|
38
|
+
``ast.Name`` references are excluded — a local variable named ``debug_port`` is
|
|
39
|
+
not a read of ``config.debug_port``.
|
|
27
40
|
- A production module that reflectively reads a whole instance — a bare or
|
|
28
41
|
``dataclasses``-qualified call to ``asdict``, ``astuple``, ``fields``,
|
|
29
42
|
``replace``, or ``vars``, or a read of ``obj.__dict__`` — consumes every field
|
|
@@ -50,6 +63,7 @@ before writing it) is precise and remains a counted read.
|
|
|
50
63
|
import ast
|
|
51
64
|
import os
|
|
52
65
|
import sys
|
|
66
|
+
from collections.abc import Iterator
|
|
53
67
|
from pathlib import Path
|
|
54
68
|
|
|
55
69
|
_blocking_directory = str(Path(__file__).resolve().parent)
|
|
@@ -137,79 +151,242 @@ def _reads_whole_instance_reflectively(tree: ast.Module) -> bool:
|
|
|
137
151
|
return False
|
|
138
152
|
|
|
139
153
|
|
|
140
|
-
def
|
|
141
|
-
"""Return
|
|
154
|
+
def _config_dataclass_names(tree: ast.Module) -> set[str]:
|
|
155
|
+
"""Return names of ``*Config`` ``@dataclass`` classes defined in a module.
|
|
156
|
+
|
|
157
|
+
A constructor-keyword exclusion fires only for a callee that names a genuine
|
|
158
|
+
config dataclass, so the caller first gathers the config dataclass names a
|
|
159
|
+
module defines, then unions those names across the scan root.
|
|
160
|
+
|
|
161
|
+
Args:
|
|
162
|
+
tree: The parsed module to inspect.
|
|
163
|
+
|
|
164
|
+
Returns:
|
|
165
|
+
Every class name in the module that is a ``@dataclass`` whose name ends in
|
|
166
|
+
the config class name suffix.
|
|
167
|
+
"""
|
|
168
|
+
config_dataclass_names: set[str] = set()
|
|
169
|
+
for each_node in ast.walk(tree):
|
|
170
|
+
if isinstance(each_node, ast.ClassDef) and _is_config_dataclass(each_node):
|
|
171
|
+
config_dataclass_names.add(each_node.name)
|
|
172
|
+
return config_dataclass_names
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def _call_constructs_config_class(
|
|
176
|
+
call_node: ast.Call, all_known_config_class_names: set[str]
|
|
177
|
+
) -> bool:
|
|
178
|
+
"""Return whether a call constructs a known ``*Config`` dataclass.
|
|
179
|
+
|
|
180
|
+
A call whose callee names a ``*Config`` dataclass defined under the scan root —
|
|
181
|
+
``AppInfoConfig(...)`` or a qualified ``module.AppInfoConfig(...)`` —
|
|
182
|
+
constructs a config instance, and its keyword arguments write the named fields
|
|
183
|
+
rather than read them. A factory function whose name merely ends in
|
|
184
|
+
``"Config"`` (``getThemeConfig(...)``) is not a known config dataclass, so its
|
|
185
|
+
keyword arguments stay genuine reads.
|
|
186
|
+
|
|
187
|
+
Args:
|
|
188
|
+
call_node: The call expression to test.
|
|
189
|
+
all_known_config_class_names: Names of ``*Config`` dataclasses defined under
|
|
190
|
+
the scan root.
|
|
191
|
+
|
|
192
|
+
Returns:
|
|
193
|
+
True when the callee names a known ``*Config`` dataclass.
|
|
194
|
+
"""
|
|
195
|
+
callee_node = call_node.func
|
|
196
|
+
if isinstance(callee_node, ast.Name):
|
|
197
|
+
return callee_node.id in all_known_config_class_names
|
|
198
|
+
if isinstance(callee_node, ast.Attribute):
|
|
199
|
+
return callee_node.attr in all_known_config_class_names
|
|
200
|
+
return False
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def _config_constructor_keyword_node_ids(
|
|
204
|
+
tree: ast.Module, all_known_config_class_names: set[str]
|
|
205
|
+
) -> set[int]:
|
|
206
|
+
"""Return ids of keyword nodes that write fields in a known ``*Config`` constructor.
|
|
207
|
+
|
|
208
|
+
A keyword in an ``AppInfoConfig(field=value)`` call sets ``field`` rather than
|
|
209
|
+
reading it, so its node id is collected for the caller to exclude. The
|
|
210
|
+
exclusion is keyed per keyword node, not by name, so a same-named keyword in a
|
|
211
|
+
``replace(cfg, field=value)`` call — which reuses a live instance and stays a
|
|
212
|
+
read — keeps its own distinct node and is not stripped.
|
|
213
|
+
|
|
214
|
+
Args:
|
|
215
|
+
tree: The parsed module to inspect.
|
|
216
|
+
all_known_config_class_names: Names of ``*Config`` dataclasses defined under
|
|
217
|
+
the scan root.
|
|
218
|
+
|
|
219
|
+
Returns:
|
|
220
|
+
The ``id()`` of every keyword node passed to a known ``*Config``
|
|
221
|
+
constructor call.
|
|
222
|
+
"""
|
|
223
|
+
constructor_keyword_node_ids: set[int] = set()
|
|
224
|
+
for each_node in ast.walk(tree):
|
|
225
|
+
if not isinstance(each_node, ast.Call):
|
|
226
|
+
continue
|
|
227
|
+
if not _call_constructs_config_class(each_node, all_known_config_class_names):
|
|
228
|
+
continue
|
|
229
|
+
for each_keyword in each_node.keywords:
|
|
230
|
+
if each_keyword.arg is not None:
|
|
231
|
+
constructor_keyword_node_ids.add(id(each_keyword))
|
|
232
|
+
return constructor_keyword_node_ids
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
def _self_referential_default_attribute_node_ids(
|
|
236
|
+
field_name: str, default_value: ast.expr
|
|
237
|
+
) -> set[int]:
|
|
238
|
+
"""Return ids of attribute reads in a default whose name equals the field.
|
|
239
|
+
|
|
240
|
+
Walks a ``*Config`` field's default-value expression and collects the ``id()``
|
|
241
|
+
of each ``ast.Attribute`` read whose ``.attr`` equals ``field_name``. Such a
|
|
242
|
+
read — ``sound_upload_timeout_ms: int = submission_timing.sound_upload_timeout_ms``
|
|
243
|
+
— names the field being defined inside the class body, so it is not a consumer
|
|
244
|
+
of the field. An attribute read of a differently-named field
|
|
245
|
+
(``timeout_ms: int = other_config.base_timeout``) is a genuine consumer and is
|
|
246
|
+
left out of the returned set.
|
|
247
|
+
|
|
248
|
+
Args:
|
|
249
|
+
field_name: The name of the field being defined.
|
|
250
|
+
default_value: The default-value expression of that field.
|
|
251
|
+
|
|
252
|
+
Returns:
|
|
253
|
+
The ``id()`` of every self-referential attribute read inside the
|
|
254
|
+
default-value expression.
|
|
255
|
+
"""
|
|
256
|
+
self_referential_node_ids: set[int] = set()
|
|
257
|
+
for each_inner_node in ast.walk(default_value):
|
|
258
|
+
if isinstance(each_inner_node, ast.Attribute) and each_inner_node.attr == field_name:
|
|
259
|
+
self_referential_node_ids.add(id(each_inner_node))
|
|
260
|
+
return self_referential_node_ids
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
def _config_field_default_value_nodes(tree: ast.Module) -> set[int]:
|
|
264
|
+
"""Return ids of self-referential attribute reads in ``*Config`` field defaults.
|
|
265
|
+
|
|
266
|
+
A field default such as ``sound_upload_timeout_ms: int =``
|
|
267
|
+
``submission_timing.sound_upload_timeout_ms`` is an attribute read whose name
|
|
268
|
+
matches the field being defined. That self-referential read inside the config
|
|
269
|
+
class body is not a consumer of the field, so its node id is collected here for
|
|
270
|
+
the caller to exclude from the attribute-read set. Only the attribute read
|
|
271
|
+
whose ``.attr`` equals the field name is collected; a default that sources a
|
|
272
|
+
differently-named field on another object
|
|
273
|
+
(``timeout_ms: int = other_config.base_timeout``) leaves that read counted, so
|
|
274
|
+
``base_timeout`` stays a live consumer.
|
|
275
|
+
|
|
276
|
+
Args:
|
|
277
|
+
tree: The parsed module to inspect.
|
|
278
|
+
|
|
279
|
+
Returns:
|
|
280
|
+
The ``id()`` of every self-referential attribute read within the
|
|
281
|
+
default-value expression of a field declared in a ``*Config`` dataclass
|
|
282
|
+
body.
|
|
283
|
+
"""
|
|
284
|
+
default_value_node_ids: set[int] = set()
|
|
285
|
+
for each_node in ast.walk(tree):
|
|
286
|
+
if not isinstance(each_node, ast.ClassDef) or not _is_config_dataclass(each_node):
|
|
287
|
+
continue
|
|
288
|
+
for each_statement in each_node.body:
|
|
289
|
+
if not isinstance(each_statement, ast.AnnAssign):
|
|
290
|
+
continue
|
|
291
|
+
if each_statement.value is None:
|
|
292
|
+
continue
|
|
293
|
+
if not isinstance(each_statement.target, ast.Name):
|
|
294
|
+
continue
|
|
295
|
+
default_value_node_ids |= _self_referential_default_attribute_node_ids(
|
|
296
|
+
each_statement.target.id, each_statement.value
|
|
297
|
+
)
|
|
298
|
+
return default_value_node_ids
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
def _attribute_read_names_in_tree(
|
|
302
|
+
tree: ast.Module, all_known_config_class_names: set[str]
|
|
303
|
+
) -> tuple[set[str], bool]:
|
|
304
|
+
"""Return attribute names read in a parsed module and a suppression flag.
|
|
142
305
|
|
|
143
306
|
Collects attribute names via five mechanisms: ``ast.Attribute.attr`` values
|
|
144
307
|
in Load context, augmented-assignment targets (so ``cfg.debug_port += 1``
|
|
145
308
|
contributes ``"debug_port"`` because ``+=`` reads the attribute before
|
|
146
309
|
writing it), string literals (so ``getattr(obj, "field")`` contributes
|
|
147
|
-
``"field"``), keyword-argument names (so ``
|
|
148
|
-
|
|
149
|
-
``
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
310
|
+
``"field"``), keyword-argument names (so ``replace(cfg, debug_port=1)``
|
|
311
|
+
contributes ``"debug_port"``), and ``ast.MatchClass.kwd_attrs`` names (so
|
|
312
|
+
``case Config(field=x)`` contributes ``"field"``). Two field-write forms are
|
|
313
|
+
excluded because they name a field without consuming it: a keyword that writes
|
|
314
|
+
a field in a known ``*Config`` constructor (``AppInfoConfig(field=value)``,
|
|
315
|
+
excluded per keyword node so a same-named ``replace`` keyword stays a read),
|
|
316
|
+
and a self-referential attribute read inside a ``*Config`` dataclass field's
|
|
317
|
+
own default-value expression (``field: int = source.field`` in the class body,
|
|
318
|
+
excluded only when the read name equals the field name) — counting either
|
|
319
|
+
would hide a field that is written but read by no module. A keyword passed to a
|
|
320
|
+
factory function whose name merely ends in ``"Config"`` is not a known config
|
|
321
|
+
constructor, so it stays a read. The boolean reports whether the module
|
|
322
|
+
suppresses the dead-field check, which it does only when it reflectively reads a
|
|
323
|
+
whole instance — a bare or ``dataclasses``-qualified
|
|
324
|
+
``asdict``/``astuple``/``fields``/``replace``/``vars`` call, or an
|
|
325
|
+
``obj.__dict__`` read — because that pattern reads every field at once without
|
|
326
|
+
naming any single field, so the caller treats it as "cannot prove any field
|
|
327
|
+
dead".
|
|
157
328
|
|
|
158
329
|
Args:
|
|
159
|
-
|
|
330
|
+
tree: The parsed module to inspect.
|
|
331
|
+
all_known_config_class_names: Names of ``*Config`` dataclasses defined under
|
|
332
|
+
the scan root, used to scope the constructor-keyword exclusion to
|
|
333
|
+
genuine config constructors.
|
|
160
334
|
|
|
161
335
|
Returns:
|
|
162
336
|
A (read_names, suppresses_dead_field_check) pair. The name set is every
|
|
163
|
-
attribute name the module reads via
|
|
164
|
-
|
|
165
|
-
|
|
337
|
+
attribute name the module reads via the mechanisms above, excluding known
|
|
338
|
+
``*Config`` constructor keyword nodes and self-referential config-field
|
|
339
|
+
default-value attribute reads; suppresses_dead_field_check is True only when
|
|
340
|
+
a reflective whole-instance read is present.
|
|
166
341
|
"""
|
|
167
|
-
try:
|
|
168
|
-
tree = ast.parse(source)
|
|
169
|
-
except SyntaxError:
|
|
170
|
-
return set(), False
|
|
171
342
|
all_read_names: set[str] = _augmented_assignment_attribute_names(tree)
|
|
343
|
+
config_constructor_keyword_node_ids = _config_constructor_keyword_node_ids(
|
|
344
|
+
tree, all_known_config_class_names
|
|
345
|
+
)
|
|
346
|
+
config_field_default_node_ids = _config_field_default_value_nodes(tree)
|
|
172
347
|
for each_node in ast.walk(tree):
|
|
173
|
-
if
|
|
348
|
+
if (
|
|
349
|
+
isinstance(each_node, ast.Attribute)
|
|
350
|
+
and isinstance(each_node.ctx, ast.Load)
|
|
351
|
+
and id(each_node) not in config_field_default_node_ids
|
|
352
|
+
):
|
|
174
353
|
all_read_names.add(each_node.attr)
|
|
175
354
|
elif isinstance(each_node, ast.Constant) and isinstance(each_node.value, str):
|
|
176
355
|
all_read_names.add(each_node.value)
|
|
177
356
|
elif isinstance(each_node, ast.MatchClass):
|
|
178
357
|
all_read_names.update(each_node.kwd_attrs)
|
|
179
|
-
elif
|
|
358
|
+
elif (
|
|
359
|
+
isinstance(each_node, ast.keyword)
|
|
360
|
+
and each_node.arg is not None
|
|
361
|
+
and id(each_node) not in config_constructor_keyword_node_ids
|
|
362
|
+
):
|
|
180
363
|
all_read_names.add(each_node.arg)
|
|
181
364
|
suppresses_dead_field_check = _reads_whole_instance_reflectively(tree)
|
|
182
365
|
return all_read_names, suppresses_dead_field_check
|
|
183
366
|
|
|
184
367
|
|
|
185
|
-
def
|
|
368
|
+
def _iter_production_module_sources(
|
|
186
369
|
scan_root: Path,
|
|
187
370
|
written_path: Path,
|
|
188
371
|
written_content: str,
|
|
189
|
-
) ->
|
|
190
|
-
"""
|
|
372
|
+
) -> Iterator[str | None]:
|
|
373
|
+
"""Yield the source of each production module under the scan root.
|
|
191
374
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
``replace``/``vars`` call, or an ``obj.__dict__`` read — sets the suppression
|
|
198
|
-
flag, signalling the caller that no field can be proven dead.
|
|
375
|
+
Yields ``written_content`` for the written module so the current edit is
|
|
376
|
+
included, then each sibling production module's source (excluding test and
|
|
377
|
+
migration files). A sibling whose text cannot be read is skipped. A single
|
|
378
|
+
``None`` is yielded when the production module count exceeds the configured
|
|
379
|
+
file cap, signalling the caller that no field can be proven dead.
|
|
199
380
|
|
|
200
381
|
Args:
|
|
201
382
|
scan_root: The directory tree to scan.
|
|
202
383
|
written_path: The resolved path of the module being written.
|
|
203
384
|
written_content: The post-edit text of the written module.
|
|
204
385
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
set is the union of attribute reads across every scanned production
|
|
208
|
-
module; cap_was_hit is True when the scan stopped at the configured file
|
|
209
|
-
cap before finishing the tree; suppresses_dead_field_check is True when
|
|
210
|
-
any scanned module reflectively reads a whole instance.
|
|
386
|
+
Yields:
|
|
387
|
+
Each production module's source text, or a single ``None`` on a cap hit.
|
|
211
388
|
"""
|
|
212
|
-
|
|
389
|
+
yield written_content
|
|
213
390
|
written_path_key = os.path.normcase(str(written_path))
|
|
214
391
|
scanned_file_count = 1
|
|
215
392
|
for each_path in scan_root.rglob("*" + PYTHON_SOURCE_SUFFIX):
|
|
@@ -223,17 +400,71 @@ def _all_production_read_names_under_root(
|
|
|
223
400
|
continue
|
|
224
401
|
scanned_file_count += 1
|
|
225
402
|
if scanned_file_count > MAX_SCAN_ROOT_FILE_COUNT:
|
|
226
|
-
|
|
403
|
+
yield None
|
|
404
|
+
return
|
|
227
405
|
try:
|
|
228
|
-
|
|
406
|
+
yield each_path.read_text(encoding="utf-8")
|
|
229
407
|
except (OSError, UnicodeDecodeError):
|
|
230
408
|
continue
|
|
231
|
-
|
|
232
|
-
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
def _all_production_read_names_under_root(
|
|
412
|
+
scan_root: Path,
|
|
413
|
+
written_path: Path,
|
|
414
|
+
written_content: str,
|
|
415
|
+
) -> tuple[set[str], bool, bool]:
|
|
416
|
+
"""Return read names, a cap-hit flag, and a suppression flag for the tree.
|
|
417
|
+
|
|
418
|
+
Reads and AST-parses every production ``.py`` module under ``scan_root``
|
|
419
|
+
(excluding test and migration files) at most once: the module sources are
|
|
420
|
+
materialized once, a first pass over the cached sources gathers every
|
|
421
|
+
``*Config`` dataclass name defined under the root so the constructor-keyword
|
|
422
|
+
exclusion fires only for a genuine config constructor, and a second pass over
|
|
423
|
+
the same cached sources collects attribute reads. The written module's
|
|
424
|
+
post-edit content replaces its on-disk text so the current edit is included.
|
|
425
|
+
Scanning stops at the configured file cap. A module that reflectively reads a
|
|
426
|
+
whole instance — a bare or ``dataclasses``-qualified
|
|
427
|
+
``asdict``/``astuple``/``fields``/``replace``/``vars`` call, or an
|
|
428
|
+
``obj.__dict__`` read — sets the suppression flag, signalling the caller that
|
|
429
|
+
no field can be proven dead.
|
|
430
|
+
|
|
431
|
+
Args:
|
|
432
|
+
scan_root: The directory tree to scan.
|
|
433
|
+
written_path: The resolved path of the module being written.
|
|
434
|
+
written_content: The post-edit text of the written module.
|
|
435
|
+
|
|
436
|
+
Returns:
|
|
437
|
+
A (read_names, cap_was_hit, suppresses_dead_field_check) triple. The name
|
|
438
|
+
set is the union of attribute reads across every scanned production
|
|
439
|
+
module; cap_was_hit is True when the scan stopped at the configured file
|
|
440
|
+
cap before finishing the tree; suppresses_dead_field_check is True when
|
|
441
|
+
any scanned module reflectively reads a whole instance.
|
|
442
|
+
"""
|
|
443
|
+
all_module_sources = list(
|
|
444
|
+
_iter_production_module_sources(scan_root, written_path, written_content)
|
|
445
|
+
)
|
|
446
|
+
if None in all_module_sources:
|
|
447
|
+
return set(), True, False
|
|
448
|
+
all_production_trees: list[ast.Module] = []
|
|
449
|
+
for each_source in all_module_sources:
|
|
450
|
+
if each_source is None:
|
|
451
|
+
continue
|
|
452
|
+
try:
|
|
453
|
+
all_production_trees.append(ast.parse(each_source))
|
|
454
|
+
except SyntaxError:
|
|
455
|
+
continue
|
|
456
|
+
all_known_config_class_names: set[str] = set()
|
|
457
|
+
for each_tree in all_production_trees:
|
|
458
|
+
all_known_config_class_names |= _config_dataclass_names(each_tree)
|
|
459
|
+
all_read_names: set[str] = set()
|
|
460
|
+
suppresses_dead_field_check = False
|
|
461
|
+
for each_tree in all_production_trees:
|
|
462
|
+
module_read_names, module_suppresses_dead_field_check = _attribute_read_names_in_tree(
|
|
463
|
+
each_tree, all_known_config_class_names
|
|
233
464
|
)
|
|
234
|
-
all_read_names |=
|
|
465
|
+
all_read_names |= module_read_names
|
|
235
466
|
suppresses_dead_field_check = (
|
|
236
|
-
suppresses_dead_field_check or
|
|
467
|
+
suppresses_dead_field_check or module_suppresses_dead_field_check
|
|
237
468
|
)
|
|
238
469
|
return all_read_names, False, suppresses_dead_field_check
|
|
239
470
|
|
|
@@ -247,9 +478,12 @@ def check_dead_config_dataclass_fields(
|
|
|
247
478
|
in ``"Config"``. For each such config dataclass in the written file, every
|
|
248
479
|
instance field whose name does not appear as an attribute read (``obj.field``),
|
|
249
480
|
augmented-assignment target (``cfg.field += 1``), string literal,
|
|
250
|
-
keyword-argument name (
|
|
481
|
+
non-constructor keyword-argument name (``replace`` keyword), or match-pattern
|
|
251
482
|
keyword attribute in any production module under the enclosing scan root is
|
|
252
|
-
flagged as dead.
|
|
483
|
+
flagged as dead. A keyword that writes a field in a ``*Config`` constructor
|
|
484
|
+
(``ThemeUpdateConfig(debug_port=1)``) is a write, not a read, so it does not
|
|
485
|
+
clear a field — a field set by a constructor keyword and read by no module is
|
|
486
|
+
flagged. When any production module under the scan root reflectively
|
|
253
487
|
reads a whole instance — a bare or ``dataclasses``-qualified call to
|
|
254
488
|
``asdict``, ``astuple``, ``fields``, ``replace``, or ``vars``, or a read of
|
|
255
489
|
``obj.__dict__`` — the check is suppressed for the whole tree and returns
|
|
@@ -223,19 +223,260 @@ def test_does_not_flag_field_used_only_as_replace_keyword(neutral_root: Path) ->
|
|
|
223
223
|
)
|
|
224
224
|
|
|
225
225
|
|
|
226
|
-
def
|
|
226
|
+
def test_flags_field_set_only_by_constructor_keyword_and_read_nowhere(
|
|
227
|
+
neutral_root: Path,
|
|
228
|
+
) -> None:
|
|
229
|
+
"""A field set ONLY by a ``*Config`` constructor keyword, read nowhere, is dead.
|
|
230
|
+
|
|
231
|
+
A constructor keyword writes the field; it is not a read. When ``debug_port``
|
|
232
|
+
is set by ``ThemeUpdateConfig(debug_port=1)`` and read through no config
|
|
233
|
+
instance anywhere in production, tuning it has no effect, so it is flagged as
|
|
234
|
+
dead config (CODE_RULES §9.8).
|
|
235
|
+
"""
|
|
227
236
|
consumer_body = (
|
|
228
237
|
"from os_update_workflow.config import ThemeUpdateConfig\n"
|
|
229
238
|
"\n"
|
|
230
239
|
"def build() -> ThemeUpdateConfig:\n"
|
|
231
|
-
"
|
|
240
|
+
" configuration = ThemeUpdateConfig(portal_url='x', debug_port=1, timeout_seconds=99)\n"
|
|
241
|
+
" print(configuration.portal_url)\n"
|
|
242
|
+
" print(configuration.timeout_seconds)\n"
|
|
243
|
+
" return configuration\n"
|
|
232
244
|
)
|
|
233
245
|
config_path = _build_config_package(
|
|
234
246
|
neutral_root / "workflow", THEME_UPDATE_CONFIG_BODY, consumer_body
|
|
235
247
|
)
|
|
236
248
|
issues = _check(THEME_UPDATE_CONFIG_BODY, str(config_path))
|
|
249
|
+
assert any("'debug_port'" in each_issue for each_issue in issues), (
|
|
250
|
+
f"Field set only by constructor keyword and read nowhere must be flagged, got: {issues}"
|
|
251
|
+
)
|
|
252
|
+
assert not any(
|
|
253
|
+
"'portal_url'" in each_issue or "'timeout_seconds'" in each_issue for each_issue in issues
|
|
254
|
+
), f"Fields read through the config instance must not be flagged, got: {issues}"
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
def test_qualified_config_constructor_keyword_does_not_clear_field(
|
|
258
|
+
neutral_root: Path,
|
|
259
|
+
) -> None:
|
|
260
|
+
"""A qualified ``module.ThemeUpdateConfig(field=value)`` keyword is a write, not a read.
|
|
261
|
+
|
|
262
|
+
The constructor callee may be a qualified attribute (``config_module.ThemeUpdateConfig``)
|
|
263
|
+
rather than a bare name. Its keyword still writes the field, so a field set only
|
|
264
|
+
this way and read through no config instance is flagged dead.
|
|
265
|
+
"""
|
|
266
|
+
consumer_body = (
|
|
267
|
+
"import os_update_workflow.config as config_module\n"
|
|
268
|
+
"\n"
|
|
269
|
+
"def build() -> config_module.ThemeUpdateConfig:\n"
|
|
270
|
+
" configuration = config_module.ThemeUpdateConfig(\n"
|
|
271
|
+
" portal_url='x', debug_port=1, timeout_seconds=99\n"
|
|
272
|
+
" )\n"
|
|
273
|
+
" print(configuration.portal_url)\n"
|
|
274
|
+
" print(configuration.timeout_seconds)\n"
|
|
275
|
+
" return configuration\n"
|
|
276
|
+
)
|
|
277
|
+
config_path = _build_config_package(
|
|
278
|
+
neutral_root / "workflow", THEME_UPDATE_CONFIG_BODY, consumer_body
|
|
279
|
+
)
|
|
280
|
+
issues = _check(THEME_UPDATE_CONFIG_BODY, str(config_path))
|
|
281
|
+
assert any("'debug_port'" in each_issue for each_issue in issues), (
|
|
282
|
+
f"A qualified config constructor keyword is a write, so debug_port is flagged, got: {issues}"
|
|
283
|
+
)
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
def test_does_not_flag_field_set_by_constructor_keyword_and_read_elsewhere(
|
|
287
|
+
neutral_root: Path,
|
|
288
|
+
) -> None:
|
|
289
|
+
"""A field set by a constructor keyword AND read via attribute elsewhere is live.
|
|
290
|
+
|
|
291
|
+
The constructor keyword does not clear the field, but a genuine attribute read
|
|
292
|
+
of the same field in another module does, so the field is not flagged.
|
|
293
|
+
"""
|
|
294
|
+
builder_body = (
|
|
295
|
+
"from os_update_workflow.config import ThemeUpdateConfig\n"
|
|
296
|
+
"\n"
|
|
297
|
+
"def build() -> ThemeUpdateConfig:\n"
|
|
298
|
+
" return ThemeUpdateConfig(portal_url='x', debug_port=1, timeout_seconds=99)\n"
|
|
299
|
+
)
|
|
300
|
+
config_path = _build_config_package(
|
|
301
|
+
neutral_root / "workflow", THEME_UPDATE_CONFIG_BODY, builder_body
|
|
302
|
+
)
|
|
303
|
+
reader_body = (
|
|
304
|
+
"from os_update_workflow.config import ThemeUpdateConfig\n"
|
|
305
|
+
"\n"
|
|
306
|
+
"def connect(configuration: ThemeUpdateConfig) -> int:\n"
|
|
307
|
+
" return configuration.debug_port\n"
|
|
308
|
+
)
|
|
309
|
+
(config_path.parent.parent / "reader.py").write_text(reader_body, encoding="utf-8")
|
|
310
|
+
issues = _check(THEME_UPDATE_CONFIG_BODY, str(config_path))
|
|
311
|
+
assert not any("'debug_port'" in each_issue for each_issue in issues), (
|
|
312
|
+
f"An attribute read in another module keeps debug_port live, got: {issues}"
|
|
313
|
+
)
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
def test_flags_field_set_by_default_and_constructor_keyword_only(
|
|
317
|
+
neutral_root: Path,
|
|
318
|
+
) -> None:
|
|
319
|
+
"""The PR #317 dead-config shape: field set by config default + constructor keyword only.
|
|
320
|
+
|
|
321
|
+
``AppInfoConfig.sound_upload_max_attempts: int = submission_timing.sound_upload_max_attempts``
|
|
322
|
+
sets the field from a same-named attribute on another object inside the config
|
|
323
|
+
body, and the orchestrator sets ``sound_upload_timeout_ms`` by a constructor
|
|
324
|
+
keyword. Neither field is read through any config instance. The default-value
|
|
325
|
+
read inside the config body and the constructor keyword are both writes, so
|
|
326
|
+
both fields are flagged dead.
|
|
327
|
+
|
|
328
|
+
Residual limitation: when a consumer module's constructor VALUE expression
|
|
329
|
+
itself reads a same-named attribute on a different object
|
|
330
|
+
(``AppInfoConfig(field=other.field)``), the object-blind attribute-read
|
|
331
|
+
collector counts ``field`` as read and the field escapes. This test keeps the
|
|
332
|
+
constructor value a literal so the keyword-write and default-write exclusions
|
|
333
|
+
are exercised without that foreign-attribute leak.
|
|
334
|
+
"""
|
|
335
|
+
config_body = (
|
|
336
|
+
"from dataclasses import dataclass\n"
|
|
337
|
+
"import submission_timing_module as submission_timing\n"
|
|
338
|
+
"\n"
|
|
339
|
+
"@dataclass(frozen=True)\n"
|
|
340
|
+
"class AppInfoConfig:\n"
|
|
341
|
+
" sound_upload_timeout_ms: int = submission_timing.sound_upload_timeout_ms\n"
|
|
342
|
+
" sound_upload_max_attempts: int = submission_timing.sound_upload_max_attempts\n"
|
|
343
|
+
)
|
|
344
|
+
workflow_directory = neutral_root / "workflow"
|
|
345
|
+
config_package = workflow_directory / "os_update_workflow"
|
|
346
|
+
config_package.mkdir(parents=True)
|
|
347
|
+
(config_package / "__init__.py").write_text("", encoding="utf-8")
|
|
348
|
+
config_path = config_package / "config.py"
|
|
349
|
+
config_path.write_text(config_body, encoding="utf-8")
|
|
350
|
+
orchestrator_body = (
|
|
351
|
+
"from os_update_workflow.config import AppInfoConfig\n"
|
|
352
|
+
"\n"
|
|
353
|
+
"def build() -> AppInfoConfig:\n"
|
|
354
|
+
" config = AppInfoConfig(sound_upload_timeout_ms=60000)\n"
|
|
355
|
+
" return config\n"
|
|
356
|
+
)
|
|
357
|
+
(workflow_directory / "orchestrator.py").write_text(orchestrator_body, encoding="utf-8")
|
|
358
|
+
issues = _check(config_body, str(config_path))
|
|
359
|
+
assert any("'sound_upload_timeout_ms'" in each_issue for each_issue in issues), (
|
|
360
|
+
f"Field set by constructor keyword and read nowhere must be flagged, got: {issues}"
|
|
361
|
+
)
|
|
362
|
+
assert any("'sound_upload_max_attempts'" in each_issue for each_issue in issues), (
|
|
363
|
+
f"Field set by config default only and read nowhere must be flagged, got: {issues}"
|
|
364
|
+
)
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
def test_does_not_flag_config_default_field_read_through_instance(
|
|
368
|
+
neutral_root: Path,
|
|
369
|
+
) -> None:
|
|
370
|
+
"""A field whose default reads a foreign attribute but is read through the instance is live.
|
|
371
|
+
|
|
372
|
+
The default-value exclusion only drops the self-referential read inside the
|
|
373
|
+
config body; a genuine ``config.sound_upload_timeout_ms`` read in production
|
|
374
|
+
keeps the field live.
|
|
375
|
+
"""
|
|
376
|
+
config_body = (
|
|
377
|
+
"from dataclasses import dataclass\n"
|
|
378
|
+
"import submission_timing_module as submission_timing\n"
|
|
379
|
+
"\n"
|
|
380
|
+
"@dataclass(frozen=True)\n"
|
|
381
|
+
"class AppInfoConfig:\n"
|
|
382
|
+
" sound_upload_timeout_ms: int = submission_timing.sound_upload_timeout_ms\n"
|
|
383
|
+
)
|
|
384
|
+
workflow_directory = neutral_root / "workflow"
|
|
385
|
+
config_package = workflow_directory / "os_update_workflow"
|
|
386
|
+
config_package.mkdir(parents=True)
|
|
387
|
+
(config_package / "__init__.py").write_text("", encoding="utf-8")
|
|
388
|
+
config_path = config_package / "config.py"
|
|
389
|
+
config_path.write_text(config_body, encoding="utf-8")
|
|
390
|
+
processor_body = (
|
|
391
|
+
"from os_update_workflow.config import AppInfoConfig\n"
|
|
392
|
+
"\n"
|
|
393
|
+
"def wait(config: AppInfoConfig) -> int:\n"
|
|
394
|
+
" return config.sound_upload_timeout_ms\n"
|
|
395
|
+
)
|
|
396
|
+
(workflow_directory / "processor.py").write_text(processor_body, encoding="utf-8")
|
|
397
|
+
issues = _check(config_body, str(config_path))
|
|
237
398
|
assert issues == [], (
|
|
238
|
-
f"
|
|
399
|
+
f"A genuine config-instance read keeps the field live, got: {issues}"
|
|
400
|
+
)
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
def test_config_default_sourcing_differently_named_field_keeps_it_live(
|
|
404
|
+
neutral_root: Path,
|
|
405
|
+
) -> None:
|
|
406
|
+
"""A default that sources a DIFFERENTLY-named field on another config keeps it live.
|
|
407
|
+
|
|
408
|
+
``AppConfig.timeout_ms: int = DEFAULT_TIMING.base_timeout`` reads
|
|
409
|
+
``base_timeout`` on another config object inside the class body. The
|
|
410
|
+
default-value exclusion drops only the self-referential read whose attribute
|
|
411
|
+
name equals the field being defined; a differently-named attribute read is a
|
|
412
|
+
genuine consumer of ``base_timeout``, so it stays live and is not flagged.
|
|
413
|
+
"""
|
|
414
|
+
config_body = (
|
|
415
|
+
"from dataclasses import dataclass\n"
|
|
416
|
+
"\n"
|
|
417
|
+
"@dataclass(frozen=True)\n"
|
|
418
|
+
"class TimingConfig:\n"
|
|
419
|
+
" base_timeout: int\n"
|
|
420
|
+
" poll_interval: int\n"
|
|
421
|
+
"\n"
|
|
422
|
+
"DEFAULT_TIMING = TimingConfig(base_timeout=30, poll_interval=5)\n"
|
|
423
|
+
"\n"
|
|
424
|
+
"@dataclass(frozen=True)\n"
|
|
425
|
+
"class AppConfig:\n"
|
|
426
|
+
" timeout_ms: int = DEFAULT_TIMING.base_timeout\n"
|
|
427
|
+
" poll_interval: int = DEFAULT_TIMING.poll_interval\n"
|
|
428
|
+
)
|
|
429
|
+
workflow_directory = neutral_root / "workflow"
|
|
430
|
+
config_package = workflow_directory / "os_update_workflow"
|
|
431
|
+
config_package.mkdir(parents=True)
|
|
432
|
+
(config_package / "__init__.py").write_text("", encoding="utf-8")
|
|
433
|
+
config_path = config_package / "config.py"
|
|
434
|
+
config_path.write_text(config_body, encoding="utf-8")
|
|
435
|
+
consumer_body = (
|
|
436
|
+
"from os_update_workflow.config import AppConfig\n"
|
|
437
|
+
"\n"
|
|
438
|
+
"def run(app_config: AppConfig) -> int:\n"
|
|
439
|
+
" return app_config.timeout_ms + app_config.poll_interval\n"
|
|
440
|
+
)
|
|
441
|
+
(workflow_directory / "runner.py").write_text(consumer_body, encoding="utf-8")
|
|
442
|
+
issues = _check(config_body, str(config_path))
|
|
443
|
+
assert not any("'base_timeout'" in each_issue for each_issue in issues), (
|
|
444
|
+
f"A default sourcing a differently-named field must keep it live, got: {issues}"
|
|
445
|
+
)
|
|
446
|
+
assert not any("'poll_interval'" in each_issue for each_issue in issues), (
|
|
447
|
+
f"poll_interval is read both in the default and the consumer, got: {issues}"
|
|
448
|
+
)
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
def test_self_referential_default_still_excludes_only_the_self_read(
|
|
452
|
+
neutral_root: Path,
|
|
453
|
+
) -> None:
|
|
454
|
+
"""The self-referential default read is still excluded after narrowing.
|
|
455
|
+
|
|
456
|
+
``sound_upload_timeout_ms: int = submission_timing.sound_upload_timeout_ms``
|
|
457
|
+
reads an attribute whose name equals the field being defined, so it is still
|
|
458
|
+
excluded; when that field is read by no module it stays flagged dead.
|
|
459
|
+
"""
|
|
460
|
+
config_body = (
|
|
461
|
+
"from dataclasses import dataclass\n"
|
|
462
|
+
"import submission_timing_module as submission_timing\n"
|
|
463
|
+
"\n"
|
|
464
|
+
"@dataclass(frozen=True)\n"
|
|
465
|
+
"class AppInfoConfig:\n"
|
|
466
|
+
" sound_upload_timeout_ms: int = submission_timing.sound_upload_timeout_ms\n"
|
|
467
|
+
)
|
|
468
|
+
workflow_directory = neutral_root / "workflow"
|
|
469
|
+
config_package = workflow_directory / "os_update_workflow"
|
|
470
|
+
config_package.mkdir(parents=True)
|
|
471
|
+
(config_package / "__init__.py").write_text("", encoding="utf-8")
|
|
472
|
+
config_path = config_package / "config.py"
|
|
473
|
+
config_path.write_text(config_body, encoding="utf-8")
|
|
474
|
+
(workflow_directory / "runner.py").write_text(
|
|
475
|
+
"def noop() -> None:\n pass\n", encoding="utf-8"
|
|
476
|
+
)
|
|
477
|
+
issues = _check(config_body, str(config_path))
|
|
478
|
+
assert any("'sound_upload_timeout_ms'" in each_issue for each_issue in issues), (
|
|
479
|
+
f"Self-referential default read is excluded, so the field stays flagged, got: {issues}"
|
|
239
480
|
)
|
|
240
481
|
|
|
241
482
|
|
|
@@ -416,6 +657,98 @@ def test_dataclasses_qualified_replace_call_suppresses_so_no_field_flagged(
|
|
|
416
657
|
)
|
|
417
658
|
|
|
418
659
|
|
|
660
|
+
def test_aliased_replace_read_keeps_field_live_despite_constructor_keyword(
|
|
661
|
+
neutral_root: Path,
|
|
662
|
+
) -> None:
|
|
663
|
+
"""An aliased ``replace`` keyword read survives a same-module constructor keyword.
|
|
664
|
+
|
|
665
|
+
A module constructs ``ThemeUpdateConfig(debug_port=1)`` (a write) and also reads
|
|
666
|
+
the field through an aliased ``dataclasses.replace`` —
|
|
667
|
+
``from dataclasses import replace as rep; rep(cfg, debug_port=2)``. The alias is
|
|
668
|
+
not the bare ``replace`` name, so the whole-instance reflective suppression does
|
|
669
|
+
not fire and the ``rep`` keyword stays a genuine field read. The constructor
|
|
670
|
+
keyword exclusion is scoped per-call, so the constructor's ``debug_port`` keyword
|
|
671
|
+
does not strip the ``rep`` keyword read and the field stays live.
|
|
672
|
+
"""
|
|
673
|
+
consumer_body = (
|
|
674
|
+
"from dataclasses import replace as rep\n"
|
|
675
|
+
"from os_update_workflow.config import ThemeUpdateConfig\n"
|
|
676
|
+
"\n"
|
|
677
|
+
"def build_then_repoint() -> ThemeUpdateConfig:\n"
|
|
678
|
+
" configuration = ThemeUpdateConfig(portal_url='x', debug_port=1, timeout_seconds=99)\n"
|
|
679
|
+
" return rep(configuration, debug_port=2)\n"
|
|
680
|
+
)
|
|
681
|
+
config_path = _build_config_package(
|
|
682
|
+
neutral_root / "workflow", THEME_UPDATE_CONFIG_BODY, consumer_body
|
|
683
|
+
)
|
|
684
|
+
issues = _check(THEME_UPDATE_CONFIG_BODY, str(config_path))
|
|
685
|
+
assert not any("'debug_port'" in each_issue for each_issue in issues), (
|
|
686
|
+
f"Aliased replace keyword read must keep debug_port live despite a"
|
|
687
|
+
f" same-module constructor keyword, got: {issues}"
|
|
688
|
+
)
|
|
689
|
+
|
|
690
|
+
|
|
691
|
+
def test_factory_function_ending_in_config_keeps_field_live(
|
|
692
|
+
neutral_root: Path,
|
|
693
|
+
) -> None:
|
|
694
|
+
"""A keyword to a factory FUNCTION ending in ``Config`` is a read, not a write.
|
|
695
|
+
|
|
696
|
+
``getThemeConfig(debug_port=1)`` calls a factory function, not a ``*Config``
|
|
697
|
+
dataclass constructor. The keyword passes a value into the function, so it
|
|
698
|
+
counts as a field read. The constructor-keyword exclusion fires only for a
|
|
699
|
+
callee that names a known ``*Config`` dataclass defined under the scan root, so
|
|
700
|
+
a factory function whose name merely ends in ``Config`` does not strip the
|
|
701
|
+
field.
|
|
702
|
+
"""
|
|
703
|
+
consumer_body = (
|
|
704
|
+
"from os_update_workflow.config import ThemeUpdateConfig\n"
|
|
705
|
+
"\n"
|
|
706
|
+
"def getThemeConfig(portal_url: str, debug_port: int) -> ThemeUpdateConfig:\n"
|
|
707
|
+
" return ThemeUpdateConfig(\n"
|
|
708
|
+
" portal_url=portal_url, debug_port=debug_port, timeout_seconds=30\n"
|
|
709
|
+
" )\n"
|
|
710
|
+
"\n"
|
|
711
|
+
"def run() -> ThemeUpdateConfig:\n"
|
|
712
|
+
" print('x')\n"
|
|
713
|
+
" return getThemeConfig(portal_url='x', debug_port=1)\n"
|
|
714
|
+
)
|
|
715
|
+
config_path = _build_config_package(
|
|
716
|
+
neutral_root / "workflow", THEME_UPDATE_CONFIG_BODY, consumer_body
|
|
717
|
+
)
|
|
718
|
+
issues = _check(THEME_UPDATE_CONFIG_BODY, str(config_path))
|
|
719
|
+
assert not any("'debug_port'" in each_issue for each_issue in issues), (
|
|
720
|
+
f"A keyword passed to a factory function ending in Config must keep the"
|
|
721
|
+
f" field live, got: {issues}"
|
|
722
|
+
)
|
|
723
|
+
|
|
724
|
+
|
|
725
|
+
def test_dead_config_field_module_has_no_collection_parameter_naming_violation() -> None:
|
|
726
|
+
"""The hook module's own source must pass the collection-parameter naming check.
|
|
727
|
+
|
|
728
|
+
The cross-module dead-config-field check passes a ``set[str]`` of config class
|
|
729
|
+
names through several helpers; every such collection parameter must carry the
|
|
730
|
+
``all_`` prefix (CODE_RULES §5). Run the real collection-prefix check over this
|
|
731
|
+
module's on-disk source so a regression that drops the prefix fails here.
|
|
732
|
+
"""
|
|
733
|
+
collection_naming_path = (
|
|
734
|
+
Path(__file__).resolve().parent / "code_rules_naming_collection.py"
|
|
735
|
+
)
|
|
736
|
+
naming_specification = importlib.util.spec_from_file_location(
|
|
737
|
+
"code_rules_naming_collection", collection_naming_path
|
|
738
|
+
)
|
|
739
|
+
assert naming_specification is not None and naming_specification.loader is not None
|
|
740
|
+
code_rules_naming_collection = importlib.util.module_from_spec(naming_specification)
|
|
741
|
+
naming_specification.loader.exec_module(code_rules_naming_collection)
|
|
742
|
+
module_path = Path(__file__).resolve().parent / "code_rules_dead_config_field.py"
|
|
743
|
+
module_source = module_path.read_text(encoding="utf-8")
|
|
744
|
+
issues = code_rules_naming_collection.check_collection_prefix(
|
|
745
|
+
module_source, str(module_path)
|
|
746
|
+
)
|
|
747
|
+
assert not any("Collection parameter" in each_issue for each_issue in issues), (
|
|
748
|
+
f"dead-config-field module has a collection-parameter naming violation: {issues}"
|
|
749
|
+
)
|
|
750
|
+
|
|
751
|
+
|
|
419
752
|
def test_validate_content_dispatch_runs_dead_config_field_check(neutral_root: Path) -> None:
|
|
420
753
|
workflow_directory = neutral_root / "workflow"
|
|
421
754
|
config_package = workflow_directory / "os_update_workflow"
|