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
smelt/engine/check.py ADDED
@@ -0,0 +1,724 @@
1
+ from __future__ import annotations
2
+
3
+ import tempfile
4
+ from dataclasses import dataclass, field, replace
5
+ from pathlib import Path
6
+ from typing import TYPE_CHECKING
7
+
8
+ from pydantic import TypeAdapter
9
+
10
+ from smelt.analysis.cache import FactCache
11
+ from smelt.analysis.context import AnalysisContext
12
+ from smelt.analysis.errors import (
13
+ InvalidOptionError as InvalidOptionError, # noqa: PLC0414 - preserve the public engine exception import
14
+ )
15
+ from smelt.analysis.files import FileIndex
16
+ from smelt.config.errors import ConfigError, ConfigIssue, did_you_mean
17
+ from smelt.config.patterns import module_matches, path_matches
18
+ from smelt.diagnostics.debt import Debt, DebtEntry
19
+ from smelt.diagnostics.dedupe import deduplicate
20
+ from smelt.diagnostics.report import ConfigurationDiagnostic, Report, RuleMeta
21
+ from smelt.diagnostics.suppressions import (
22
+ Suppression,
23
+ parse_suppressions,
24
+ )
25
+ from smelt.diagnostics.violation import (
26
+ Category,
27
+ Severity,
28
+ Violation,
29
+ docs_url,
30
+ )
31
+ from smelt.engine.cache import ANALYSIS, CachedAnalysis, analysis_inputs
32
+ from smelt.engine.changes import Comparison, export_base, resolve_comparison
33
+ from smelt.engine.coverage import effective_coverage
34
+ from smelt.engine.file_checks import rule_findings
35
+ from smelt.engine.git_source import GitSourceStore
36
+ from smelt.engine.paths import missing_paths, project_metadata
37
+ from smelt.engine.rule_settings import RuleSettings
38
+ from smelt.rules.meta import ResolvedDebt, SuppressionWithoutReason, UnusedSuppression
39
+ from smelt.rules.registry import build_rule_set
40
+
41
+ if TYPE_CHECKING:
42
+ from collections.abc import Callable
43
+
44
+ from smelt.config.loader import LoadedConfig
45
+ from smelt.config.source import SourceStore
46
+ from smelt.rules.base import Rule, RuleSet
47
+
48
+ _META = frozenset({"SMT901", "SMT902", "SMT903"})
49
+ _COVERAGE = "SMT306"
50
+ _BASELINE = TypeAdapter(list[DebtEntry])
51
+ _SUPPRESSIONS = TypeAdapter(list[Suppression])
52
+
53
+
54
+ _CATEGORY_ORDER = [
55
+ Category.DEPENDENCIES,
56
+ Category.STRUCTURE,
57
+ Category.TESTS,
58
+ ]
59
+
60
+
61
+ @dataclass(frozen=True)
62
+ class CheckOptions:
63
+ paths: tuple[str, ...] = () # project-relative POSIX paths
64
+ changed: bool = False
65
+ base: str | None = None
66
+ select: tuple[str, ...] = ()
67
+ ignore: tuple[str, ...] = ()
68
+ fail_on: Severity = Severity.ERROR
69
+ use_debt: bool = True
70
+ strict_coverage: bool = False
71
+ show_coverage: bool = False
72
+ use_cache: bool = True
73
+ cache_dir: Path | None = None
74
+ comparison_root: Path | None = None
75
+ staged: bool = False
76
+ snapshot_id: str | None = None
77
+ repository_root: Path | None = None
78
+ comparison: Comparison | None = None
79
+
80
+
81
+ @dataclass
82
+ class CheckOutcome:
83
+ report: Report
84
+ context: AnalysisContext
85
+ rules: RuleSet
86
+ active: list[tuple[Rule, Severity]]
87
+ # every violation after ignores and suppressions, before debt and scoping
88
+ unfiltered: list[Violation] = field(default_factory=list)
89
+ resolved_debt: list[DebtEntry] = field(default_factory=list)
90
+
91
+
92
+ def rule_meta(rule: Rule) -> RuleMeta:
93
+ default = bool(getattr(rule, "enabled_by_default", False))
94
+ activation = getattr(rule, "activation", None)
95
+ return RuleMeta(
96
+ code=rule.code,
97
+ name=rule.name,
98
+ category=rule.category,
99
+ default_severity=rule.default_severity,
100
+ enabled_by_default=default,
101
+ summary=rule.explain().summary,
102
+ docs_url=docs_url(rule.code),
103
+ requires=tuple(sorted(index.value for index in rule.requires)),
104
+ config=rule.explain().config,
105
+ activation={
106
+ "mode": "default" if default else "policy" if activation else "explicit",
107
+ "description": "Enabled by default."
108
+ if default
109
+ else activation.description
110
+ if activation
111
+ else "Enable by rule setting or --select.",
112
+ },
113
+ scope=getattr(rule, "scope", "source"),
114
+ )
115
+
116
+
117
+ def run_check(
118
+ loaded: LoadedConfig,
119
+ options: CheckOptions,
120
+ *,
121
+ rules: RuleSet | None = None,
122
+ context: AnalysisContext | None = None,
123
+ ) -> CheckOutcome:
124
+ cache = (
125
+ FactCache(options.cache_dir or loaded.root / ".smelt_cache")
126
+ if options.use_cache and rules is None and context is None
127
+ else None
128
+ )
129
+ try:
130
+ return _run_check(loaded, options, rules=rules, context=context, cache=cache)
131
+ finally:
132
+ if cache is not None:
133
+ cache.close()
134
+
135
+
136
+ def snippet_reader(ctx: AnalysisContext) -> Callable[[Violation], str]:
137
+ # A cached snapshot needs source lines, not source discovery, to compare
138
+ # fingerprints. A lightweight FileIndex provides the same decoding/cache.
139
+ files = ctx.__dict__.get("files") or FileIndex(
140
+ ctx.root, ctx.config, store=ctx.store
141
+ )
142
+
143
+ def snippet(violation: Violation) -> str:
144
+ if not violation.path or not violation.line:
145
+ return ""
146
+ try:
147
+ return files.line(violation.path, violation.line)
148
+ except OSError:
149
+ return ""
150
+
151
+ return snippet
152
+
153
+
154
+ def relative_scope(
155
+ root: Path, cwd: Path, raw: str, *, store: SourceStore | None = None
156
+ ) -> str:
157
+ absolute = store.resolve(cwd / raw) if store else (cwd / raw).resolve()
158
+ try:
159
+ return absolute.relative_to(
160
+ store.resolve(root) if store else root.resolve()
161
+ ).as_posix()
162
+ except ValueError:
163
+ return Path(raw).as_posix()
164
+
165
+
166
+ def _settings_by_code(rules: RuleSet, loaded: LoadedConfig) -> dict[str, str]:
167
+ """Key ``rules:`` overrides by code; the config may name a rule by code or name."""
168
+ return RuleSettings(rules, loaded).global_rules
169
+
170
+
171
+ def _resolve_active_rules(
172
+ rules: RuleSet, loaded: LoadedConfig, options: CheckOptions
173
+ ) -> list[tuple[Rule, Severity]]:
174
+ policy = RuleSettings(rules, loaded)
175
+ settings = policy.global_rules
176
+ known = {rule.code for rule in rules.rules}
177
+ names = {rule.name: rule.code for rule in rules.rules}
178
+ normalized: dict[str, tuple[str, ...]] = {}
179
+ for key in ("select", "ignore"):
180
+ selectors: list[str] = []
181
+ for prefix in getattr(options, key):
182
+ selector = names.get(prefix.strip().lower(), prefix.strip().upper())
183
+ if selector != "ALL" and not any(
184
+ code.startswith(selector) for code in known
185
+ ):
186
+ msg = (
187
+ f'unknown rule prefix "{prefix}" for --{key}{_prefix_suggestion(prefix, known)}; '
188
+ "`smelt rules` lists valid codes"
189
+ )
190
+ raise InvalidOptionError(msg, option=f"--{key}", value=prefix)
191
+ selectors.append(selector)
192
+ normalized[key] = tuple(selectors)
193
+ options = replace(options, select=normalized["select"], ignore=normalized["ignore"])
194
+
195
+ active: list[tuple[Rule, Severity]] = []
196
+ for rule in rules.rules:
197
+ setting = settings.get(rule.code)
198
+ if rule.code == _COVERAGE and loaded.config.project.coverage == "strict":
199
+ _validate_strict_coverage(loaded, options, setting)
200
+ active.append((rule, Severity.ERROR))
201
+ continue
202
+ explicitly_selected = bool(options.select) and _matches_prefix(
203
+ rule.code, options.select
204
+ )
205
+ package_enabled = policy.has_package_activation(rule.code)
206
+ if setting == "off" and not package_enabled:
207
+ continue
208
+ if setting is not None and setting != "off":
209
+ severity = Severity(setting)
210
+ elif (
211
+ policy.globally_enabled(rule, selected=explicitly_selected)
212
+ or package_enabled
213
+ ):
214
+ severity = rule.default_severity
215
+ else:
216
+ continue
217
+ if options.select and not _matches_prefix(rule.code, options.select):
218
+ continue
219
+ if options.ignore and _matches_prefix(rule.code, options.ignore):
220
+ continue
221
+ active.append((rule, severity))
222
+ return active
223
+
224
+
225
+ def _validate_strict_coverage(
226
+ loaded: LoadedConfig, options: CheckOptions, setting: str | None
227
+ ) -> None:
228
+ if setting is not None and setting != "error":
229
+ raise ConfigError(
230
+ [
231
+ ConfigIssue(
232
+ f"rules.{_COVERAGE}",
233
+ "strict coverage must remain an error; use reasoned project.exclude entries instead",
234
+ )
235
+ ],
236
+ loaded.path.name,
237
+ )
238
+ if _matches_prefix(_COVERAGE, options.ignore):
239
+ raise InvalidOptionError(
240
+ "--ignore cannot disable strict coverage; use reasoned project.exclude entries instead",
241
+ option="--ignore",
242
+ value=",".join(options.ignore),
243
+ )
244
+
245
+
246
+ def _matches_prefix(code: str, prefixes: tuple[str, ...]) -> bool:
247
+ return any(
248
+ prefix.strip().upper() == "ALL" or code.startswith(prefix.strip().upper())
249
+ for prefix in prefixes
250
+ if prefix
251
+ )
252
+
253
+
254
+ def _prefix_suggestion(raw: str, known: set[str]) -> str:
255
+ wanted = raw.strip().upper()
256
+ candidates = {code[: len(wanted)] for code in known if len(code) >= len(wanted)}
257
+ close = [
258
+ code
259
+ for code in candidates
260
+ if sum(a != b for a, b in zip(code, wanted, strict=True)) == 1
261
+ ]
262
+ return f' (did you mean "{close[0]}"?)' if len(close) == 1 else ""
263
+
264
+
265
+ def _run_check(
266
+ loaded: LoadedConfig,
267
+ options: CheckOptions,
268
+ *,
269
+ rules: RuleSet | None,
270
+ context: AnalysisContext | None,
271
+ cache: FactCache | None,
272
+ ) -> CheckOutcome:
273
+ config = loaded.config
274
+ if options.strict_coverage and config.project.coverage != "strict":
275
+ config = config.model_copy(
276
+ update={"project": config.project.model_copy(update={"coverage": "strict"})}
277
+ )
278
+ loaded = replace(loaded, config=config)
279
+ rules = rules or build_rule_set(lazy=True)
280
+ active = _resolve_active_rules(rules, loaded, options)
281
+ ctx = (
282
+ context
283
+ if context is not None and context.config == config
284
+ else AnalysisContext(loaded.root, config, cache=cache, store=loaded.source)
285
+ )
286
+ if isinstance(ctx.store, GitSourceStore):
287
+ ctx.store.cache = cache
288
+ key = (
289
+ cache.key(
290
+ "index-analysis",
291
+ [
292
+ options.snapshot_id,
293
+ config.model_dump(mode="json"),
294
+ [(r.code, s.value) for r, s in active],
295
+ str(options.comparison_root),
296
+ options.select,
297
+ ],
298
+ )
299
+ if cache and options.snapshot_id
300
+ else None
301
+ )
302
+ analyzed = cache.read(key, ANALYSIS) if cache and key else None
303
+ if analyzed is None:
304
+ issues = missing_paths(ctx)
305
+ if issues:
306
+ raise ConfigError(issues, loaded.path.name)
307
+ RuleSettings(rules, loaded).validate_packages(ctx)
308
+ key = key or (
309
+ cache.key("analysis", analysis_inputs(ctx, active, options.select))
310
+ if cache
311
+ else None
312
+ )
313
+ analyzed = cache.read(key, ANALYSIS) if cache and key else None
314
+ if options.paths:
315
+ _validate_scope(ctx, options.paths)
316
+ if analyzed is None:
317
+ violations, suppressed = _collect(ctx, rules, loaded, active, options)
318
+ analyzed = CachedAnalysis(
319
+ violations,
320
+ suppressed,
321
+ effective_coverage(
322
+ ctx,
323
+ include_inventory=True,
324
+ include_ordering=any(rule.code == "SMT313" for rule, _ in active),
325
+ include_test_quality=any(
326
+ rule.code.startswith("SMT42") for rule, _ in active
327
+ ),
328
+ ),
329
+ len(ctx.files.sources),
330
+ list(ctx.files.all_python_paths()),
331
+ dict(ctx.store.dependencies()),
332
+ )
333
+ if cache and key:
334
+ cache.write(key, analyzed, ANALYSIS)
335
+ ctx.store.dependencies().update(analyzed.dependencies)
336
+ violations, suppressed = list(analyzed.violations), analyzed.suppressed
337
+ unfiltered = list(violations)
338
+ coverage_gaps = [v for v in violations if v.code == _COVERAGE]
339
+ violations = [v for v in violations if v.code != _COVERAGE]
340
+
341
+ in_debt = 0
342
+ resolved: list[DebtEntry] = []
343
+ severity_of = {rule.code: severity for rule, severity in active}
344
+ if options.use_debt and config.debt:
345
+ debt = Debt.load(loaded.root / config.debt, store=ctx.store)
346
+ # Inactive rules have not been checked; their debt is not resolved.
347
+ policy = RuleSettings(rules, loaded)
348
+ debt = Debt(
349
+ [
350
+ entry
351
+ for entry in debt.entries
352
+ if entry.code in severity_of
353
+ and (rule := rules.by_code(entry.code)) is not None
354
+ and policy.severity_for_path(
355
+ rule, ctx, entry.path, selected=bool(options.select)
356
+ )
357
+ is not None
358
+ ]
359
+ )
360
+ violations, known, resolved = debt.match(violations, snippet_reader(ctx))
361
+ in_debt = len(known)
362
+ full_run = not options.paths and not options.changed
363
+ if "SMT903" in severity_of and full_run:
364
+ violations.extend(
365
+ _resolved_violations(resolved, config.debt, severity_of["SMT903"])
366
+ )
367
+ comparison: Comparison | None = None
368
+ if options.changed:
369
+ violations, comparison = _introduced(
370
+ ctx,
371
+ violations,
372
+ lambda before: _collect(before, rules, loaded, active, options)[0],
373
+ options=options,
374
+ cache_key=[
375
+ config.model_dump(mode="json"),
376
+ [(r.code, s.value) for r, s in active],
377
+ options.select,
378
+ ],
379
+ )
380
+
381
+ violations = coverage_gaps + [v for v in violations if _in_scope(v, options)]
382
+ violations.sort(key=Violation.sort_key)
383
+ categories = {rule.category for rule, _ in active}
384
+ report = Report(
385
+ violations=violations,
386
+ modules=analyzed.modules,
387
+ categories=[c for c in _CATEGORY_ORDER if c in categories],
388
+ rules=[rule_meta(rule) for rule, _ in active],
389
+ fail_on=options.fail_on,
390
+ suppressed=suppressed,
391
+ in_debt=in_debt,
392
+ scope=options.paths,
393
+ checked_files=sum(
394
+ _under_any(path, options.paths) if options.paths else True
395
+ for path in analyzed.paths
396
+ ),
397
+ comparison=comparison.to_json() if comparison else None,
398
+ coverage={
399
+ k: v
400
+ for k, v in analyzed.coverage.items()
401
+ if k != "inventory" or options.show_coverage
402
+ },
403
+ project=project_metadata(loaded),
404
+ configuration_diagnostics=[
405
+ ConfigurationDiagnostic(loaded.path.name, issue.path, issue.message)
406
+ for issue in loaded.warnings
407
+ ],
408
+ )
409
+ return CheckOutcome(report, ctx, rules, active, unfiltered, resolved)
410
+
411
+
412
+ def _introduced(
413
+ ctx: AnalysisContext,
414
+ violations: list[Violation],
415
+ collect: Callable[[AnalysisContext], list[Violation]],
416
+ *,
417
+ options: CheckOptions,
418
+ cache_key: object = None,
419
+ ) -> tuple[list[Violation], Comparison]:
420
+ """The violations the working tree adds to the base commit, judged by today's config."""
421
+ root = options.comparison_root or ctx.root
422
+ if options.comparison is not None and options.repository_root is not None:
423
+ repository, comparison = options.repository_root, options.comparison
424
+ else:
425
+ repository, comparison = resolve_comparison(
426
+ root, options.base, repository=options.repository_root
427
+ )
428
+ if options.staged:
429
+ comparison = replace(comparison, staged=True)
430
+ cache = ctx.cache
431
+ # A source root outside the repository can change independently of HEAD.
432
+ if any(
433
+ not ctx.store.resolve(root / path).is_relative_to(repository.resolve())
434
+ for path in [*ctx.config.project.source_roots, *ctx.config.project.test_roots]
435
+ ):
436
+ cache = None
437
+ key = (
438
+ cache.key(
439
+ "baseline",
440
+ [root.relative_to(repository).as_posix(), comparison.revision, cache_key],
441
+ )
442
+ if cache
443
+ else None
444
+ )
445
+ entries = cache.read(key, _BASELINE) if cache and key else None
446
+ if entries is not None:
447
+ introduced, _, _ = Debt(entries).match(violations, snippet_reader(ctx))
448
+ return introduced, comparison
449
+ if isinstance(ctx.store, GitSourceStore):
450
+ if comparison.revision is None:
451
+ return violations, comparison
452
+ before_store = GitSourceStore.from_revision(
453
+ repository, comparison.revision, cache=ctx.cache
454
+ )
455
+ if not before_store.is_dir(root):
456
+ return violations, comparison
457
+ before_ctx = AnalysisContext(
458
+ root, ctx.config, cache=ctx.cache, store=before_store
459
+ )
460
+ before = collect(before_ctx) if before_ctx.files.sources else []
461
+ known = Debt.from_violations(before, snippet_reader(before_ctx))
462
+ if cache and key:
463
+ cache.write(key, known.entries, _BASELINE)
464
+ introduced, _, _ = known.match(violations, snippet_reader(ctx))
465
+ return introduced, comparison
466
+ with tempfile.TemporaryDirectory(prefix="smelt-base-") as tmp:
467
+ base_root = export_base(root, repository, comparison, Path(tmp))
468
+ if base_root is None:
469
+ return violations, comparison
470
+ before_ctx = AnalysisContext(base_root, ctx.config, cache=ctx.cache)
471
+ # A committed README (or a newly added workspace package) is an empty
472
+ # historical inventory, not an analysis failure. Existing source still
473
+ # goes through normal parsing so corrupt baselines do not pass silently.
474
+ before = collect(before_ctx) if before_ctx.files.sources else []
475
+ known = Debt.from_violations(before, snippet_reader(before_ctx))
476
+ if cache and key:
477
+ cache.write(key, known.entries, _BASELINE)
478
+ introduced, _, _ = known.match(violations, snippet_reader(ctx))
479
+ return introduced, comparison
480
+
481
+
482
+ def _collect(
483
+ ctx: AnalysisContext,
484
+ rules: RuleSet,
485
+ loaded: LoadedConfig,
486
+ active: list[tuple[Rule, Severity]],
487
+ options: CheckOptions,
488
+ ) -> tuple[list[Violation], int]:
489
+ """Every violation after ignores and suppressions, and the suppressed count."""
490
+ ctx.store.prefetch(ctx.root / path for path in ctx.files.all_python_paths())
491
+ requires = frozenset(index for rule, _ in active for index in rule.requires)
492
+ ctx.ensure(requires)
493
+
494
+ policy = RuleSettings(rules, loaded)
495
+ violations: list[Violation] = []
496
+ for rule, _ in active:
497
+ if rule.code in _META:
498
+ continue
499
+ for violation in rule_findings(rule, ctx):
500
+ severity = policy.severity(
501
+ rule, ctx, violation.source_module, selected=bool(options.select)
502
+ )
503
+ if severity is not None:
504
+ violations.append(violation.with_severity(severity))
505
+ violations = deduplicate(violations)
506
+ violations = _apply_ignores(ctx, violations, rules)
507
+
508
+ active_codes = {rule.code for rule, _ in active}
509
+ severity_of = {rule.code: severity for rule, severity in active}
510
+ suppressions = _collect_suppressions(ctx)
511
+ violations, suppressed = _apply_suppressions(violations, suppressions)
512
+ known_codes = {rule.code for rule in rules.rules}
513
+ # The rules a full run with this config would execute; the others are off and
514
+ # cannot need a suppression.
515
+ enabled = _resolve_active_rules(rules, loaded, CheckOptions())
516
+ enabled_codes = {rule.code for rule, _ in enabled} - _META
517
+ violations.extend(
518
+ _suppression_violations(
519
+ ctx, suppressions, active_codes, (known_codes, enabled_codes), severity_of
520
+ )
521
+ )
522
+ return violations, suppressed
523
+
524
+
525
+ def _apply_ignores(
526
+ ctx: AnalysisContext, violations: list[Violation], rules: RuleSet
527
+ ) -> list[Violation]:
528
+ entries = ctx.config.ignore
529
+ if not entries:
530
+ return violations
531
+ names = {rule.name: rule.code for rule in rules.rules}
532
+ kept: list[Violation] = []
533
+ for violation in violations:
534
+ if violation.code == _COVERAGE:
535
+ kept.append(violation)
536
+ continue
537
+ module = violation.source_module or _module_for_path(ctx, violation.path)
538
+ ignored = any(
539
+ (
540
+ entry.rule == "ALL"
541
+ or violation.code.startswith(names.get(entry.rule, entry.rule))
542
+ )
543
+ and (
544
+ (module and any(module_matches(p, module) for p in entry.modules))
545
+ or (
546
+ violation.path
547
+ and any(path_matches(p, violation.path) for p in entry.paths)
548
+ )
549
+ )
550
+ for entry in entries
551
+ )
552
+ if not ignored:
553
+ kept.append(violation)
554
+ return kept
555
+
556
+
557
+ def _module_for_path(ctx: AnalysisContext, path: str | None) -> str | None:
558
+ if path is None:
559
+ return None
560
+ source = ctx.files.source_for_path(path)
561
+ return source.module if source else None
562
+
563
+
564
+ def _collect_suppressions(ctx: AnalysisContext) -> list[Suppression]:
565
+ found: list[Suppression] = []
566
+ keys = (
567
+ {
568
+ path: ctx.cache.key("suppressions", [path, ctx.files.content_id(path)])
569
+ for path in ctx.files.all_python_paths()
570
+ }
571
+ if ctx.cache
572
+ else {}
573
+ )
574
+ missing = ctx.cache.missing(list(keys.values())) if ctx.cache else set()
575
+ ctx.store.prefetch(
576
+ ctx.root / path
577
+ for path in ctx.files.all_python_paths()
578
+ if ctx.cache is None or keys[path] in missing
579
+ )
580
+ for path in ctx.files.all_python_paths():
581
+ cached = ctx.cache.read(keys[path], _SUPPRESSIONS) if ctx.cache else None
582
+ if cached is None:
583
+ text = ctx.files.read_text(path)
584
+ cached = parse_suppressions(path, text) if "smelt:" in text else []
585
+ if ctx.cache:
586
+ ctx.cache.write(keys[path], cached, _SUPPRESSIONS)
587
+ found.extend(cached)
588
+ return found
589
+
590
+
591
+ def _apply_suppressions(
592
+ violations: list[Violation], suppressions: list[Suppression]
593
+ ) -> tuple[list[Violation], int]:
594
+ by_path: dict[str, list[Suppression]] = {}
595
+ for suppression in suppressions:
596
+ by_path.setdefault(suppression.path, []).append(suppression)
597
+ kept: list[Violation] = []
598
+ suppressed = 0
599
+ for violation in violations:
600
+ if violation.code == _COVERAGE:
601
+ kept.append(violation)
602
+ continue
603
+ match = next(
604
+ (s for s in by_path.get(violation.path or "", []) if s.matches(violation)),
605
+ None,
606
+ )
607
+ if match is None:
608
+ kept.append(violation)
609
+ else:
610
+ match.mark_used(violation.code)
611
+ suppressed += 1
612
+ return kept, suppressed
613
+
614
+
615
+ def _suppression_violations(
616
+ ctx: AnalysisContext,
617
+ suppressions: list[Suppression],
618
+ active_codes: set[str],
619
+ codes: tuple[set[str], set[str]],
620
+ severity_of: dict[str, Severity],
621
+ ) -> list[Violation]:
622
+ results: list[Violation] = []
623
+ unused_rule = UnusedSuppression()
624
+ reason_rule = SuppressionWithoutReason()
625
+ checked = active_codes - _META
626
+ for suppression in suppressions:
627
+ if (
628
+ "SMT902" in active_codes
629
+ and ctx.config.suppressions.require_reason
630
+ and not suppression.reason
631
+ ):
632
+ results.append(
633
+ reason_rule.violation(
634
+ "suppression without a reason",
635
+ path=suppression.path,
636
+ line=suppression.line,
637
+ column=suppression.column + 1,
638
+ hint="Append ` -- <why this exception is acceptable>` to the comment.",
639
+ ).with_severity(severity_of["SMT902"])
640
+ )
641
+ if "SMT901" not in active_codes:
642
+ continue
643
+ unused = _unused_codes(suppression, checked, *codes)
644
+ if unused is None:
645
+ continue
646
+ label = ", ".join(unused) if unused else "all codes"
647
+ results.append(
648
+ unused_rule.violation(
649
+ f"unused suppression ({label})",
650
+ path=suppression.path,
651
+ line=suppression.line,
652
+ column=suppression.column + 1,
653
+ hint="Remove the suppression; nothing on this line needs it.",
654
+ ).with_severity(severity_of["SMT901"])
655
+ )
656
+ return results
657
+
658
+
659
+ def _unused_codes(
660
+ suppression: Suppression, checked: set[str], known: set[str], enabled: set[str]
661
+ ) -> tuple[str, ...] | None:
662
+ """Codes to remove, ``()`` for the whole comment, None if the suppression is fine.
663
+
664
+ A code only counts as unused when every enabled rule it refers to actually ran;
665
+ a narrowed run (``--select``, ``--ignore``) cannot judge the others.
666
+ """
667
+ if not suppression.codes:
668
+ return None if suppression.is_used or not enabled <= checked else ()
669
+ evaluated = tuple(
670
+ prefix
671
+ for prefix in suppression.unused_codes()
672
+ if (matching := {code for code in known if code.startswith(prefix)})
673
+ and matching & enabled <= checked
674
+ )
675
+ if not evaluated:
676
+ return None
677
+ if len(evaluated) == len(suppression.codes):
678
+ return ()
679
+ return evaluated
680
+
681
+
682
+ def _resolved_violations(
683
+ resolved: list[DebtEntry], debt_path: str, severity: Severity
684
+ ) -> list[Violation]:
685
+ rule = ResolvedDebt()
686
+ return [
687
+ rule.violation(
688
+ f"resolved debt entry: {entry.code} {entry.path or ''} ({entry.message})",
689
+ path=debt_path,
690
+ hint="Run `smelt debt --prune` to remove resolved entries.",
691
+ ).with_severity(severity)
692
+ for entry in resolved
693
+ ]
694
+
695
+
696
+ def _in_scope(violation: Violation, options: CheckOptions) -> bool:
697
+ return not options.paths or _under_any(violation.path, options.paths)
698
+
699
+
700
+ def _validate_scope(ctx: AnalysisContext, paths: tuple[str, ...]) -> None:
701
+ known = list(ctx.files.all_python_paths())
702
+ candidates = [*known, *(p.path for p in ctx.files.packages.values())]
703
+ for path in paths:
704
+ if not ctx.store.exists(ctx.root / path):
705
+ msg = f'check path "{path}" does not exist{did_you_mean(path, candidates)}'
706
+ raise InvalidOptionError(msg, option="paths", value=path)
707
+ if not any(_under_any(p, (path,)) for p in known):
708
+ msg = (
709
+ f'check path "{path}" contains no analyzed Python source or test files; '
710
+ "check project.root_packages, source_roots, test_roots and exclude"
711
+ )
712
+ raise InvalidOptionError(msg, option="paths", value=path)
713
+
714
+
715
+ def _under_any(path: str | None, scopes: tuple[str, ...]) -> bool:
716
+ if path is None:
717
+ return False
718
+ for scope in scopes:
719
+ normalized = scope.strip("/")
720
+ if normalized in ("", "."):
721
+ return True
722
+ if path == normalized or path.startswith(f"{normalized}/"):
723
+ return True
724
+ return False