smelt-cli 0.2.0__py3-none-win_amd64.whl

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.
Files changed (133) hide show
  1. smelt/__init__.py +6 -0
  2. smelt/__main__.py +5 -0
  3. smelt/analysis/__init__.py +45 -0
  4. smelt/analysis/annotations.py +105 -0
  5. smelt/analysis/cache.py +168 -0
  6. smelt/analysis/context.py +147 -0
  7. smelt/analysis/contracts.py +210 -0
  8. smelt/analysis/coverage.py +377 -0
  9. smelt/analysis/definition_blocks.py +569 -0
  10. smelt/analysis/enum_values.py +217 -0
  11. smelt/analysis/errors.py +11 -0
  12. smelt/analysis/exports.py +166 -0
  13. smelt/analysis/files.py +279 -0
  14. smelt/analysis/function_usage.py +123 -0
  15. smelt/analysis/graphs.py +77 -0
  16. smelt/analysis/identity.py +64 -0
  17. smelt/analysis/import_cache.py +98 -0
  18. smelt/analysis/imports.py +452 -0
  19. smelt/analysis/literals.py +17 -0
  20. smelt/analysis/member_state.py +108 -0
  21. smelt/analysis/members.py +975 -0
  22. smelt/analysis/ordering.py +399 -0
  23. smelt/analysis/ordering_rewrite.py +232 -0
  24. smelt/analysis/package_exports.py +58 -0
  25. smelt/analysis/parsing.py +126 -0
  26. smelt/analysis/python_version.py +164 -0
  27. smelt/analysis/retained.py +84 -0
  28. smelt/analysis/syntax.py +109 -0
  29. smelt/analysis/test_awaits.py +91 -0
  30. smelt/analysis/test_cases.py +84 -0
  31. smelt/analysis/test_deadlines.py +90 -0
  32. smelt/analysis/test_outcomes.py +71 -0
  33. smelt/analysis/test_subprocesses.py +107 -0
  34. smelt/analysis/test_syntax.py +1425 -0
  35. smelt/analysis/test_timing.py +76 -0
  36. smelt/analysis/type_dispatch.py +193 -0
  37. smelt/analysis/workspace.py +146 -0
  38. smelt/cli/__init__.py +3 -0
  39. smelt/cli/app.py +300 -0
  40. smelt/cli/commands/__init__.py +62 -0
  41. smelt/cli/commands/adopt.py +274 -0
  42. smelt/cli/commands/check.py +151 -0
  43. smelt/cli/commands/discover.py +51 -0
  44. smelt/cli/commands/info.py +112 -0
  45. smelt/cli/session.py +163 -0
  46. smelt/cli/session_slots.py +43 -0
  47. smelt/cli/support.py +90 -0
  48. smelt/config/__init__.py +36 -0
  49. smelt/config/discovery.py +93 -0
  50. smelt/config/errors.py +51 -0
  51. smelt/config/loader.py +282 -0
  52. smelt/config/models.py +717 -0
  53. smelt/config/patterns.py +47 -0
  54. smelt/config/schema.py +23 -0
  55. smelt/config/source.py +56 -0
  56. smelt/config/validation.py +180 -0
  57. smelt/diagnostics/__init__.py +0 -0
  58. smelt/diagnostics/debt.py +226 -0
  59. smelt/diagnostics/dedupe.py +41 -0
  60. smelt/diagnostics/groups.py +109 -0
  61. smelt/diagnostics/render/__init__.py +0 -0
  62. smelt/diagnostics/render/machine.py +237 -0
  63. smelt/diagnostics/render/text.py +321 -0
  64. smelt/diagnostics/report.py +104 -0
  65. smelt/diagnostics/suppressions.py +80 -0
  66. smelt/diagnostics/violation.py +120 -0
  67. smelt/docs.py +98 -0
  68. smelt/engine/__init__.py +0 -0
  69. smelt/engine/briefing.py +630 -0
  70. smelt/engine/cache.py +65 -0
  71. smelt/engine/changes.py +260 -0
  72. smelt/engine/check.py +724 -0
  73. smelt/engine/coverage.py +151 -0
  74. smelt/engine/evaluation.py +291 -0
  75. smelt/engine/file_checks.py +94 -0
  76. smelt/engine/git_source.py +140 -0
  77. smelt/engine/inference.py +854 -0
  78. smelt/engine/mirror_inference.py +95 -0
  79. smelt/engine/native_git.py +190 -0
  80. smelt/engine/ordering_fixes.py +132 -0
  81. smelt/engine/paths.py +163 -0
  82. smelt/engine/result_cache.py +208 -0
  83. smelt/engine/rule_settings.py +164 -0
  84. smelt/engine/schema.py +58 -0
  85. smelt/engine/staged.py +75 -0
  86. smelt/engine/workspace.py +5 -0
  87. smelt/model/__init__.py +9 -0
  88. smelt/model/architecture.py +234 -0
  89. smelt/py.typed +0 -0
  90. smelt/rules/__init__.py +0 -0
  91. smelt/rules/activation.py +129 -0
  92. smelt/rules/base.py +131 -0
  93. smelt/rules/catalog.json +600 -0
  94. smelt/rules/common.py +164 -0
  95. smelt/rules/dependencies/__init__.py +0 -0
  96. smelt/rules/dependencies/composition_root.py +86 -0
  97. smelt/rules/dependencies/cycles.py +329 -0
  98. smelt/rules/dependencies/fanout.py +81 -0
  99. smelt/rules/dependencies/features.py +165 -0
  100. smelt/rules/dependencies/interfaces.py +84 -0
  101. smelt/rules/dependencies/layers.py +138 -0
  102. smelt/rules/dependencies/third_party.py +132 -0
  103. smelt/rules/member_names.py +13 -0
  104. smelt/rules/meta.py +74 -0
  105. smelt/rules/registry.py +132 -0
  106. smelt/rules/structure/__init__.py +0 -0
  107. smelt/rules/structure/assignments.py +114 -0
  108. smelt/rules/structure/contracts.py +71 -0
  109. smelt/rules/structure/coverage.py +100 -0
  110. smelt/rules/structure/enum_values.py +50 -0
  111. smelt/rules/structure/headers.py +143 -0
  112. smelt/rules/structure/import_style.py +145 -0
  113. smelt/rules/structure/layout.py +202 -0
  114. smelt/rules/structure/members.py +116 -0
  115. smelt/rules/structure/naming.py +132 -0
  116. smelt/rules/structure/ordering.py +83 -0
  117. smelt/rules/structure/package_imports.py +117 -0
  118. smelt/rules/structure/type_dispatch.py +79 -0
  119. smelt/rules/testing/__init__.py +0 -0
  120. smelt/rules/testing/awaits.py +46 -0
  121. smelt/rules/testing/cases.py +47 -0
  122. smelt/rules/testing/location.py +537 -0
  123. smelt/rules/testing/mocking.py +140 -0
  124. smelt/rules/testing/outcomes.py +119 -0
  125. smelt/rules/testing/private_access.py +93 -0
  126. smelt/rules/testing/public_attributes.py +62 -0
  127. smelt/rules/testing/public_functions.py +52 -0
  128. smelt/rules/testing/timing.py +62 -0
  129. smelt_cli-0.2.0.data/scripts/smelt.exe +0 -0
  130. smelt_cli-0.2.0.dist-info/METADATA +103 -0
  131. smelt_cli-0.2.0.dist-info/RECORD +133 -0
  132. smelt_cli-0.2.0.dist-info/WHEEL +4 -0
  133. smelt_cli-0.2.0.dist-info/sboms/smelt-cli.cyclonedx.json +5462 -0
