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,47 @@
1
+ import re
2
+ from functools import cache
3
+
4
+
5
+ @cache
6
+ def _path_regex(pattern: str) -> re.Pattern[str]:
7
+ parts: list[str] = []
8
+ i = 0
9
+ while i < len(pattern):
10
+ if pattern.startswith("**/", i):
11
+ parts.append("(?:.*/)?")
12
+ i += 3
13
+ elif pattern.startswith("**", i):
14
+ parts.append(".*")
15
+ i += 2
16
+ elif pattern[i] == "*":
17
+ parts.append("[^/]*")
18
+ i += 1
19
+ elif pattern[i] == "?":
20
+ parts.append("[^/]")
21
+ i += 1
22
+ else:
23
+ parts.append(re.escape(pattern[i]))
24
+ i += 1
25
+ return re.compile("".join(parts) + "(?:/.*)?")
26
+
27
+
28
+ def path_matches(pattern: str, path: str) -> bool:
29
+ """Match a POSIX path against a glob; a match on a directory covers its contents."""
30
+ return _path_regex(pattern.strip("/")).fullmatch(path) is not None
31
+
32
+
33
+ @cache
34
+ def _module_regex(pattern: str) -> re.Pattern[str]:
35
+ parts: list[str] = []
36
+ for index, segment in enumerate(pattern.split(".")):
37
+ if segment == "**":
38
+ parts.append(r"(?:\.[^.]+)*" if index else r"[^.]+(?:\.[^.]+)*")
39
+ continue
40
+ piece = re.escape(segment).replace(r"\*", "[^.]*")
41
+ parts.append(rf"\.{piece}" if index else piece)
42
+ return re.compile("".join(parts))
43
+
44
+
45
+ def module_matches(pattern: str, module: str) -> bool:
46
+ """Match a dotted module name; ``*`` is one segment, ``**`` any number (including none)."""
47
+ return _module_regex(pattern).fullmatch(module) is not None
smelt/config/schema.py ADDED
@@ -0,0 +1,23 @@
1
+ from typing import Any
2
+
3
+ from smelt.config.models import SmeltConfig
4
+
5
+ SCHEMA_ID = (
6
+ "https://raw.githubusercontent.com/mathisarends/smelt/main/smelt.schema.json"
7
+ )
8
+
9
+
10
+ def config_json_schema() -> dict[str, Any]:
11
+ schema = SmeltConfig.model_json_schema()
12
+ # Allow the short forms `third_party: allow | deny`.
13
+ policy = schema.get("$defs", {}).get("ThirdPartyPolicy")
14
+ if policy is not None:
15
+ schema["$defs"]["ThirdPartyPolicy"] = {
16
+ "anyOf": [{"enum": ["allow", "deny"], "type": "string"}, policy],
17
+ }
18
+ return {
19
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
20
+ "$id": SCHEMA_ID,
21
+ **schema,
22
+ "title": "smelt.yaml",
23
+ }
smelt/config/source.py ADDED
@@ -0,0 +1,56 @@
1
+ """Read-only project storage shared by config discovery and analysis."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import TYPE_CHECKING
6
+
7
+ if TYPE_CHECKING:
8
+ from collections.abc import Iterable, Iterator
9
+ from pathlib import Path
10
+
11
+
12
+ class SourceStore:
13
+ """Disk implementation; immutable Git views implement the same operations."""
14
+
15
+ def resolve(self, path: Path) -> Path:
16
+ return path.resolve()
17
+
18
+ def is_file(self, path: Path) -> bool:
19
+ return path.is_file()
20
+
21
+ def is_dir(self, path: Path) -> bool:
22
+ return path.is_dir()
23
+
24
+ def exists(self, path: Path) -> bool:
25
+ return path.exists()
26
+
27
+ def iterdir(self, path: Path) -> Iterator[Path]:
28
+ yield from path.iterdir()
29
+
30
+ def glob(self, path: Path, pattern: str) -> Iterator[Path]:
31
+ yield from path.glob(pattern)
32
+
33
+ def read_bytes(self, path: Path) -> bytes:
34
+ return path.read_bytes()
35
+
36
+ def read_text(self, path: Path, *, encoding: str = "utf-8") -> str:
37
+ # Match Path.read_text's universal-newline behavior for Git blobs too.
38
+ return (
39
+ self.read_bytes(path)
40
+ .decode(encoding)
41
+ .replace("\r\n", "\n")
42
+ .replace("\r", "\n")
43
+ )
44
+
45
+ def identity(self, path: Path) -> str | None:
46
+ return None
47
+
48
+ def prefetch(self, paths: Iterable[Path]) -> None:
49
+ """Load several immutable objects in one request, when supported."""
50
+
51
+ def inventory(self) -> Iterable[Path] | None:
52
+ return None
53
+
54
+ def dependencies(self) -> dict[str, str]:
55
+ """Immutable inputs observed by this view; disk checks cannot reuse these."""
56
+ return {}
@@ -0,0 +1,180 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING
4
+
5
+ from smelt.config.errors import ConfigIssue, did_you_mean
6
+ from smelt.config.models import CrossFeatureAllowance
7
+
8
+ if TYPE_CHECKING:
9
+ from smelt.config.models import SmeltConfig
10
+
11
+
12
+ def validate_semantics(
13
+ config: SmeltConfig,
14
+ ) -> tuple[list[ConfigIssue], list[ConfigIssue]]:
15
+ validator = _Validator(config)
16
+ validator.run()
17
+ return validator.errors, validator.warnings
18
+
19
+
20
+ class _Validator:
21
+ def __init__(self, config: SmeltConfig) -> None:
22
+ self.config = config
23
+ self.errors: list[ConfigIssue] = []
24
+ self.warnings: list[ConfigIssue] = []
25
+
26
+ def run(self) -> None:
27
+ self._layers()
28
+ self._cross_feature()
29
+ self._module_sets()
30
+ self._contracts()
31
+
32
+ def _contracts(self) -> None:
33
+ roots = self.config.project.root_packages
34
+ for name, contract in self.config.structure.contracts.items():
35
+ first = contract.match.split(".")[0]
36
+ if first != "*" and first not in roots:
37
+ self._error(
38
+ f"structure.contracts.{name}.match",
39
+ f'"{contract.match}" is not inside project.root_packages ({", ".join(roots)})',
40
+ )
41
+
42
+ def _error(self, path: str, message: str) -> None:
43
+ self.errors.append(ConfigIssue(path, message))
44
+
45
+ def _check_layer(self, path: str, name: str) -> None:
46
+ layers = self.config.architecture.layers
47
+ if name not in layers:
48
+ self._error(path, f'unknown layer "{name}"{did_you_mean(name, layers)}')
49
+
50
+ def _layers(self) -> None:
51
+ seen_paths: dict[str, str] = {}
52
+ for name, layer in self.config.architecture.layers.items():
53
+ base = f"architecture.layers.{name}"
54
+ for index, dep in enumerate(layer.may_depend_on):
55
+ self._check_layer(f"{base}.may_depend_on[{index}]", dep)
56
+ if layer.path in seen_paths:
57
+ self._error(
58
+ f"{base}.path",
59
+ f'duplicate layer path "{layer.path}" '
60
+ f'(already used by "{seen_paths[layer.path]}")',
61
+ )
62
+ seen_paths.setdefault(layer.path, name)
63
+ cycle = _find_layer_cycle(self.config)
64
+ if cycle:
65
+ self.warnings.append(
66
+ ConfigIssue(
67
+ "architecture.layers", "cyclic may_depend_on: " + " -> ".join(cycle)
68
+ )
69
+ )
70
+
71
+ def _cross_feature(self) -> None:
72
+ for index, allowance in enumerate(self.config.architecture.cross_feature.allow):
73
+ if isinstance(allowance, CrossFeatureAllowance):
74
+ _, source_layer, _, target_layer = allowance.components()
75
+ pair = (source_layer, target_layer)
76
+ else:
77
+ source_layer, target_layer = allowance.split("->", maxsplit=1)
78
+ pair = source_layer.strip(), target_layer.strip()
79
+ for name in pair:
80
+ self._check_layer(f"architecture.cross_feature.allow[{index}]", name)
81
+
82
+ def _module_sets(self) -> None:
83
+ arch = self.config.architecture
84
+ named: list[tuple[str, str]] = [
85
+ (f"architecture.{key}[{index}]", module)
86
+ for key in ("shared", "composition_root")
87
+ for index, module in enumerate(getattr(arch, key))
88
+ ]
89
+ named.extend(
90
+ (f"architecture.modules.{module}", module) for module in arch.modules
91
+ )
92
+ for module, layer in arch.modules.items():
93
+ self._check_layer(f"architecture.modules.{module}", layer)
94
+ if arch.features and arch.features.root:
95
+ named.append(("architecture.features.root", arch.features.root))
96
+ if arch.features and arch.features.pattern:
97
+ prefix = arch.features.pattern.rsplit(".", 1)[0]
98
+ named.append(("architecture.features.pattern", prefix))
99
+ roots = self.config.project.root_packages
100
+ for path, module in named:
101
+ if not any(_within(module, root) for root in roots):
102
+ self._error(
103
+ path,
104
+ f'"{module}" is not inside project.root_packages ({", ".join(roots)})',
105
+ )
106
+ for index, pattern in enumerate(arch.wiring):
107
+ prefix = pattern.split("*")[0].rstrip(".")
108
+ if not any(_within(prefix, root) for root in roots):
109
+ self._error(
110
+ f"architecture.wiring[{index}]",
111
+ f'"{pattern}" is not inside project.root_packages ({", ".join(roots)})',
112
+ )
113
+ self._overlapping_sets()
114
+
115
+ def _overlapping_sets(self) -> None:
116
+ arch = self.config.architecture
117
+ features_root = arch.features.root if arch.features else None
118
+ for key in ("shared", "composition_root"):
119
+ for index, module in enumerate(getattr(arch, key)):
120
+ if features_root and _within(module, features_root):
121
+ self._error(
122
+ f"architecture.{key}[{index}]",
123
+ f'"{module}" is inside the features root "{features_root}"',
124
+ )
125
+ for module in arch.modules:
126
+ if features_root and (
127
+ _within(module, features_root) or _within(features_root, module)
128
+ ):
129
+ self._error(
130
+ f"architecture.modules.{module}",
131
+ f'"{module}" overlaps the features root "{features_root}"; '
132
+ "modules are for code outside the features",
133
+ )
134
+ for other in (*arch.shared, *arch.composition_root):
135
+ if _within(module, other):
136
+ self._error(
137
+ f"architecture.modules.{module}",
138
+ f'"{module}" is already covered by "{other}" '
139
+ "(shared or composition_root)",
140
+ )
141
+ for index, module in enumerate(arch.shared):
142
+ for other in arch.composition_root:
143
+ if _within(module, other) or _within(other, module):
144
+ self._error(
145
+ f"architecture.shared[{index}]",
146
+ f'"{module}" overlaps composition_root "{other}"',
147
+ )
148
+
149
+
150
+ def _within(module: str, package: str) -> bool:
151
+ return module == package or module.startswith(f"{package}.")
152
+
153
+
154
+ def _find_layer_cycle(config: SmeltConfig) -> list[str] | None:
155
+ layers = config.architecture.layers
156
+ state: dict[str, int] = {}
157
+ stack: list[str] = []
158
+
159
+ def visit(name: str) -> list[str] | None:
160
+ state[name] = 1
161
+ stack.append(name)
162
+ for dep in layers[name].may_depend_on:
163
+ if dep not in layers or dep == name:
164
+ continue
165
+ if state.get(dep) == 1:
166
+ return [*stack[stack.index(dep) :], dep]
167
+ if dep not in state:
168
+ found = visit(dep)
169
+ if found:
170
+ return found
171
+ stack.pop()
172
+ state[name] = 2
173
+ return None
174
+
175
+ for name in sorted(layers):
176
+ if name not in state:
177
+ found = visit(name)
178
+ if found:
179
+ return found
180
+ return None
File without changes
@@ -0,0 +1,226 @@
1
+ from __future__ import annotations
2
+
3
+ import hashlib
4
+ import json
5
+ import re
6
+ from collections import Counter
7
+ from dataclasses import dataclass, replace
8
+ from typing import TYPE_CHECKING, Literal
9
+
10
+ from pydantic import BaseModel, ConfigDict, ValidationError
11
+
12
+ from smelt.config.errors import ConfigError, ConfigIssue, format_loc
13
+ from smelt.config.source import SourceStore
14
+
15
+ if TYPE_CHECKING:
16
+ from collections.abc import Callable, Iterable
17
+ from pathlib import Path
18
+
19
+ from smelt.diagnostics.violation import Violation
20
+
21
+ DEBT_VERSION = 1
22
+
23
+
24
+ class _DebtItem(BaseModel):
25
+ model_config = ConfigDict(strict=True, extra="forbid")
26
+ fingerprint: str
27
+ code: str
28
+ path: str | None = None
29
+ message: str = ""
30
+
31
+
32
+ class _DebtDocument(BaseModel):
33
+ model_config = ConfigDict(strict=True, extra="forbid")
34
+ version: Literal[1]
35
+ violations: list[_DebtItem]
36
+
37
+
38
+ @dataclass(frozen=True, slots=True)
39
+ class DebtEntry:
40
+ fingerprint: str
41
+ code: str
42
+ path: str | None
43
+ message: str
44
+
45
+
46
+ def fingerprint(violation: Violation, snippet: str, *, legacy: bool = False) -> str:
47
+ # An import finding is an architecture edge, independent of its spelling.
48
+ # Multiplicity in Debt.match still detects an additional identical edge.
49
+ normalized = (
50
+ ""
51
+ if violation.target_module and not legacy
52
+ else re.sub(r"\s+", " ", snippet).strip()
53
+ )
54
+ payload = "\x1f".join(
55
+ [
56
+ violation.code,
57
+ violation.path or "",
58
+ violation.source_module or "",
59
+ violation.target_module or "",
60
+ normalized,
61
+ ]
62
+ )
63
+ return hashlib.sha256(payload.encode()).hexdigest()[:32]
64
+
65
+
66
+ def _edge_field(violation: Violation) -> str | None:
67
+ return {
68
+ "SMT104": "cyclic_edges",
69
+ "SMT313": "ordering_edges",
70
+ "SMT315": "dispatch_edges",
71
+ "SMT423": "patch_edges",
72
+ }.get(violation.code)
73
+
74
+
75
+ def _semantic_edges(violation: Violation) -> list[str]:
76
+ field = _edge_field(violation)
77
+ if field is None or not violation.expected:
78
+ return []
79
+ return list(violation.expected.get(field, []))
80
+
81
+
82
+ def _fingerprints(violation: Violation, snippet: str) -> list[str]:
83
+ edges = _semantic_edges(violation)
84
+ if edges:
85
+ # One baseline token per cyclic architecture edge, independent of the
86
+ # displayed shortest witness. Subtracting edges is an improvement; adding
87
+ # an edge or expanding/splitting a component cannot hide behind a witness.
88
+ prefix = (
89
+ "SMT104"
90
+ if violation.code == "SMT104"
91
+ else f"{violation.code}\x1f{violation.path}\x1f{violation.source_module}"
92
+ )
93
+ return [
94
+ hashlib.sha256(f"{prefix}\x1f{edge}".encode()).hexdigest()[:32]
95
+ for edge in edges
96
+ ]
97
+ return [fingerprint(violation, snippet)]
98
+
99
+
100
+ @dataclass
101
+ class Debt:
102
+ entries: list[DebtEntry]
103
+
104
+ @classmethod
105
+ def load(cls, path: Path, *, store: SourceStore | None = None) -> Debt:
106
+ store = store or SourceStore()
107
+ if not store.is_file(path):
108
+ return cls([])
109
+ try:
110
+ data = _DebtDocument.model_validate_json(store.read_text(path))
111
+ except (OSError, UnicodeError) as exc:
112
+ raise ConfigError(
113
+ [ConfigIssue("", f"cannot read debt file: {exc}")], path.as_posix()
114
+ ) from exc
115
+ except ValidationError as exc:
116
+ issues = [
117
+ ConfigIssue(format_loc(e["loc"]), str(e["msg"])) for e in exc.errors()
118
+ ]
119
+ raise ConfigError(issues, path.as_posix()) from exc
120
+ return cls(
121
+ [
122
+ DebtEntry(
123
+ fingerprint=item.fingerprint,
124
+ code=item.code,
125
+ path=item.path,
126
+ message=item.message,
127
+ )
128
+ for item in data.violations
129
+ ]
130
+ )
131
+
132
+ @classmethod
133
+ def from_violations(
134
+ cls, violations: Iterable[Violation], snippet: Callable[[Violation], str]
135
+ ) -> Debt:
136
+ entries = [
137
+ DebtEntry(key, v.code, v.path, v.message)
138
+ for v in violations
139
+ for key in _fingerprints(
140
+ v, "" if v.target_module or _semantic_edges(v) else snippet(v)
141
+ )
142
+ ]
143
+ entries.sort(key=lambda e: (e.path or "", e.code, e.fingerprint))
144
+ return cls(entries)
145
+
146
+ def write(self, path: Path) -> None:
147
+ path.parent.mkdir(parents=True, exist_ok=True)
148
+ data = {
149
+ "version": DEBT_VERSION,
150
+ "violations": [
151
+ {
152
+ "fingerprint": e.fingerprint,
153
+ "code": e.code,
154
+ "path": e.path,
155
+ "message": e.message,
156
+ }
157
+ for e in self.entries
158
+ ],
159
+ }
160
+ path.write_text(json.dumps(data, indent=2) + "\n", encoding="utf-8")
161
+
162
+ def match(
163
+ self, violations: Iterable[Violation], snippet: Callable[[Violation], str]
164
+ ) -> tuple[list[Violation], list[Violation], list[DebtEntry]]:
165
+ """Split into (new, known) violations and the resolved entries."""
166
+ available = Counter(entry.fingerprint for entry in self.entries)
167
+ new: list[Violation] = []
168
+ known: list[Violation] = []
169
+ for violation in violations:
170
+ text = (
171
+ ""
172
+ if violation.target_module or _semantic_edges(violation)
173
+ else snippet(violation)
174
+ )
175
+ keys = _fingerprints(violation, text)
176
+ matched: set[str] = set()
177
+ for key in keys:
178
+ if available[key] > 0:
179
+ available[key] -= 1
180
+ matched.add(key)
181
+ if not matched and not _semantic_edges(violation):
182
+ # Existing version-1 debt files used the complete source line.
183
+ if violation.target_module:
184
+ text = snippet(violation)
185
+ key = fingerprint(violation, text, legacy=True)
186
+ if available[key] > 0:
187
+ available[key] -= 1
188
+ matched.add(keys[0])
189
+ if matched:
190
+ existing = violation
191
+ if len(matched) < len(keys):
192
+ # Pruning may retain the old part of a growing cycle, but
193
+ # must never accept its newly introduced edges as debt.
194
+ existing = replace(
195
+ violation,
196
+ expected={
197
+ **(violation.expected or {}),
198
+ (_edge_field(violation) or "cyclic_edges"): [
199
+ edge
200
+ for edge, key in zip(
201
+ _semantic_edges(violation), keys, strict=True
202
+ )
203
+ if key in matched
204
+ ],
205
+ },
206
+ )
207
+ known.append(existing)
208
+ if len(matched) < len(keys):
209
+ new.append(violation)
210
+ resolved: list[DebtEntry] = []
211
+ for entry in self.entries:
212
+ if available[entry.fingerprint] > 0:
213
+ available[entry.fingerprint] -= 1
214
+ resolved.append(entry)
215
+ return new, known, resolved
216
+
217
+ def without(self, resolved: Iterable[DebtEntry]) -> Debt:
218
+ """A copy without ``resolved`` entries (matched by fingerprint, one per entry)."""
219
+ remove = Counter(entry.fingerprint for entry in resolved)
220
+ kept: list[DebtEntry] = []
221
+ for entry in self.entries:
222
+ if remove[entry.fingerprint] > 0:
223
+ remove[entry.fingerprint] -= 1
224
+ else:
225
+ kept.append(entry)
226
+ return Debt(kept)
@@ -0,0 +1,41 @@
1
+ from __future__ import annotations
2
+
3
+ from collections import defaultdict
4
+ from typing import TYPE_CHECKING
5
+
6
+ if TYPE_CHECKING:
7
+ from smelt.diagnostics.violation import Violation
8
+
9
+ # winner -> codes it makes redundant when both report the same root cause
10
+ SPECIFICITY: dict[str, frozenset[str]] = {
11
+ "SMT306": frozenset({"SMT301", "SMT305"}),
12
+ # shared or central code importing a feature (also its layer or wiring module):
13
+ # the feature import is the root cause
14
+ "SMT105": frozenset({"SMT101", "SMT106"}),
15
+ }
16
+
17
+
18
+ def deduplicate(violations: list[Violation]) -> list[Violation]:
19
+ """Drop violations that a more specific rule already reports for the same cause."""
20
+ codes_by_key: dict[tuple[str | None, str | int | None], set[str]] = defaultdict(set)
21
+ for violation in violations:
22
+ codes_by_key[_key(violation)].add(violation.code)
23
+
24
+ kept: list[Violation] = []
25
+ for violation in violations:
26
+ # One originating import can reach several unrelated forbidden targets.
27
+ # Only a finding for the same target can explain an import violation.
28
+ same_cause = codes_by_key[_key(violation)]
29
+ redundant = any(
30
+ violation.code in losers and winner in same_cause
31
+ for winner, losers in SPECIFICITY.items()
32
+ )
33
+ if not redundant:
34
+ kept.append(violation)
35
+ return kept
36
+
37
+
38
+ def _key(violation: Violation) -> tuple[str | None, str | int | None]:
39
+ if violation.target_module:
40
+ return violation.path, violation.target_module
41
+ return violation.path, violation.line
@@ -0,0 +1,109 @@
1
+ """Findings that share one decision or fix, for the JSON ``groups`` summary.
2
+
3
+ 86 findings are rarely 86 problems: one facade re-exporting an adapter, one
4
+ missing cross-feature allowance or one old test directory explains many of them.
5
+ A group only points at findings; every finding stays in ``violations``.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import posixpath
11
+ from collections import defaultdict
12
+ from typing import TYPE_CHECKING, Any
13
+
14
+ if TYPE_CHECKING:
15
+ from collections.abc import Callable
16
+
17
+ from smelt.diagnostics.violation import Violation
18
+
19
+
20
+ def group_findings(violations: list[Violation]) -> list[dict[str, Any]]:
21
+ """Facades, dependency edges, cycles and test moves behind several findings.
22
+
23
+ Edges and test moves need two findings to form a group; a facade or a cycle
24
+ is one decision even behind a single finding.
25
+ """
26
+ groups = [
27
+ *_groups("facade", violations, _facade, minimum=1),
28
+ *_groups("edge", violations, _edge, minimum=2),
29
+ *_groups("cycle", violations, _cycle, minimum=1),
30
+ *_groups("test_move", violations, _test_move, minimum=2),
31
+ ]
32
+ return sorted(groups, key=lambda g: (-g["count"], g["kind"], g["key"]))
33
+
34
+
35
+ _CYCLE = "SMT104"
36
+ _TEST_LOCATION = "SMT401"
37
+
38
+
39
+ def _is_transitive(violation: Violation) -> bool:
40
+ """An import finding reached through other modules, not the import itself."""
41
+ return violation.code != _CYCLE and len(violation.import_chain) > 1
42
+
43
+
44
+ def _groups(
45
+ kind: str,
46
+ violations: list[Violation],
47
+ key: Callable[[Violation], str | None],
48
+ *,
49
+ minimum: int,
50
+ ) -> list[dict[str, Any]]:
51
+ members: dict[str, list[Violation]] = defaultdict(list)
52
+ for violation in violations:
53
+ value = key(violation)
54
+ if value is not None:
55
+ members[value].append(violation)
56
+ return [
57
+ _group(kind, value, found)
58
+ for value, found in members.items()
59
+ if len(found) >= minimum
60
+ ]
61
+
62
+
63
+ def _group(kind: str, key: str, found: list[Violation]) -> dict[str, Any]:
64
+ group: dict[str, Any] = {
65
+ "kind": kind,
66
+ "key": key,
67
+ "count": len(found),
68
+ "codes": sorted({v.code for v in found}),
69
+ "violations": [v.finding_id for v in found],
70
+ }
71
+ if kind in ("facade", "edge"):
72
+ transitive = sum(_is_transitive(v) for v in found)
73
+ group["direct"] = len(found) - transitive
74
+ group["transitive"] = transitive
75
+ if kind == "facade":
76
+ group["edges"] = sorted({v.edge for v in found if v.edge})
77
+ if kind == "cycle":
78
+ first = found[0]
79
+ group["witness"] = [link.to_json() for link in first.import_chain]
80
+ group["type_checking_only"] = bool(
81
+ first.expected and first.expected.get("type_checking_only")
82
+ )
83
+ return group
84
+
85
+
86
+ def _facade(violation: Violation) -> str | None:
87
+ """The module relaying a transitive import: narrowing it fixes the chain."""
88
+ if not _is_transitive(violation):
89
+ return None
90
+ return violation.import_chain[0].imported
91
+
92
+
93
+ def _edge(violation: Violation) -> str | None:
94
+ if violation.code == _CYCLE or not violation.edge:
95
+ return None
96
+ return f"{violation.code} {violation.edge}"
97
+
98
+
99
+ def _cycle(violation: Violation) -> str | None:
100
+ return violation.edge if violation.code == _CYCLE and violation.edge else None
101
+
102
+
103
+ def _test_move(violation: Violation) -> str | None:
104
+ if violation.code != _TEST_LOCATION or not violation.path:
105
+ return None
106
+ target = (violation.expected or {}).get("path")
107
+ if not isinstance(target, str):
108
+ return None
109
+ return f"{posixpath.dirname(violation.path)}/ -> {posixpath.dirname(target)}/"
File without changes