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.
- smelt/__init__.py +6 -0
- smelt/__main__.py +5 -0
- smelt/analysis/__init__.py +45 -0
- smelt/analysis/annotations.py +105 -0
- smelt/analysis/cache.py +168 -0
- smelt/analysis/context.py +147 -0
- smelt/analysis/contracts.py +210 -0
- smelt/analysis/coverage.py +377 -0
- smelt/analysis/definition_blocks.py +569 -0
- smelt/analysis/enum_values.py +217 -0
- smelt/analysis/errors.py +11 -0
- smelt/analysis/exports.py +166 -0
- smelt/analysis/files.py +279 -0
- smelt/analysis/function_usage.py +123 -0
- smelt/analysis/graphs.py +77 -0
- smelt/analysis/identity.py +64 -0
- smelt/analysis/import_cache.py +98 -0
- smelt/analysis/imports.py +452 -0
- smelt/analysis/literals.py +17 -0
- smelt/analysis/member_state.py +108 -0
- smelt/analysis/members.py +975 -0
- smelt/analysis/ordering.py +399 -0
- smelt/analysis/ordering_rewrite.py +232 -0
- smelt/analysis/package_exports.py +58 -0
- smelt/analysis/parsing.py +126 -0
- smelt/analysis/python_version.py +164 -0
- smelt/analysis/retained.py +84 -0
- smelt/analysis/syntax.py +109 -0
- smelt/analysis/test_awaits.py +91 -0
- smelt/analysis/test_cases.py +84 -0
- smelt/analysis/test_deadlines.py +90 -0
- smelt/analysis/test_outcomes.py +71 -0
- smelt/analysis/test_subprocesses.py +107 -0
- smelt/analysis/test_syntax.py +1425 -0
- smelt/analysis/test_timing.py +76 -0
- smelt/analysis/type_dispatch.py +193 -0
- smelt/analysis/workspace.py +146 -0
- smelt/cli/__init__.py +3 -0
- smelt/cli/app.py +300 -0
- smelt/cli/commands/__init__.py +62 -0
- smelt/cli/commands/adopt.py +274 -0
- smelt/cli/commands/check.py +151 -0
- smelt/cli/commands/discover.py +51 -0
- smelt/cli/commands/info.py +112 -0
- smelt/cli/session.py +163 -0
- smelt/cli/session_slots.py +43 -0
- smelt/cli/support.py +90 -0
- smelt/config/__init__.py +36 -0
- smelt/config/discovery.py +93 -0
- smelt/config/errors.py +51 -0
- smelt/config/loader.py +282 -0
- smelt/config/models.py +717 -0
- smelt/config/patterns.py +47 -0
- smelt/config/schema.py +23 -0
- smelt/config/source.py +56 -0
- smelt/config/validation.py +180 -0
- smelt/diagnostics/__init__.py +0 -0
- smelt/diagnostics/debt.py +226 -0
- smelt/diagnostics/dedupe.py +41 -0
- smelt/diagnostics/groups.py +109 -0
- smelt/diagnostics/render/__init__.py +0 -0
- smelt/diagnostics/render/machine.py +237 -0
- smelt/diagnostics/render/text.py +321 -0
- smelt/diagnostics/report.py +104 -0
- smelt/diagnostics/suppressions.py +80 -0
- smelt/diagnostics/violation.py +120 -0
- smelt/docs.py +98 -0
- smelt/engine/__init__.py +0 -0
- smelt/engine/briefing.py +630 -0
- smelt/engine/cache.py +65 -0
- smelt/engine/changes.py +260 -0
- smelt/engine/check.py +724 -0
- smelt/engine/coverage.py +151 -0
- smelt/engine/evaluation.py +291 -0
- smelt/engine/file_checks.py +94 -0
- smelt/engine/git_source.py +140 -0
- smelt/engine/inference.py +854 -0
- smelt/engine/mirror_inference.py +95 -0
- smelt/engine/native_git.py +190 -0
- smelt/engine/ordering_fixes.py +132 -0
- smelt/engine/paths.py +163 -0
- smelt/engine/result_cache.py +208 -0
- smelt/engine/rule_settings.py +164 -0
- smelt/engine/schema.py +58 -0
- smelt/engine/staged.py +75 -0
- smelt/engine/workspace.py +5 -0
- smelt/model/__init__.py +9 -0
- smelt/model/architecture.py +234 -0
- smelt/py.typed +0 -0
- smelt/rules/__init__.py +0 -0
- smelt/rules/activation.py +129 -0
- smelt/rules/base.py +131 -0
- smelt/rules/catalog.json +600 -0
- smelt/rules/common.py +164 -0
- smelt/rules/dependencies/__init__.py +0 -0
- smelt/rules/dependencies/composition_root.py +86 -0
- smelt/rules/dependencies/cycles.py +329 -0
- smelt/rules/dependencies/fanout.py +81 -0
- smelt/rules/dependencies/features.py +165 -0
- smelt/rules/dependencies/interfaces.py +84 -0
- smelt/rules/dependencies/layers.py +138 -0
- smelt/rules/dependencies/third_party.py +132 -0
- smelt/rules/member_names.py +13 -0
- smelt/rules/meta.py +74 -0
- smelt/rules/registry.py +132 -0
- smelt/rules/structure/__init__.py +0 -0
- smelt/rules/structure/assignments.py +114 -0
- smelt/rules/structure/contracts.py +71 -0
- smelt/rules/structure/coverage.py +100 -0
- smelt/rules/structure/enum_values.py +50 -0
- smelt/rules/structure/headers.py +143 -0
- smelt/rules/structure/import_style.py +145 -0
- smelt/rules/structure/layout.py +202 -0
- smelt/rules/structure/members.py +116 -0
- smelt/rules/structure/naming.py +132 -0
- smelt/rules/structure/ordering.py +83 -0
- smelt/rules/structure/package_imports.py +117 -0
- smelt/rules/structure/type_dispatch.py +79 -0
- smelt/rules/testing/__init__.py +0 -0
- smelt/rules/testing/awaits.py +46 -0
- smelt/rules/testing/cases.py +47 -0
- smelt/rules/testing/location.py +537 -0
- smelt/rules/testing/mocking.py +140 -0
- smelt/rules/testing/outcomes.py +119 -0
- smelt/rules/testing/private_access.py +93 -0
- smelt/rules/testing/public_attributes.py +62 -0
- smelt/rules/testing/public_functions.py +52 -0
- smelt/rules/testing/timing.py +62 -0
- smelt_cli-0.2.0.data/scripts/smelt.exe +0 -0
- smelt_cli-0.2.0.dist-info/METADATA +103 -0
- smelt_cli-0.2.0.dist-info/RECORD +133 -0
- smelt_cli-0.2.0.dist-info/WHEEL +4 -0
- smelt_cli-0.2.0.dist-info/sboms/smelt-cli.cyclonedx.json +5462 -0
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
"""Stable definition ordering within execution barriers, without running source."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import ast
|
|
6
|
+
import heapq
|
|
7
|
+
from collections import defaultdict
|
|
8
|
+
from dataclasses import dataclass, field, replace
|
|
9
|
+
from typing import TYPE_CHECKING, Any
|
|
10
|
+
|
|
11
|
+
from smelt.analysis.definition_blocks import (
|
|
12
|
+
DefinitionBlock,
|
|
13
|
+
ScopeNode,
|
|
14
|
+
build_blocks,
|
|
15
|
+
is_main_guard,
|
|
16
|
+
)
|
|
17
|
+
from smelt.config.models import OrderingConfig
|
|
18
|
+
|
|
19
|
+
if TYPE_CHECKING:
|
|
20
|
+
from smelt.analysis.contracts import ContractIndex
|
|
21
|
+
from smelt.analysis.files import FileIndex, SourceFile
|
|
22
|
+
from smelt.analysis.parsing import AstCache
|
|
23
|
+
from smelt.analysis.python_version import PythonTargetIndex
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@dataclass(frozen=True)
|
|
27
|
+
class OrderSection:
|
|
28
|
+
owner: ScopeNode
|
|
29
|
+
scope: str
|
|
30
|
+
blocks: tuple[DefinitionBlock, ...]
|
|
31
|
+
order: tuple[int, ...]
|
|
32
|
+
edges: tuple[str, ...]
|
|
33
|
+
fix_reason: str | None = None
|
|
34
|
+
fix_blocker: _FixBlocker | None = None
|
|
35
|
+
|
|
36
|
+
@property
|
|
37
|
+
def key(self) -> str:
|
|
38
|
+
return f"{self.scope}:{self.blocks[0].line}:{self.blocks[-1].end_line}"
|
|
39
|
+
|
|
40
|
+
@property
|
|
41
|
+
def line(self) -> int:
|
|
42
|
+
return self.blocks[0].line
|
|
43
|
+
|
|
44
|
+
@property
|
|
45
|
+
def original(self) -> list[str]:
|
|
46
|
+
return [block.label for block in self.blocks]
|
|
47
|
+
|
|
48
|
+
@property
|
|
49
|
+
def expected(self) -> list[str]:
|
|
50
|
+
return [self.blocks[index].label for index in self.order]
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
@dataclass(frozen=True)
|
|
54
|
+
class OrderIssue:
|
|
55
|
+
scope: str
|
|
56
|
+
line: int
|
|
57
|
+
message: str
|
|
58
|
+
edges: tuple[str, ...]
|
|
59
|
+
section: OrderSection | None = None
|
|
60
|
+
reason: str | None = None
|
|
61
|
+
fix_node: ast.stmt | ast.expr | ast.type_param | None = None
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
@dataclass
|
|
65
|
+
class FileOrder:
|
|
66
|
+
path: str
|
|
67
|
+
module: str
|
|
68
|
+
tree: ast.Module
|
|
69
|
+
config: OrderingConfig
|
|
70
|
+
contract: str
|
|
71
|
+
deferred: bool
|
|
72
|
+
sections: list[OrderSection] = field(default_factory=list)
|
|
73
|
+
issues: list[OrderIssue] = field(default_factory=list)
|
|
74
|
+
barriers: list[dict[str, Any]] = field(default_factory=list)
|
|
75
|
+
|
|
76
|
+
def summary(self) -> dict[str, Any]:
|
|
77
|
+
return {
|
|
78
|
+
"path": self.path,
|
|
79
|
+
"contract": self.contract,
|
|
80
|
+
"deferred_annotations": self.deferred,
|
|
81
|
+
"sections": len(self.sections),
|
|
82
|
+
"barriers": self.barriers,
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
class DefinitionOrderIndex:
|
|
87
|
+
def __init__(
|
|
88
|
+
self,
|
|
89
|
+
files: FileIndex,
|
|
90
|
+
contracts: ContractIndex,
|
|
91
|
+
asts: AstCache,
|
|
92
|
+
python_targets: PythonTargetIndex,
|
|
93
|
+
) -> None:
|
|
94
|
+
self.files: dict[str, FileOrder] = {}
|
|
95
|
+
for source in files.sources.values():
|
|
96
|
+
contract = contracts.policy_for(source.module, "ordering")
|
|
97
|
+
tree = asts.parse(source.path)
|
|
98
|
+
future = any(
|
|
99
|
+
isinstance(node, ast.ImportFrom)
|
|
100
|
+
and node.module == "__future__"
|
|
101
|
+
and node.level == 0
|
|
102
|
+
and any(alias.name == "annotations" for alias in node.names)
|
|
103
|
+
for node in tree.body
|
|
104
|
+
)
|
|
105
|
+
deferred = (
|
|
106
|
+
future or python_targets.for_path(source.absolute).deferred_annotations
|
|
107
|
+
)
|
|
108
|
+
self.files[source.path] = _analyze_order(
|
|
109
|
+
tree,
|
|
110
|
+
contract.spec.ordering
|
|
111
|
+
if contract and contract.spec.ordering
|
|
112
|
+
else OrderingConfig(),
|
|
113
|
+
source=source,
|
|
114
|
+
contract=contract.name
|
|
115
|
+
if contract
|
|
116
|
+
else "rules.public-definitions-first",
|
|
117
|
+
deferred=deferred,
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
def summary(self) -> list[dict[str, Any]]:
|
|
121
|
+
return [result.summary() for _, result in sorted(self.files.items())]
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
@dataclass(frozen=True)
|
|
125
|
+
class _FixBlocker:
|
|
126
|
+
reason: str
|
|
127
|
+
node: ast.stmt | ast.expr | ast.type_param
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def _precedence(blocks: tuple[DefinitionBlock, ...]) -> list[set[int]]:
|
|
131
|
+
"""Preserve group order, binding versions, and reads before later rebinding."""
|
|
132
|
+
before: list[set[int]] = [set() for _ in blocks]
|
|
133
|
+
last_group: dict[str, int] = {}
|
|
134
|
+
writers: dict[str, list[int]] = defaultdict(list)
|
|
135
|
+
for index, block in enumerate(blocks):
|
|
136
|
+
if block.category in last_group:
|
|
137
|
+
before[index].add(last_group[block.category])
|
|
138
|
+
last_group[block.category] = index
|
|
139
|
+
for name in block.binds:
|
|
140
|
+
if writers[name]:
|
|
141
|
+
before[index].add(writers[name][-1])
|
|
142
|
+
writers[name].append(index)
|
|
143
|
+
for index, block in enumerate(blocks):
|
|
144
|
+
for name in block.reads:
|
|
145
|
+
prior = [writer for writer in writers[name] if writer < index]
|
|
146
|
+
later = [writer for writer in writers[name] if writer > index]
|
|
147
|
+
if prior:
|
|
148
|
+
before[index].add(prior[-1])
|
|
149
|
+
if later:
|
|
150
|
+
before[later[0]].add(index)
|
|
151
|
+
return before
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def _canonical_order(
|
|
155
|
+
blocks: tuple[DefinitionBlock, ...], priorities: list[str]
|
|
156
|
+
) -> tuple[int, ...]:
|
|
157
|
+
dependencies = _precedence(blocks)
|
|
158
|
+
ranks = {name: index for index, name in enumerate(priorities)}
|
|
159
|
+
followers: dict[int, list[int]] = defaultdict(list)
|
|
160
|
+
for index, required in enumerate(dependencies):
|
|
161
|
+
for prerequisite in required:
|
|
162
|
+
followers[prerequisite].append(index)
|
|
163
|
+
priority = [ranks.get(block.category, len(ranks)) for block in blocks]
|
|
164
|
+
for index in reversed(range(len(blocks))):
|
|
165
|
+
for follower in followers[index]:
|
|
166
|
+
priority[index] = min(priority[index], priority[follower])
|
|
167
|
+
ready = [
|
|
168
|
+
(priority[index], index)
|
|
169
|
+
for index in range(len(blocks))
|
|
170
|
+
if not dependencies[index]
|
|
171
|
+
]
|
|
172
|
+
heapq.heapify(ready)
|
|
173
|
+
result: list[int] = []
|
|
174
|
+
while ready:
|
|
175
|
+
_, index = heapq.heappop(ready)
|
|
176
|
+
result.append(index)
|
|
177
|
+
for follower in followers[index]:
|
|
178
|
+
dependencies[follower].remove(index)
|
|
179
|
+
if not dependencies[follower]:
|
|
180
|
+
heapq.heappush(ready, (priority[follower], follower))
|
|
181
|
+
# All constraints preserve an existing ordering relation, so they form a DAG.
|
|
182
|
+
return tuple(result)
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def _section(
|
|
186
|
+
result: FileOrder,
|
|
187
|
+
owner: ScopeNode,
|
|
188
|
+
scope: str,
|
|
189
|
+
blocks: list[DefinitionBlock],
|
|
190
|
+
fix_blocker: _FixBlocker | None,
|
|
191
|
+
) -> None:
|
|
192
|
+
if not blocks:
|
|
193
|
+
return
|
|
194
|
+
group = tuple(blocks)
|
|
195
|
+
priorities = (
|
|
196
|
+
result.config.class_order
|
|
197
|
+
if isinstance(owner, ast.ClassDef)
|
|
198
|
+
else result.config.module
|
|
199
|
+
)
|
|
200
|
+
order = _canonical_order(group, list(priorities))
|
|
201
|
+
positions = {index: position for position, index in enumerate(order)}
|
|
202
|
+
edges = tuple(
|
|
203
|
+
f"{group[left].key} -> {group[right].key}"
|
|
204
|
+
for left in range(len(group))
|
|
205
|
+
for right in range(left + 1, len(group))
|
|
206
|
+
if positions[left] > positions[right]
|
|
207
|
+
)
|
|
208
|
+
factory = next((block for block in group if block.fix_reason), None)
|
|
209
|
+
blocker = fix_blocker or (
|
|
210
|
+
_FixBlocker(factory.fix_reason, factory.fix_node or factory.nodes[0])
|
|
211
|
+
if factory and factory.fix_reason
|
|
212
|
+
else None
|
|
213
|
+
)
|
|
214
|
+
reason = blocker.reason if blocker else None
|
|
215
|
+
if result.config.fix == "off":
|
|
216
|
+
reason = "ordering fixes are disabled for this package"
|
|
217
|
+
blocker = None
|
|
218
|
+
section = OrderSection(owner, scope, group, order, edges, reason, blocker)
|
|
219
|
+
result.sections.append(section)
|
|
220
|
+
if edges:
|
|
221
|
+
first_changed = next(
|
|
222
|
+
index for index, expected in enumerate(order) if index != expected
|
|
223
|
+
)
|
|
224
|
+
result.issues.append(
|
|
225
|
+
OrderIssue(
|
|
226
|
+
scope,
|
|
227
|
+
group[first_changed].line,
|
|
228
|
+
f"definitions in {scope} have {len(edges)} avoidable ordering inversion(s)",
|
|
229
|
+
edges,
|
|
230
|
+
section,
|
|
231
|
+
)
|
|
232
|
+
)
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
def _property_boundaries(blocks: list[DefinitionBlock]) -> list[DefinitionBlock]:
|
|
236
|
+
families: dict[str, list[int]] = defaultdict(list)
|
|
237
|
+
for index, block in enumerate(blocks):
|
|
238
|
+
if any(
|
|
239
|
+
isinstance(node, ast.FunctionDef | ast.AsyncFunctionDef)
|
|
240
|
+
and any(
|
|
241
|
+
isinstance(decorator, ast.Attribute)
|
|
242
|
+
and decorator.attr in {"getter", "setter", "deleter"}
|
|
243
|
+
for decorator in node.decorator_list
|
|
244
|
+
)
|
|
245
|
+
for node in block.nodes
|
|
246
|
+
):
|
|
247
|
+
families[block.label].append(index)
|
|
248
|
+
for name in families:
|
|
249
|
+
indices = [index for index, block in enumerate(blocks) if block.label == name]
|
|
250
|
+
if len(indices) > 1:
|
|
251
|
+
for index in indices:
|
|
252
|
+
blocks[index] = replace(
|
|
253
|
+
blocks[index],
|
|
254
|
+
barrier="noncontiguous property family is kept in place",
|
|
255
|
+
)
|
|
256
|
+
return blocks
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
def _fixed_positions(result: FileOrder, blocks: list[DefinitionBlock]) -> None:
|
|
260
|
+
seen: list[DefinitionBlock] = []
|
|
261
|
+
ranks: dict[str, int] = {
|
|
262
|
+
name: index for index, name in enumerate(result.config.module)
|
|
263
|
+
}
|
|
264
|
+
for index, block in enumerate(blocks):
|
|
265
|
+
if block.category in {"imports", "future-imports"}:
|
|
266
|
+
rank = ranks[block.category]
|
|
267
|
+
late = [
|
|
268
|
+
prior
|
|
269
|
+
for prior in seen
|
|
270
|
+
if prior.category in result.config.module
|
|
271
|
+
and ranks[prior.category] > rank
|
|
272
|
+
]
|
|
273
|
+
if late:
|
|
274
|
+
edges = tuple(f"{prior.key} -> fixed:{block.key}" for prior in late)
|
|
275
|
+
result.issues.append(
|
|
276
|
+
OrderIssue(
|
|
277
|
+
"module",
|
|
278
|
+
block.line,
|
|
279
|
+
f"{block.category} appear after later definition groups",
|
|
280
|
+
edges,
|
|
281
|
+
reason="imports are execution barriers; review placement manually",
|
|
282
|
+
fix_node=block.nodes[0],
|
|
283
|
+
)
|
|
284
|
+
)
|
|
285
|
+
if is_main_guard(block.nodes[0]) and index + 1 < len(blocks):
|
|
286
|
+
edges = tuple(
|
|
287
|
+
f"main:{block.key} -> {later.key}" for later in blocks[index + 1 :]
|
|
288
|
+
)
|
|
289
|
+
result.issues.append(
|
|
290
|
+
OrderIssue(
|
|
291
|
+
"module",
|
|
292
|
+
block.line,
|
|
293
|
+
"the main guard must be the final module statement",
|
|
294
|
+
edges,
|
|
295
|
+
reason="moving the main guard changes execution timing; review manually",
|
|
296
|
+
fix_node=block.nodes[0],
|
|
297
|
+
)
|
|
298
|
+
)
|
|
299
|
+
seen.append(block)
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
def _reflective(tree: ast.Module) -> _FixBlocker | None:
|
|
303
|
+
found = [
|
|
304
|
+
node
|
|
305
|
+
for node in ast.walk(tree)
|
|
306
|
+
if isinstance(node, ast.Global | ast.Nonlocal)
|
|
307
|
+
or (
|
|
308
|
+
isinstance(node, ast.Name)
|
|
309
|
+
and node.id
|
|
310
|
+
in {"globals", "locals", "vars", "eval", "exec", "__annotations__"}
|
|
311
|
+
)
|
|
312
|
+
or (
|
|
313
|
+
isinstance(node, ast.Attribute)
|
|
314
|
+
and node.attr
|
|
315
|
+
in {"__dict__", "__annotations__", "__code__", "settrace", "setprofile"}
|
|
316
|
+
)
|
|
317
|
+
]
|
|
318
|
+
return (
|
|
319
|
+
_FixBlocker(
|
|
320
|
+
"runtime namespace or annotation introspection requires manual review",
|
|
321
|
+
min(found, key=lambda node: (node.lineno, node.col_offset)),
|
|
322
|
+
)
|
|
323
|
+
if found
|
|
324
|
+
else None
|
|
325
|
+
)
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
def _body(
|
|
329
|
+
result: FileOrder,
|
|
330
|
+
owner: ScopeNode,
|
|
331
|
+
scope: str,
|
|
332
|
+
aliases: dict[str, str],
|
|
333
|
+
fix_blocker: _FixBlocker | None,
|
|
334
|
+
) -> None:
|
|
335
|
+
blocks, children = build_blocks(
|
|
336
|
+
owner,
|
|
337
|
+
scope=scope,
|
|
338
|
+
config=result.config,
|
|
339
|
+
aliases=dict(aliases),
|
|
340
|
+
deferred=result.deferred,
|
|
341
|
+
)
|
|
342
|
+
blocks = _property_boundaries(blocks)
|
|
343
|
+
if isinstance(owner, ast.Module):
|
|
344
|
+
_fixed_positions(result, blocks)
|
|
345
|
+
group: list[DefinitionBlock] = []
|
|
346
|
+
for block in blocks:
|
|
347
|
+
if block.barrier:
|
|
348
|
+
_section(result, owner, scope, group, fix_blocker)
|
|
349
|
+
group = []
|
|
350
|
+
result.barriers.append(
|
|
351
|
+
{"scope": scope, "line": block.line, "reason": block.barrier}
|
|
352
|
+
)
|
|
353
|
+
else:
|
|
354
|
+
group.append(block)
|
|
355
|
+
_section(result, owner, scope, group, fix_blocker)
|
|
356
|
+
counts: dict[str, int] = defaultdict(int)
|
|
357
|
+
for child, child_aliases in children:
|
|
358
|
+
ordinal = counts[child.name]
|
|
359
|
+
counts[child.name] += 1
|
|
360
|
+
child_scope = f"{scope}.{child.name}#{ordinal}"
|
|
361
|
+
triggers: list[ast.expr | ast.type_param] = [
|
|
362
|
+
*child.decorator_list,
|
|
363
|
+
*child.bases,
|
|
364
|
+
*(keyword.value for keyword in child.keywords),
|
|
365
|
+
*child.type_params,
|
|
366
|
+
]
|
|
367
|
+
child_blocker = fix_blocker or (
|
|
368
|
+
_FixBlocker(
|
|
369
|
+
"class bases, metaclass or decorators may inspect definition order",
|
|
370
|
+
min(triggers, key=lambda node: (node.lineno, node.col_offset)),
|
|
371
|
+
)
|
|
372
|
+
if triggers
|
|
373
|
+
else None
|
|
374
|
+
)
|
|
375
|
+
_body(result, child, child_scope, child_aliases, child_blocker)
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
def _analyze_order(
|
|
379
|
+
tree: ast.Module,
|
|
380
|
+
config: OrderingConfig,
|
|
381
|
+
*,
|
|
382
|
+
source: SourceFile | None = None,
|
|
383
|
+
contract: str = "ordering",
|
|
384
|
+
deferred: bool = False,
|
|
385
|
+
) -> FileOrder:
|
|
386
|
+
result = FileOrder(
|
|
387
|
+
source.path if source else "module.py",
|
|
388
|
+
source.module if source else "module",
|
|
389
|
+
tree,
|
|
390
|
+
config,
|
|
391
|
+
contract,
|
|
392
|
+
deferred,
|
|
393
|
+
)
|
|
394
|
+
reflection = _reflective(tree)
|
|
395
|
+
aliases = {
|
|
396
|
+
name: f"builtins.{name}" for name in ("property", "staticmethod", "classmethod")
|
|
397
|
+
}
|
|
398
|
+
_body(result, tree, "module", aliases, reflection)
|
|
399
|
+
return result
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
"""Render approved statement moves while preserving source text and trivia."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import ast
|
|
6
|
+
import copy
|
|
7
|
+
import io
|
|
8
|
+
import re
|
|
9
|
+
import tokenize
|
|
10
|
+
import warnings
|
|
11
|
+
from dataclasses import dataclass
|
|
12
|
+
from typing import TYPE_CHECKING
|
|
13
|
+
|
|
14
|
+
from smelt.analysis.definition_blocks import statement_line
|
|
15
|
+
from smelt.analysis.parsing import AnalysisError
|
|
16
|
+
|
|
17
|
+
if TYPE_CHECKING:
|
|
18
|
+
from smelt.analysis.definition_blocks import ScopeNode
|
|
19
|
+
from smelt.analysis.ordering import FileOrder, OrderSection
|
|
20
|
+
|
|
21
|
+
_CODING = re.compile(r"coding[:=]\s*([-\w.]+)")
|
|
22
|
+
_FILE_PRAGMA = re.compile(
|
|
23
|
+
r"\s*#\s*(?:ruff:|mypy:|pyright:|flake8:|type:\s*ignore|smelt:\s*ignore-file)"
|
|
24
|
+
)
|
|
25
|
+
_ENCODING_LINES = 2
|
|
26
|
+
_FIX_BOUNDARY = re.compile(
|
|
27
|
+
r"(?m)^\s*#\s*(?:fmt:\s*(?:off|skip)\b|pylint:\s*(?:disable|enable)\b)"
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
@dataclass(frozen=True)
|
|
32
|
+
class Span:
|
|
33
|
+
start: int
|
|
34
|
+
end: int
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
@dataclass(frozen=True)
|
|
38
|
+
class Move:
|
|
39
|
+
section: OrderSection
|
|
40
|
+
parts: tuple[Span, ...]
|
|
41
|
+
|
|
42
|
+
@property
|
|
43
|
+
def span(self) -> Span:
|
|
44
|
+
return Span(self.parts[0].start, self.parts[-1].end)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def rewrite_order(source: str, result: FileOrder, sections: list[OrderSection]) -> str:
|
|
48
|
+
if not sections:
|
|
49
|
+
return source
|
|
50
|
+
if any(section.fix_reason for section in sections):
|
|
51
|
+
raise AnalysisError("an ordering section requires manual review")
|
|
52
|
+
if _FIX_BOUNDARY.search(source):
|
|
53
|
+
raise AnalysisError(
|
|
54
|
+
"formatter or suppression region prevents safe ordering fixes"
|
|
55
|
+
)
|
|
56
|
+
if any(
|
|
57
|
+
"\t" in line[: len(line) - len(line.lstrip())] for line in source.splitlines()
|
|
58
|
+
):
|
|
59
|
+
raise AnalysisError("tab indentation requires manual ordering review")
|
|
60
|
+
if ast.dump(ast.parse(source), include_attributes=False) != ast.dump(
|
|
61
|
+
result.tree, include_attributes=False
|
|
62
|
+
):
|
|
63
|
+
raise AnalysisError("source changed since ordering analysis")
|
|
64
|
+
_compile_source(source, result.path)
|
|
65
|
+
newline = "\r\n" if "\r\n" in source else "\n"
|
|
66
|
+
rewritten = _render(source, Span(0, len(source)), _moves(source, sections), newline)
|
|
67
|
+
if not source.endswith(("\n", "\r")) and rewritten.endswith(newline):
|
|
68
|
+
rewritten = rewritten[: -len(newline)]
|
|
69
|
+
try:
|
|
70
|
+
parsed = ast.parse(rewritten)
|
|
71
|
+
except SyntaxError as exc:
|
|
72
|
+
raise AnalysisError("ordering rewrite did not preserve valid source") from exc
|
|
73
|
+
expected = _expected_tree(result.tree, sections)
|
|
74
|
+
if ast.dump(parsed, include_attributes=False) != ast.dump(
|
|
75
|
+
expected, include_attributes=False
|
|
76
|
+
):
|
|
77
|
+
raise AnalysisError(
|
|
78
|
+
"ordering rewrite changed more than the approved statement order"
|
|
79
|
+
)
|
|
80
|
+
_compile_source(rewritten, result.path)
|
|
81
|
+
return rewritten
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def _header_lines(source: str, owner: ScopeNode) -> int:
|
|
85
|
+
if isinstance(owner, ast.Module):
|
|
86
|
+
first = statement_line(owner.body[0]) if owner.body else 1
|
|
87
|
+
fixed = 0
|
|
88
|
+
for index, line in enumerate(source.splitlines()[: first - 1]):
|
|
89
|
+
if (
|
|
90
|
+
(index == 0 and line.startswith("#!"))
|
|
91
|
+
or (index < _ENCODING_LINES and _CODING.search(line))
|
|
92
|
+
or _FILE_PRAGMA.match(line)
|
|
93
|
+
):
|
|
94
|
+
fixed = index + 1
|
|
95
|
+
return fixed
|
|
96
|
+
depth = 0
|
|
97
|
+
for token in tokenize.generate_tokens(io.StringIO(source).readline):
|
|
98
|
+
if token.start < (owner.lineno, owner.col_offset):
|
|
99
|
+
continue
|
|
100
|
+
if token.type == tokenize.OP:
|
|
101
|
+
if token.string in {"(", "[", "{"}:
|
|
102
|
+
depth += 1
|
|
103
|
+
elif token.string in {")", "]", "}"}:
|
|
104
|
+
depth -= 1
|
|
105
|
+
elif token.string == ":" and depth == 0:
|
|
106
|
+
return token.end[0]
|
|
107
|
+
raise AnalysisError("cannot identify the class suite for a safe ordering fix")
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def _statement_end(node: ast.stmt, lines: list[str]) -> int:
|
|
111
|
+
end = node.end_lineno or node.lineno
|
|
112
|
+
if not isinstance(node, ast.FunctionDef | ast.AsyncFunctionDef | ast.ClassDef):
|
|
113
|
+
return end
|
|
114
|
+
for index in range(end, len(lines)):
|
|
115
|
+
line = lines[index]
|
|
116
|
+
if not line.strip():
|
|
117
|
+
continue
|
|
118
|
+
indentation = len(line) - len(line.lstrip(" \t"))
|
|
119
|
+
if line.lstrip().startswith("#") and indentation > node.col_offset:
|
|
120
|
+
end = index + 1
|
|
121
|
+
else:
|
|
122
|
+
break
|
|
123
|
+
return end
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def _moves(source: str, sections: list[OrderSection]) -> list[Move]:
|
|
127
|
+
lines = source.splitlines(keepends=True)
|
|
128
|
+
offsets = [0]
|
|
129
|
+
for line in lines:
|
|
130
|
+
offsets.append(offsets[-1] + len(line))
|
|
131
|
+
scopes: dict[int, dict[int, Span]] = {}
|
|
132
|
+
moves: list[Move] = []
|
|
133
|
+
for section in sections:
|
|
134
|
+
owner = section.owner
|
|
135
|
+
if id(owner) not in scopes:
|
|
136
|
+
cursor = _header_lines(source, owner)
|
|
137
|
+
spans: dict[int, Span] = {}
|
|
138
|
+
for node in owner.body:
|
|
139
|
+
end = _statement_end(node, lines)
|
|
140
|
+
spans[id(node)] = Span(offsets[cursor], offsets[end])
|
|
141
|
+
cursor = end
|
|
142
|
+
scopes[id(owner)] = spans
|
|
143
|
+
spans = scopes[id(owner)]
|
|
144
|
+
parts = tuple(
|
|
145
|
+
Span(spans[id(block.nodes[0])].start, spans[id(block.nodes[-1])].end)
|
|
146
|
+
for block in section.blocks
|
|
147
|
+
)
|
|
148
|
+
if any(part.start >= part.end for part in parts):
|
|
149
|
+
raise AnalysisError("overlapping source lines cannot be fixed safely")
|
|
150
|
+
moves.append(Move(section, parts))
|
|
151
|
+
return moves
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def _render(source: str, span: Span, moves: list[Move], newline: str) -> str:
|
|
155
|
+
inside = [
|
|
156
|
+
move
|
|
157
|
+
for move in moves
|
|
158
|
+
if span.start <= move.span.start and move.span.end <= span.end
|
|
159
|
+
]
|
|
160
|
+
roots = [
|
|
161
|
+
move
|
|
162
|
+
for move in inside
|
|
163
|
+
if not any(
|
|
164
|
+
other is not move
|
|
165
|
+
and other.span.start <= move.span.start
|
|
166
|
+
and move.span.end <= other.span.end
|
|
167
|
+
for other in inside
|
|
168
|
+
)
|
|
169
|
+
]
|
|
170
|
+
pieces: list[str] = []
|
|
171
|
+
cursor = span.start
|
|
172
|
+
for move in sorted(roots, key=lambda item: item.span.start):
|
|
173
|
+
if move.span.start < cursor:
|
|
174
|
+
raise AnalysisError("overlapping ordering edits cannot be fixed safely")
|
|
175
|
+
pieces.append(source[cursor : move.span.start])
|
|
176
|
+
children = [
|
|
177
|
+
item
|
|
178
|
+
for item in inside
|
|
179
|
+
if item is not move
|
|
180
|
+
and move.span.start <= item.span.start
|
|
181
|
+
and item.span.end <= move.span.end
|
|
182
|
+
]
|
|
183
|
+
blocks = [
|
|
184
|
+
_render(source, move.parts[index], children, newline)
|
|
185
|
+
for index in move.section.order
|
|
186
|
+
]
|
|
187
|
+
for index, block in enumerate(blocks):
|
|
188
|
+
if (
|
|
189
|
+
index + 1 < len(blocks)
|
|
190
|
+
and block
|
|
191
|
+
and not block.endswith(("\n", "\r"))
|
|
192
|
+
and not blocks[index + 1].startswith(("\n", "\r"))
|
|
193
|
+
):
|
|
194
|
+
pieces.append(block + newline)
|
|
195
|
+
continue
|
|
196
|
+
pieces.append(block)
|
|
197
|
+
cursor = move.span.end
|
|
198
|
+
pieces.append(source[cursor : span.end])
|
|
199
|
+
return "".join(pieces)
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def _expected_tree(tree: ast.Module, sections: list[OrderSection]) -> ast.Module:
|
|
203
|
+
expected = copy.deepcopy(tree)
|
|
204
|
+
copies = {
|
|
205
|
+
id(original): cloned
|
|
206
|
+
for original, cloned in zip(ast.walk(tree), ast.walk(expected), strict=True)
|
|
207
|
+
}
|
|
208
|
+
for section in sections:
|
|
209
|
+
owner = copies[id(section.owner)]
|
|
210
|
+
if not isinstance(owner, ast.Module | ast.ClassDef):
|
|
211
|
+
raise AnalysisError("invalid ordering scope")
|
|
212
|
+
nodes = [
|
|
213
|
+
copies[id(node)]
|
|
214
|
+
for index in section.order
|
|
215
|
+
for node in section.blocks[index].nodes
|
|
216
|
+
]
|
|
217
|
+
replacement: list[ast.stmt] = []
|
|
218
|
+
for node in nodes:
|
|
219
|
+
if not isinstance(node, ast.stmt):
|
|
220
|
+
raise AnalysisError("invalid statement in an ordering rewrite")
|
|
221
|
+
replacement.append(node)
|
|
222
|
+
owner.body[section.blocks[0].first : section.blocks[-1].stop] = replacement
|
|
223
|
+
return expected
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
def _compile_source(source: str, path: str) -> None:
|
|
227
|
+
try:
|
|
228
|
+
with warnings.catch_warnings():
|
|
229
|
+
warnings.simplefilter("ignore", SyntaxWarning)
|
|
230
|
+
compile(source, path, "exec", dont_inherit=True)
|
|
231
|
+
except SyntaxError as exc:
|
|
232
|
+
raise AnalysisError(f"source cannot be safely compiled: {exc.msg}") from exc
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"""Conservative, direct public re-exports from analyzed package initializers."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import ast
|
|
6
|
+
from typing import TYPE_CHECKING
|
|
7
|
+
|
|
8
|
+
from smelt.analysis.function_usage import definition_bindings, explicit_exports
|
|
9
|
+
from smelt.analysis.parsing import resolve_relative
|
|
10
|
+
|
|
11
|
+
if TYPE_CHECKING:
|
|
12
|
+
from smelt.analysis.syntax import ModuleSyntax
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def direct_package_exports(module: ModuleSyntax) -> dict[str, str]:
|
|
16
|
+
"""Public name -> imported name, excluding conditional or rebound bindings."""
|
|
17
|
+
if not module.is_package:
|
|
18
|
+
return {}
|
|
19
|
+
exports = explicit_exports(module.tree)
|
|
20
|
+
if exports is None:
|
|
21
|
+
return {}
|
|
22
|
+
declares_all = any(
|
|
23
|
+
isinstance(node, ast.Name) and node.id == "__all__"
|
|
24
|
+
for node in ast.walk(module.tree)
|
|
25
|
+
)
|
|
26
|
+
definitions = definition_bindings(module.tree)
|
|
27
|
+
if "__getattr__" in definitions:
|
|
28
|
+
return {}
|
|
29
|
+
imports: dict[str, list[ast.AST]] = {}
|
|
30
|
+
for node in ast.walk(module.tree):
|
|
31
|
+
if isinstance(node, ast.Import | ast.ImportFrom):
|
|
32
|
+
for alias in node.names:
|
|
33
|
+
if alias.name == "*":
|
|
34
|
+
return {}
|
|
35
|
+
name = alias.asname or (
|
|
36
|
+
alias.name.split(".")[0]
|
|
37
|
+
if isinstance(node, ast.Import)
|
|
38
|
+
else alias.name
|
|
39
|
+
)
|
|
40
|
+
imports.setdefault(name, []).append(node)
|
|
41
|
+
result: dict[str, str] = {}
|
|
42
|
+
for node in module.tree.body:
|
|
43
|
+
if not isinstance(node, ast.ImportFrom):
|
|
44
|
+
continue
|
|
45
|
+
base = resolve_relative(
|
|
46
|
+
module.module, is_package=True, level=node.level, target=node.module
|
|
47
|
+
)
|
|
48
|
+
for alias in node.names:
|
|
49
|
+
name = alias.asname or alias.name
|
|
50
|
+
if (
|
|
51
|
+
name.startswith("_")
|
|
52
|
+
or name in definitions
|
|
53
|
+
or len(imports[name]) != 1
|
|
54
|
+
or (declares_all and name not in exports)
|
|
55
|
+
):
|
|
56
|
+
continue
|
|
57
|
+
result[name] = f"{base}.{alias.name}"
|
|
58
|
+
return result
|