@@ -0,0 +1,452 @@
1
+ from __future__ import annotations
2
+
3
+ import ast
4
+ from collections import defaultdict, deque
5
+ from dataclasses import dataclass
6
+ from pathlib import Path
7
+ from typing import TYPE_CHECKING, cast
8
+
9
+ import grimp
10
+ from grimp import _rustgrimp as rust # type: ignore[attr-defined]
11
+ from grimp.adaptors.filesystem import FileSystem
12
+ from grimp.application.config import settings as grimp_settings
13
+ from grimp.application.ports.modulefinder import (
14
+ AbstractModuleFinder,
15
+ FoundPackage,
16
+ ModuleFile,
17
+ )
18
+ from grimp.application.ports.packagefinder import AbstractPackageFinder
19
+ from grimp.domain.valueobjects import Module
20
+ from grimp.exceptions import SourceSyntaxError
21
+ from pydantic import TypeAdapter
22
+
23
+ from smelt.analysis.import_cache import import_cache_type, resolution_identity
24
+ from smelt.analysis.parsing import (
25
+ AnalysisError,
26
+ AstCache,
27
+ python_note,
28
+ resolve_relative,
29
+ type_checking_lines,
30
+ )
31
+ from smelt.analysis.parsing import (
32
+ is_stdlib as is_stdlib, # noqa: PLC0414 - preserve the public import helper
33
+ )
34
+ from smelt.model import is_within
35
+
36
+ if TYPE_CHECKING:
37
+ from collections.abc import Iterator
38
+
39
+ from grimp.application.ports.filesystem import AbstractFileSystem, BasicFileSystem
40
+
41
+ from smelt.analysis.cache import FactCache
42
+ from smelt.analysis.files import FileIndex
43
+
44
+
45
+ class _SourceFileSystem(FileSystem):
46
+ def __init__(self, files: FileIndex) -> None:
47
+ self.files = files
48
+ self._basic: BasicFileSystem | None = None
49
+
50
+ def read(self, file_name: str) -> str:
51
+ return self.files.store.read_text(Path(file_name), encoding="utf-8-sig")
52
+
53
+ def exists(self, file_name: str) -> bool:
54
+ return self.files.store.is_file(Path(file_name))
55
+
56
+ def get_mtime(self, file_name: str) -> float:
57
+ identity = self.files.store.identity(Path(file_name))
58
+ if identity is not None:
59
+ return float(int(identity[-12:], 16))
60
+ return super().get_mtime(file_name)
61
+
62
+ def convert_to_basic(self) -> BasicFileSystem:
63
+ if self._basic is not None:
64
+ return self._basic
65
+ inventory = self.files.store.inventory()
66
+ if inventory is None:
67
+ return super().convert_to_basic()
68
+ # Native Grimp accepts its in-memory filesystem. Include unselected
69
+ # filenames for import resolution without parsing their source.
70
+ contents = {path.as_posix(): "" for path in inventory if path.suffix == ".py"}
71
+ basic = rust.FakeBasicFileSystem(content_map=contents)
72
+ # FakeBasicFileSystem's constructor strips indentation from its test
73
+ # fixtures. write() preserves the exact analyzed source instead.
74
+ for source in self.files.sources.values():
75
+ basic.write(source.absolute.as_posix(), self.files.read_text(source.path))
76
+ self._basic = cast("BasicFileSystem", basic)
77
+ return self._basic
78
+
79
+ def write(self, file_name: str, contents: str) -> None:
80
+ raise OSError("analysis sources are read-only")
81
+
82
+
83
+ @dataclass(frozen=True, slots=True)
84
+ class ImportDetail:
85
+ importer: str
86
+ # Grimp's resolved target; external packages are squashed to their top level.
87
+ imported: str
88
+ line: int
89
+ line_contents: str
90
+ # Fully qualified names the statement imports, e.g. ``sqlalchemy.orm.Session``.
91
+ names: tuple[str, ...]
92
+ type_checking: bool
93
+ external: bool
94
+ column: int
95
+ end_column: int
96
+
97
+ @property
98
+ def top_level(self) -> str:
99
+ return self.imported.split(".")[0]
100
+
101
+
102
+ _DETAILS = TypeAdapter(list[ImportDetail])
103
+ _CHAIN_CACHE_HOPS = 20_000
104
+
105
+
106
+ class _SourceRootPackageFinder(AbstractPackageFinder):
107
+ def __init__(self, files: FileIndex) -> None:
108
+ self._files = files
109
+
110
+ def determine_package_directories(
111
+ self, package_name: str, file_system: AbstractFileSystem
112
+ ) -> set[str]:
113
+ return {
114
+ self._files.resolve_path(path).as_posix()
115
+ for path in self._files.package_roots[package_name]
116
+ }
117
+
118
+
119
+ class _IndexedModuleFinder(AbstractModuleFinder):
120
+ """Give Grimp the same modules and namespace packages as Smelt's file index."""
121
+
122
+ def __init__(self, files: FileIndex) -> None:
123
+ self._files = files
124
+
125
+ def find_package(
126
+ self,
127
+ package_name: str,
128
+ package_directory: str,
129
+ file_system: AbstractFileSystem,
130
+ ) -> FoundPackage:
131
+ directory = Path(package_directory)
132
+ module_files = frozenset(
133
+ ModuleFile(
134
+ Module(source.module), file_system.get_mtime(str(source.absolute))
135
+ )
136
+ for source in self._files.sources.values()
137
+ if is_within(source.module, package_name)
138
+ and source.absolute.is_relative_to(directory)
139
+ )
140
+ namespace_packages = frozenset(
141
+ package.module
142
+ for portions in self._files.package_parts.values()
143
+ for package in portions
144
+ if is_within(package.module, package_name)
145
+ and not package.has_init
146
+ and (self._files.root / package.path).is_relative_to(directory)
147
+ )
148
+ return FoundPackage(
149
+ name=package_name,
150
+ directory=package_directory,
151
+ module_files=module_files,
152
+ namespace_packages=namespace_packages,
153
+ )
154
+
155
+
156
+ class ImportIndex:
157
+ def __init__(
158
+ self,
159
+ graph: grimp.ImportGraph,
160
+ files: FileIndex,
161
+ asts: AstCache,
162
+ *,
163
+ cache: FactCache | None = None,
164
+ inventory: str = "",
165
+ ) -> None:
166
+ self.graph = graph
167
+ self.files = files
168
+ self._asts = asts
169
+ self._cache = cache
170
+ self._inventory = inventory
171
+ roots = files.config.project.root_packages
172
+ self.first_party = frozenset(
173
+ m for m in graph.modules if any(is_within(m, r) for r in roots)
174
+ )
175
+ self._outgoing: dict[str, list[ImportDetail]] = {}
176
+ self._chains: dict[tuple[str, bool], tuple[tuple[ImportDetail, ...], ...]] = {}
177
+ self._chain_hops = 0
178
+
179
+ @classmethod
180
+ def build(
181
+ cls, files: FileIndex, asts: AstCache, *, cache: FactCache | None = None
182
+ ) -> ImportIndex:
183
+ project = files.config.project
184
+ packages = [
185
+ p for p in project.root_packages if p not in files.missing_root_packages
186
+ ]
187
+ if not packages:
188
+ msg = "none of project.root_packages were found under project.source_roots"
189
+ raise AnalysisError(msg)
190
+ inventory = resolution_identity(files)
191
+ previous_package_finder = grimp_settings.PACKAGE_FINDER
192
+ previous_module_finder = grimp_settings.MODULE_FINDER
193
+ previous_file_system = grimp_settings.FILE_SYSTEM
194
+ previous_cache_class = grimp_settings.CACHE_CLASS
195
+ grimp_settings.configure(
196
+ PACKAGE_FINDER=_SourceRootPackageFinder(files),
197
+ MODULE_FINDER=_IndexedModuleFinder(files),
198
+ FILE_SYSTEM=_SourceFileSystem(files),
199
+ CACHE_CLASS=import_cache_type(files, asts, cache, inventory),
200
+ )
201
+ try:
202
+ graph = grimp.build_graph(
203
+ packages[0],
204
+ *packages[1:],
205
+ include_external_packages=True,
206
+ cache_dir="smelt",
207
+ )
208
+ except SourceSyntaxError as exc:
209
+ raise AnalysisError(
210
+ str(exc) + python_note(files.root, store=files.store)
211
+ ) from exc
212
+ finally:
213
+ grimp_settings.configure(
214
+ PACKAGE_FINDER=previous_package_finder,
215
+ MODULE_FINDER=previous_module_finder,
216
+ FILE_SYSTEM=previous_file_system,
217
+ CACHE_CLASS=previous_cache_class,
218
+ )
219
+ for module in list(graph.modules):
220
+ if (
221
+ any(is_within(module, p) for p in packages)
222
+ and module not in files.sources
223
+ and module not in files.packages
224
+ ):
225
+ graph.remove_module(module)
226
+ missing = files.sources.keys() - graph.modules
227
+ if missing:
228
+ examples = ", ".join(sorted(missing)[:3])
229
+ raise AnalysisError(
230
+ f"import graph omitted {len(missing)} source module(s): {examples}"
231
+ )
232
+ return cls(graph, files, asts, cache=cache, inventory=inventory)
233
+
234
+ def modules(self) -> list[str]:
235
+ return sorted(m for m in self.first_party if m in self.files.sources)
236
+
237
+ def imports_of(self, module: str) -> list[ImportDetail]:
238
+ cached = self._outgoing.get(module)
239
+ if cached is None:
240
+ source = self.files.sources.get(module)
241
+ cached = (
242
+ self._details_for(module)
243
+ if self._cache is None or source is None
244
+ else self._cache.get(
245
+ "import-details",
246
+ [
247
+ module,
248
+ source.path,
249
+ source.is_package,
250
+ self.files.content_id(source.path),
251
+ self._inventory,
252
+ ],
253
+ _DETAILS,
254
+ lambda: self._details_for(module),
255
+ )
256
+ )
257
+ self._outgoing[module] = cached
258
+ return cached
259
+
260
+ def all_imports(self) -> Iterator[ImportDetail]:
261
+ for module in self.modules():
262
+ yield from self.imports_of(module)
263
+
264
+ def first_party_edges(
265
+ self, *, include_type_checking: bool = True
266
+ ) -> dict[str, set[str]]:
267
+ adjacency: dict[str, set[str]] = defaultdict(set)
268
+ for detail in self.all_imports():
269
+ if detail.external or (detail.type_checking and not include_type_checking):
270
+ continue
271
+ adjacency[detail.importer].add(detail.imported)
272
+ return adjacency
273
+
274
+ def shortest_chains_from(
275
+ self, module: str, *, include_type_checking: bool = True
276
+ ) -> Iterator[tuple[ImportDetail, ...]]:
277
+ """Share deterministic witnesses between rules within this graph view."""
278
+ key = module, include_type_checking
279
+ if key in self._chains:
280
+ yield from self._chains[key]
281
+ return
282
+ paths: list[tuple[ImportDetail, ...]] = []
283
+ hops = 0
284
+ for path in self._shortest_chains_from(
285
+ module, include_type_checking=include_type_checking
286
+ ):
287
+ hops += len(path)
288
+ if hops + self._chain_hops <= _CHAIN_CACHE_HOPS:
289
+ paths.append(path)
290
+ else:
291
+ paths.clear()
292
+ yield path
293
+ if hops + self._chain_hops <= _CHAIN_CACHE_HOPS:
294
+ self._chains[key] = tuple(paths)
295
+ self._chain_hops += hops
296
+
297
+ def _shortest_chains_from(
298
+ self, module: str, *, include_type_checking: bool
299
+ ) -> Iterator[tuple[ImportDetail, ...]]:
300
+ """Shortest import paths, including external leaves, without revisiting cycles.
301
+
302
+ Keep every external statement: Grimp squashes external submodules, but
303
+ policies may allow ``sqlalchemy.orm`` while denying ``sqlalchemy.sql``.
304
+ Traverse modules in lexical order and prefer the earliest active import
305
+ for a deterministic witness. No third-party source is inspected.
306
+ """
307
+ previous: dict[str, ImportDetail] = {}
308
+ visited = {module}
309
+ queue = deque([module])
310
+ while queue:
311
+ current = queue.popleft()
312
+ for detail in sorted(
313
+ self.imports_of(current),
314
+ key=lambda d: (d.imported, d.line, d.column),
315
+ ):
316
+ if detail.type_checking and not include_type_checking:
317
+ continue
318
+ if not detail.external:
319
+ if detail.imported in visited:
320
+ continue
321
+ visited.add(detail.imported)
322
+ previous[detail.imported] = detail
323
+ queue.append(detail.imported)
324
+ path = [detail]
325
+ while path[-1].importer != module:
326
+ path.append(previous[path[-1].importer])
327
+ yield tuple(reversed(path))
328
+
329
+ def _details_for(self, module: str) -> list[ImportDetail]:
330
+ source = self.files.sources.get(module)
331
+ if source is None or module not in self.first_party:
332
+ return []
333
+ tree = self._asts.parse(source.path)
334
+ statements = _statements_by_line(tree)
335
+ guarded = type_checking_lines(tree)
336
+ details: list[ImportDetail] = []
337
+ for imported in sorted(self.graph.find_modules_directly_imported_by(module)):
338
+ external = imported not in self.first_party
339
+ raw_lines = {
340
+ int(raw["line_number"]): str(raw["line_contents"])
341
+ for raw in self.graph.get_import_details(
342
+ importer=module, imported=imported
343
+ )
344
+ }
345
+ for line, contents in sorted(raw_lines.items()):
346
+ nodes: list[ast.Import | ast.ImportFrom | None] = [
347
+ node
348
+ for node in statements.get(line, [])
349
+ if _matches_statement(node, module, source.is_package, imported)
350
+ ]
351
+ for node in nodes or [None]:
352
+ names, column, end_column = _describe(
353
+ node, module, source.is_package, imported, contents
354
+ )
355
+ details.append(
356
+ ImportDetail(
357
+ importer=module,
358
+ imported=imported,
359
+ line=line,
360
+ line_contents=contents,
361
+ names=names,
362
+ type_checking=line in guarded,
363
+ external=external,
364
+ column=column,
365
+ end_column=end_column,
366
+ )
367
+ )
368
+ details.sort(key=lambda d: (d.line, d.column, d.imported))
369
+ return details
370
+
371
+
372
+ def _statements_by_line(
373
+ tree: ast.Module,
374
+ ) -> dict[int, list[ast.Import | ast.ImportFrom]]:
375
+ statements: dict[int, list[ast.Import | ast.ImportFrom]] = defaultdict(list)
376
+ for node in ast.walk(tree):
377
+ if isinstance(node, ast.Import | ast.ImportFrom):
378
+ statements[node.lineno].append(node)
379
+ return statements
380
+
381
+
382
+ def _matches_statement(
383
+ node: ast.Import | ast.ImportFrom, module: str, is_package: bool, imported: str
384
+ ) -> bool:
385
+ if isinstance(node, ast.Import):
386
+ names = [alias.name for alias in node.names]
387
+ else:
388
+ base = resolve_relative(
389
+ module, is_package=is_package, level=node.level, target=node.module
390
+ )
391
+ names = [
392
+ base if alias.name == "*" else f"{base}.{alias.name}"
393
+ for alias in node.names
394
+ ]
395
+ return any(is_within(name, imported) or is_within(imported, name) for name in names)
396
+
397
+
398
+ def _describe(
399
+ node: ast.Import | ast.ImportFrom | None,
400
+ module: str,
401
+ is_package: bool,
402
+ imported: str,
403
+ line_contents: str,
404
+ ) -> tuple[tuple[str, ...], int, int]:
405
+ """Imported names and the 1-based column span to underline on the first line."""
406
+ if node is None:
407
+ return (imported,), 1, len(line_contents) + 1
408
+ if isinstance(node, ast.Import):
409
+ return _describe_import(node, imported, line_contents)
410
+ return _describe_import_from(node, module, is_package, imported)
411
+
412
+
413
+ def _describe_import(
414
+ node: ast.Import, imported: str, line_contents: str
415
+ ) -> tuple[tuple[str, ...], int, int]:
416
+ first_line_end = (
417
+ node.end_col_offset if node.end_lineno == node.lineno else len(line_contents)
418
+ )
419
+ span = (node.col_offset + 1, (first_line_end or 0) + 1)
420
+ matching = [
421
+ alias
422
+ for alias in node.names
423
+ if is_within(alias.name, imported) or is_within(imported, alias.name)
424
+ ]
425
+ names = tuple(alias.name for alias in matching) or (imported,)
426
+ if len(matching) == 1 and matching[0].lineno == node.lineno:
427
+ alias = matching[0]
428
+ span = (alias.col_offset + 1, (alias.end_col_offset or 0) + 1)
429
+ return names, *span
430
+
431
+
432
+ def _describe_import_from(
433
+ node: ast.ImportFrom, module: str, is_package: bool, imported: str
434
+ ) -> tuple[tuple[str, ...], int, int]:
435
+ base = resolve_relative(
436
+ module, is_package=is_package, level=node.level, target=node.module
437
+ )
438
+ names = tuple(
439
+ f"{base}.{alias.name}" if base else alias.name for alias in node.names
440
+ )
441
+ for alias, name in zip(node.names, names, strict=True):
442
+ if name == imported and alias.lineno == node.lineno:
443
+ return (name,), alias.col_offset + 1, (alias.end_col_offset or 0) + 1
444
+ matching = tuple(
445
+ n for n in names if is_within(n, imported) or is_within(imported, n)
446
+ )
447
+ if len(node.names) == 1 and node.names[0].lineno == node.lineno:
448
+ alias = node.names[0]
449
+ return matching or names, alias.col_offset + 1, (alias.end_col_offset or 0) + 1
450
+ start = node.col_offset + len("from ")
451
+ text = "." * node.level + (node.module or "")
452
+ return matching or names, start + 1, start + len(text) + 1
@@ -0,0 +1,17 @@
1
+ """Literal shape evidence without loading the test analyzer."""
2
+
3
+ import ast
4
+
5
+
6
+ def nonempty_literal(node: ast.AST) -> bool:
7
+ if isinstance(node, ast.List | ast.Tuple | ast.Set):
8
+ return bool(node.elts) and not any(
9
+ isinstance(item, ast.Starred) for item in node.elts
10
+ )
11
+ if isinstance(node, ast.Dict):
12
+ return bool(node.keys) and None not in node.keys
13
+ return (
14
+ isinstance(node, ast.Constant)
15
+ and isinstance(node.value, str | bytes)
16
+ and bool(node.value)
17
+ )
@@ -0,0 +1,108 @@
1
+ """A disposable dependency index for member decisions across CLI processes."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from dataclasses import dataclass, field
6
+ from typing import TYPE_CHECKING
7
+
8
+ from pydantic import TypeAdapter
9
+
10
+ if TYPE_CHECKING:
11
+ from collections.abc import Callable
12
+
13
+ type NamespaceIdentity = tuple[str, str, bool] | None
14
+
15
+
16
+ @dataclass
17
+ class MemberSummary:
18
+ identity: str
19
+ namespaces: dict[str, NamespaceIdentity]
20
+ contributions: dict[str, int]
21
+ queries: list[str]
22
+ has_members: bool
23
+ decision: str = ""
24
+
25
+
26
+ @dataclass
27
+ class MemberIndexState:
28
+ files: dict[str, MemberSummary] = field(default_factory=dict)
29
+ namespaces: dict[str, NamespaceIdentity] = field(default_factory=dict)
30
+ totals: dict[str, int] = field(default_factory=dict)
31
+ readers: dict[str, set[str]] = field(default_factory=dict)
32
+ resolvers: dict[str, set[str]] = field(default_factory=dict)
33
+
34
+
35
+ MEMBER_STATE = TypeAdapter(MemberIndexState)
36
+
37
+
38
+ def update_members(
39
+ state: MemberIndexState,
40
+ identities: dict[str, str],
41
+ namespaces: dict[str, NamespaceIdentity],
42
+ build: Callable[[str], MemberSummary],
43
+ decision: Callable[[MemberSummary, dict[str, int]], str],
44
+ ) -> MemberIndexState:
45
+ """Replace changed contributions and invalidate their reverse dependencies.
46
+
47
+ A state is one atomically stored value, never a shared mutable database. A
48
+ concurrent writer can replace it with another snapshot; the next invocation
49
+ compares the entire input manifest before reusing any decision.
50
+ """
51
+ changed = {
52
+ path
53
+ for path in state.files.keys() | identities.keys()
54
+ if path not in state.files or state.files[path].identity != identities.get(path)
55
+ }
56
+ for name in state.namespaces.keys() | namespaces.keys():
57
+ if state.namespaces.get(name) != namespaces.get(name):
58
+ changed.update(state.resolvers.get(name, ()))
59
+
60
+ affected = set(changed)
61
+ differences: dict[str, int] = {}
62
+ for path in sorted(changed):
63
+ _remove_file(state, path, differences)
64
+ if path not in identities:
65
+ continue
66
+ current = build(path)
67
+ state.files[path] = current
68
+ for name in current.namespaces:
69
+ state.resolvers.setdefault(name, set()).add(path)
70
+ for query in current.queries:
71
+ state.readers.setdefault(query, set()).add(path)
72
+ for query, count in current.contributions.items():
73
+ differences[query] = differences.get(query, 0) + count
74
+
75
+ for query, delta in differences.items():
76
+ if delta:
77
+ count = state.totals.get(query, 0) + delta
78
+ if count:
79
+ state.totals[query] = count
80
+ else:
81
+ state.totals.pop(query, None)
82
+ affected.update(state.readers.get(query, ()))
83
+ for path in sorted(affected & state.files.keys()):
84
+ summary = state.files[path]
85
+ summary.decision = decision(summary, state.totals)
86
+ state.namespaces = namespaces
87
+ return state
88
+
89
+
90
+ def _remove_file(
91
+ state: MemberIndexState, path: str, differences: dict[str, int]
92
+ ) -> None:
93
+ previous = state.files.pop(path, None)
94
+ if previous is None:
95
+ return
96
+ for name in previous.namespaces:
97
+ _remove_reader(state.resolvers, name, path)
98
+ for query in previous.queries:
99
+ _remove_reader(state.readers, query, path)
100
+ for query, count in previous.contributions.items():
101
+ differences[query] = differences.get(query, 0) - count
102
+
103
+
104
+ def _remove_reader(readers: dict[str, set[str]], query: str, path: str) -> None:
105
+ paths = readers[query]
106
+ paths.discard(path)
107
+ if not paths:
108
+ del readers[query]