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,80 @@
1
+ from __future__ import annotations
2
+
3
+ import io
4
+ import re
5
+ import tokenize
6
+ from dataclasses import dataclass, field
7
+ from typing import TYPE_CHECKING
8
+
9
+ if TYPE_CHECKING:
10
+ from smelt.diagnostics.violation import Violation
11
+
12
+ _PATTERN = re.compile(
13
+ r"#\s*smelt:\s*(?P<kind>ignore-file|ignore)\b"
14
+ r"(?:\[(?P<codes>[^\]]*)\])?"
15
+ r"(?:\s*--\s*(?P<reason>.*))?"
16
+ )
17
+
18
+
19
+ @dataclass(eq=False)
20
+ class Suppression:
21
+ path: str
22
+ line: int
23
+ column: int # 0-based start of the comment
24
+ file_level: bool
25
+ codes: tuple[str, ...] # empty: every code
26
+ reason: str | None
27
+ used_codes: set[str] = field(default_factory=set)
28
+
29
+ def covers(self, code: str) -> bool:
30
+ return not self.codes or any(code.startswith(prefix) for prefix in self.codes)
31
+
32
+ def matches(self, violation: Violation) -> bool:
33
+ if violation.path != self.path or not self.covers(violation.code):
34
+ return False
35
+ return self.file_level or violation.line == self.line
36
+
37
+ def mark_used(self, code: str) -> None:
38
+ self.used_codes.add(code)
39
+
40
+ def unused_codes(self) -> tuple[str, ...]:
41
+ return tuple(
42
+ prefix
43
+ for prefix in self.codes
44
+ if not any(code.startswith(prefix) for code in self.used_codes)
45
+ )
46
+
47
+ @property
48
+ def is_used(self) -> bool:
49
+ return bool(self.used_codes)
50
+
51
+
52
+ def parse_suppressions(path: str, source: str) -> list[Suppression]:
53
+ found: list[Suppression] = []
54
+ try:
55
+ tokens = list(tokenize.generate_tokens(io.StringIO(source).readline))
56
+ except (tokenize.TokenError, SyntaxError):
57
+ return found
58
+ for token in tokens:
59
+ if token.type != tokenize.COMMENT:
60
+ continue
61
+ match = _PATTERN.search(token.string)
62
+ if match is None:
63
+ continue
64
+ codes = tuple(
65
+ code.strip().upper()
66
+ for code in (match.group("codes") or "").split(",")
67
+ if code.strip()
68
+ )
69
+ reason = (match.group("reason") or "").strip() or None
70
+ found.append(
71
+ Suppression(
72
+ path=path,
73
+ line=token.start[0],
74
+ column=token.start[1] + match.start(),
75
+ file_level=match.group("kind") == "ignore-file",
76
+ codes=codes,
77
+ reason=reason,
78
+ )
79
+ )
80
+ return found
@@ -0,0 +1,120 @@
1
+ from __future__ import annotations
2
+
3
+ import hashlib
4
+ from collections.abc import Mapping # noqa: TC003 - persistent JSON validation
5
+ from dataclasses import dataclass, field, replace
6
+ from enum import StrEnum
7
+ from typing import Any, Self
8
+
9
+ DOCS_BASE_URL = "https://github.com/mathisarends/smelt/blob/main/docs/rules"
10
+
11
+
12
+ class Severity(StrEnum):
13
+ ERROR = "error"
14
+ WARNING = "warning"
15
+ HINT = "hint"
16
+
17
+ @property
18
+ def rank(self) -> int:
19
+ return {"error": 3, "warning": 2, "hint": 1}[self.value]
20
+
21
+ def at_least(self, other: Severity) -> bool:
22
+ return self.rank >= other.rank
23
+
24
+
25
+ class Category(StrEnum):
26
+ DEPENDENCIES = "dependencies"
27
+ STRUCTURE = "structure"
28
+ TESTS = "tests"
29
+ META = "meta"
30
+
31
+
32
+ @dataclass(frozen=True, slots=True)
33
+ class ImportLink:
34
+ importer: str
35
+ imported: str
36
+ line: int | None = None
37
+
38
+ def to_json(self) -> dict[str, Any]:
39
+ return {"importer": self.importer, "imported": self.imported, "line": self.line}
40
+
41
+
42
+ @dataclass(frozen=True, slots=True)
43
+ class Violation:
44
+ code: str
45
+ rule: str
46
+ severity: Severity
47
+ message: str
48
+ path: str | None = None
49
+ line: int | None = None
50
+ column: int | None = None
51
+ end_line: int | None = None
52
+ end_column: int | None = None
53
+ source_module: str | None = None
54
+ target_module: str | None = None
55
+ import_chain: tuple[ImportLink, ...] = ()
56
+ feature: str | None = None
57
+ layer: str | None = None
58
+ expected: Mapping[str, Any] | None = None
59
+ hint: str | None = None
60
+ # The architecture dependency behind an import finding, e.g. "user.domain ->
61
+ # chat.domain": findings sharing one usually share one decision or fix.
62
+ edge: str | None = None
63
+ docs_url: str | None = None
64
+ category: Category | None = field(default=None, compare=False)
65
+
66
+ def with_severity(self, severity: Severity) -> Self:
67
+ return replace(self, severity=severity)
68
+
69
+ def sort_key(self) -> tuple[str, int, int, str, str]:
70
+ return (
71
+ self.path or "",
72
+ self.line or 0,
73
+ self.column or 0,
74
+ self.code,
75
+ self.message,
76
+ )
77
+
78
+ @property
79
+ def finding_id(self) -> str:
80
+ """A short id, stable for the same finding at the same place; groups cite it."""
81
+ payload = "\x1f".join(
82
+ [
83
+ self.code,
84
+ self.path or "",
85
+ str(self.line or 0),
86
+ str(self.column or 0),
87
+ self.message,
88
+ ]
89
+ )
90
+ return hashlib.sha256(payload.encode()).hexdigest()[:12]
91
+
92
+ def to_json(self) -> dict[str, Any]:
93
+ data: dict[str, Any] = {
94
+ "id": self.finding_id,
95
+ "code": self.code,
96
+ "rule": self.rule,
97
+ "severity": self.severity.value,
98
+ "category": self.category.value if self.category else None,
99
+ "message": self.message,
100
+ "path": self.path,
101
+ "line": self.line,
102
+ "column": self.column,
103
+ "end_line": self.end_line,
104
+ "end_column": self.end_column,
105
+ "feature": self.feature,
106
+ "layer": self.layer,
107
+ "source_module": self.source_module,
108
+ "target_module": self.target_module,
109
+ "edge": self.edge,
110
+ }
111
+ if self.import_chain:
112
+ data["import_chain"] = [link.to_json() for link in self.import_chain]
113
+ data["expected"] = dict(self.expected) if self.expected is not None else None
114
+ data["hint"] = self.hint
115
+ data["docs_url"] = self.docs_url
116
+ return data
117
+
118
+
119
+ def docs_url(code: str) -> str:
120
+ return f"{DOCS_BASE_URL}/{code}.md"
smelt/docs.py ADDED
@@ -0,0 +1,98 @@
1
+ from __future__ import annotations
2
+
3
+ import json
4
+ from typing import TYPE_CHECKING
5
+
6
+ from smelt.engine.check import rule_meta
7
+ from smelt.engine.schema import config_json_schema
8
+ from smelt.rules.registry import build_rule_set, catalog_metadata
9
+
10
+ if TYPE_CHECKING:
11
+ from pathlib import Path
12
+
13
+ from smelt.rules.base import Rule
14
+
15
+ # Maintainer scripts outside the analyzed package use this generator entry point.
16
+ __all__ = ["write_generated_files"]
17
+
18
+ SCHEMA_FILE = "smelt.schema.json"
19
+ DOCS_DIR = "docs/rules"
20
+
21
+
22
+ def write_generated_files(root: Path) -> list[str]:
23
+ """Write the schema and the rule pages, and report what changed."""
24
+ changed = []
25
+ for name, content in _generated_files().items():
26
+ path = root / name
27
+ path.parent.mkdir(parents=True, exist_ok=True)
28
+ if not path.is_file() or path.read_text(encoding="utf-8") != content:
29
+ path.write_text(content, encoding="utf-8", newline="\n")
30
+ changed.append(name)
31
+ return changed
32
+
33
+
34
+ def _rule_page(rule: Rule) -> str:
35
+ """The reference page for one rule, the same content `smelt explain` prints."""
36
+ doc = rule.explain()
37
+ meta = rule_meta(rule)
38
+ lines = [
39
+ f"# {rule.code} {rule.name}",
40
+ "",
41
+ doc.summary,
42
+ "",
43
+ f"- **Category:** {meta.category.value}",
44
+ f"- **Activation:** {meta.activation['mode']} — {meta.activation['description']}",
45
+ f"- **Severity when active:** {meta.default_severity.value}",
46
+ "",
47
+ "## Why",
48
+ "",
49
+ doc.rationale,
50
+ "",
51
+ "## Bad",
52
+ "",
53
+ f"```{doc.example_language}",
54
+ doc.bad,
55
+ "```",
56
+ "",
57
+ "## Good",
58
+ "",
59
+ f"```{doc.example_language}",
60
+ doc.good,
61
+ "```",
62
+ "",
63
+ "## How to fix",
64
+ "",
65
+ doc.fix,
66
+ ]
67
+ if doc.config:
68
+ lines += ["", "## Config", "", *(f"- `{key}`" for key in doc.config)]
69
+ lines += ["", f"Explain it in the terminal with `smelt explain {rule.code}`."]
70
+ return "\n".join(lines) + "\n"
71
+
72
+
73
+ def _rule_index(rules: list[Rule]) -> str:
74
+ lines = [
75
+ "# Rules",
76
+ "",
77
+ "| Code | Name | Category | Activation | Severity |",
78
+ "| --- | --- | --- | --- | --- |",
79
+ ]
80
+ for rule in rules:
81
+ meta = rule_meta(rule)
82
+ lines.append(
83
+ f"| [{rule.code}]({rule.code}.md) | {rule.name} | "
84
+ f"{meta.category.value} | {meta.activation['mode']} | {meta.default_severity.value} |"
85
+ )
86
+ lines += ["", "`smelt rules` prints the same table in the terminal."]
87
+ return "\n".join(lines) + "\n"
88
+
89
+
90
+ def _generated_files() -> dict[str, str]:
91
+ """Every generated file, keyed by its path relative to the repository root."""
92
+ rules = build_rule_set().rules
93
+ files = {SCHEMA_FILE: json.dumps(config_json_schema(), indent=2) + "\n"}
94
+ files["smelt/rules/catalog.json"] = json.dumps(catalog_metadata(), indent=2) + "\n"
95
+ files[f"{DOCS_DIR}/README.md"] = _rule_index(rules)
96
+ for rule in rules:
97
+ files[f"{DOCS_DIR}/{rule.code}.md"] = _rule_page(rule)
98
+ return files
File without changes