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,217 @@
|
|
|
1
|
+
"""Bounded recognition of direct member accesses on standard StrEnum classes."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import ast
|
|
6
|
+
from collections import Counter
|
|
7
|
+
from dataclasses import dataclass
|
|
8
|
+
from typing import TYPE_CHECKING
|
|
9
|
+
|
|
10
|
+
from smelt.analysis.exports import Namespace, resolve_dotted, resolve_export
|
|
11
|
+
from smelt.analysis.parsing import resolve_relative
|
|
12
|
+
|
|
13
|
+
if TYPE_CHECKING:
|
|
14
|
+
from collections.abc import Iterator
|
|
15
|
+
|
|
16
|
+
from smelt.analysis.syntax import ModuleSyntax, SyntaxIndex
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@dataclass(frozen=True)
|
|
20
|
+
class StrEnumValueAccess:
|
|
21
|
+
module: str
|
|
22
|
+
path: str
|
|
23
|
+
node: ast.Attribute
|
|
24
|
+
enum: str
|
|
25
|
+
member: str
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def str_enum_value_accesses(syntax: SyntaxIndex) -> Iterator[StrEnumValueAccess]:
|
|
29
|
+
enums = _Enums(syntax)
|
|
30
|
+
for module in enums.modules.values():
|
|
31
|
+
namespace = enums.namespaces[module.module]
|
|
32
|
+
for node in ast.walk(module.tree):
|
|
33
|
+
if not (
|
|
34
|
+
isinstance(node, ast.Attribute)
|
|
35
|
+
and isinstance(node.ctx, ast.Load)
|
|
36
|
+
and node.attr == "value"
|
|
37
|
+
and isinstance(node.value, ast.Attribute)
|
|
38
|
+
):
|
|
39
|
+
continue
|
|
40
|
+
enum = enums.resolve(node.value.value, namespace)
|
|
41
|
+
if enum is not None and node.value.attr in enums.members.get(enum, ()):
|
|
42
|
+
yield StrEnumValueAccess(
|
|
43
|
+
module.module, module.path, node, enum, node.value.attr
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _bound_names(node: ast.AST) -> tuple[str, ...]:
|
|
48
|
+
match node:
|
|
49
|
+
case (
|
|
50
|
+
ast.Name(id=name, ctx=ast.Store() | ast.Del())
|
|
51
|
+
| ast.arg(arg=name)
|
|
52
|
+
| ast.FunctionDef(name=name)
|
|
53
|
+
| ast.AsyncFunctionDef(name=name)
|
|
54
|
+
| ast.ClassDef(name=name)
|
|
55
|
+
| ast.TypeVar(name=name)
|
|
56
|
+
| ast.ParamSpec(name=name)
|
|
57
|
+
| ast.TypeVarTuple(name=name)
|
|
58
|
+
):
|
|
59
|
+
return (name,)
|
|
60
|
+
case ast.Import() | ast.ImportFrom():
|
|
61
|
+
return tuple(
|
|
62
|
+
alias.asname or alias.name.split(".")[0] for alias in node.names
|
|
63
|
+
)
|
|
64
|
+
case (
|
|
65
|
+
ast.ExceptHandler(name=name)
|
|
66
|
+
| ast.MatchAs(name=name)
|
|
67
|
+
| ast.MatchStar(name=name)
|
|
68
|
+
| ast.MatchMapping(rest=name)
|
|
69
|
+
):
|
|
70
|
+
return (name,) if name else ()
|
|
71
|
+
case ast.Global(names=names) | ast.Nonlocal(names=names):
|
|
72
|
+
return tuple(names)
|
|
73
|
+
return ()
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _binding_counts(tree: ast.Module) -> Counter[str]:
|
|
77
|
+
bound = Counter(name for node in ast.walk(tree) for name in _bound_names(node))
|
|
78
|
+
for node in ast.walk(tree):
|
|
79
|
+
if isinstance(node, ast.Attribute | ast.Subscript) and isinstance(
|
|
80
|
+
node.ctx, ast.Store | ast.Del
|
|
81
|
+
):
|
|
82
|
+
root = node.value
|
|
83
|
+
while isinstance(root, ast.Attribute | ast.Subscript):
|
|
84
|
+
root = root.value
|
|
85
|
+
if isinstance(root, ast.Name):
|
|
86
|
+
bound[root.id] += 1
|
|
87
|
+
return bound
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def _namespace(module: ModuleSyntax) -> Namespace:
|
|
91
|
+
# A binding shadowed even in another scope is unknown. This intentionally
|
|
92
|
+
# trades some recognition for avoiding guesses about Python name resolution.
|
|
93
|
+
bound = _binding_counts(module.tree)
|
|
94
|
+
imported: dict[str, str] = {}
|
|
95
|
+
if not bound["*"]:
|
|
96
|
+
for node in module.tree.body:
|
|
97
|
+
if isinstance(node, ast.Import):
|
|
98
|
+
for alias in node.names:
|
|
99
|
+
name = alias.asname or alias.name.split(".")[0]
|
|
100
|
+
if bound[name] == 1:
|
|
101
|
+
imported[name] = alias.name if alias.asname else name
|
|
102
|
+
elif isinstance(node, ast.ImportFrom):
|
|
103
|
+
base = resolve_relative(
|
|
104
|
+
module.module,
|
|
105
|
+
is_package=module.is_package,
|
|
106
|
+
level=node.level,
|
|
107
|
+
target=node.module,
|
|
108
|
+
)
|
|
109
|
+
for alias in node.names:
|
|
110
|
+
name = alias.asname or alias.name
|
|
111
|
+
if bound[name] == 1 and name != "*":
|
|
112
|
+
imported[name] = f"{base}.{alias.name}"
|
|
113
|
+
# Block fallback resolution to a submodule for uncertain/rebound names.
|
|
114
|
+
return Namespace(module.module, imported, frozenset(bound.keys() - imported.keys()))
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
class _Enums:
|
|
118
|
+
def __init__(self, syntax: SyntaxIndex) -> None:
|
|
119
|
+
self.modules = {
|
|
120
|
+
source.module: module
|
|
121
|
+
for source in sorted(
|
|
122
|
+
syntax.files.sources.values(), key=lambda item: item.path
|
|
123
|
+
)
|
|
124
|
+
if (module := syntax.for_path(source.path)) is not None
|
|
125
|
+
}
|
|
126
|
+
self.namespaces = {
|
|
127
|
+
name: _namespace(module) for name, module in self.modules.items()
|
|
128
|
+
}
|
|
129
|
+
for package in syntax.files.packages:
|
|
130
|
+
self.namespaces.setdefault(package, Namespace(package))
|
|
131
|
+
self.namespaces.setdefault(
|
|
132
|
+
"enum", Namespace("enum", defined=frozenset({"StrEnum", "auto"}))
|
|
133
|
+
)
|
|
134
|
+
self.members: dict[str, frozenset[str]] = {}
|
|
135
|
+
for module in self.modules.values():
|
|
136
|
+
bound = _binding_counts(module.tree)
|
|
137
|
+
if bound["*"]:
|
|
138
|
+
continue
|
|
139
|
+
for node in module.tree.body:
|
|
140
|
+
if isinstance(node, ast.ClassDef) and bound[node.name] == 1:
|
|
141
|
+
self.members[f"{module.module}.{node.name}"] = self._members(
|
|
142
|
+
node, self.namespaces[module.module]
|
|
143
|
+
)
|
|
144
|
+
|
|
145
|
+
def resolve(self, node: ast.expr, namespace: Namespace) -> str | None:
|
|
146
|
+
resolved = self._target(node, namespace)
|
|
147
|
+
if resolved is None or resolved[1] is None:
|
|
148
|
+
return None
|
|
149
|
+
return f"{resolved[0]}.{resolved[1]}"
|
|
150
|
+
|
|
151
|
+
def _target(
|
|
152
|
+
self, node: ast.expr, namespace: Namespace
|
|
153
|
+
) -> tuple[str, str | None] | None:
|
|
154
|
+
if isinstance(node, ast.Name):
|
|
155
|
+
qualified = namespace.imported.get(node.id, f"{namespace.module}.{node.id}")
|
|
156
|
+
return resolve_dotted(qualified, self.namespaces.get)
|
|
157
|
+
if isinstance(node, ast.Attribute):
|
|
158
|
+
base = self._target(node.value, namespace)
|
|
159
|
+
# Attributes of an already resolved class/member are unknown, rather
|
|
160
|
+
# than another spelling of that class.
|
|
161
|
+
if base is not None and base[1] is None:
|
|
162
|
+
return resolve_export(base[0], node.attr, self.namespaces.get)
|
|
163
|
+
return None
|
|
164
|
+
|
|
165
|
+
def _members(self, node: ast.ClassDef, namespace: Namespace) -> frozenset[str]:
|
|
166
|
+
if (
|
|
167
|
+
len(node.bases) != 1
|
|
168
|
+
or self.resolve(node.bases[0], namespace) != "enum.StrEnum"
|
|
169
|
+
or "enum" in self.modules
|
|
170
|
+
or node.keywords
|
|
171
|
+
or node.decorator_list
|
|
172
|
+
):
|
|
173
|
+
return frozenset()
|
|
174
|
+
if any(
|
|
175
|
+
name
|
|
176
|
+
in {
|
|
177
|
+
"value",
|
|
178
|
+
"_ignore_",
|
|
179
|
+
"__new__",
|
|
180
|
+
"__init__",
|
|
181
|
+
"__getattribute__",
|
|
182
|
+
"__getattr__",
|
|
183
|
+
}
|
|
184
|
+
for child in node.body
|
|
185
|
+
for binding in (
|
|
186
|
+
ast.walk(child)
|
|
187
|
+
if isinstance(child, ast.Assign | ast.AnnAssign | ast.AugAssign)
|
|
188
|
+
else (child,)
|
|
189
|
+
)
|
|
190
|
+
for name in _bound_names(binding)
|
|
191
|
+
):
|
|
192
|
+
return frozenset()
|
|
193
|
+
members: set[str] = set()
|
|
194
|
+
value: ast.expr | None
|
|
195
|
+
for child in node.body:
|
|
196
|
+
if isinstance(child, ast.Assign):
|
|
197
|
+
targets, value = child.targets, child.value
|
|
198
|
+
elif isinstance(child, ast.AnnAssign):
|
|
199
|
+
targets, value = [child.target], child.value
|
|
200
|
+
else:
|
|
201
|
+
continue
|
|
202
|
+
if (
|
|
203
|
+
(isinstance(value, ast.Constant) and isinstance(value.value, str))
|
|
204
|
+
or (isinstance(value, ast.Name) and value.id in members)
|
|
205
|
+
or (
|
|
206
|
+
isinstance(value, ast.Call)
|
|
207
|
+
and not value.args
|
|
208
|
+
and not value.keywords
|
|
209
|
+
and self.resolve(value.func, namespace) == "enum.auto"
|
|
210
|
+
)
|
|
211
|
+
):
|
|
212
|
+
members.update(
|
|
213
|
+
target.id
|
|
214
|
+
for target in targets
|
|
215
|
+
if isinstance(target, ast.Name) and not target.id.startswith("_")
|
|
216
|
+
)
|
|
217
|
+
return frozenset(members)
|
smelt/analysis/errors.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
class AnalysisError(Exception):
|
|
2
|
+
"""The project cannot be analyzed (missing packages, unreadable source, ...)."""
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class InvalidOptionError(AnalysisError):
|
|
6
|
+
"""A check option names something that does not exist; a usage error."""
|
|
7
|
+
|
|
8
|
+
def __init__(self, message: str, *, option: str, value: str) -> None:
|
|
9
|
+
super().__init__(message)
|
|
10
|
+
self.option = option
|
|
11
|
+
self.value = value
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"""Where a name that a module exposes is defined, following package re-exports.
|
|
2
|
+
|
|
3
|
+
``from app.billing import Invoice`` names ``app.billing``, but ``Invoice`` lives in
|
|
4
|
+
``app.billing.models`` when the facade ``app/billing/__init__.py`` re-exports it.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import ast
|
|
10
|
+
from dataclasses import dataclass, field
|
|
11
|
+
from typing import TYPE_CHECKING
|
|
12
|
+
|
|
13
|
+
from smelt.analysis.parsing import resolve_relative
|
|
14
|
+
|
|
15
|
+
if TYPE_CHECKING:
|
|
16
|
+
from collections.abc import Callable, Iterator
|
|
17
|
+
|
|
18
|
+
# Re-export chains are short; a longer one is more likely a cycle than a design.
|
|
19
|
+
MAX_HOPS = 8
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@dataclass(frozen=True, slots=True)
|
|
23
|
+
class Namespace:
|
|
24
|
+
"""The names a module binds at top level."""
|
|
25
|
+
|
|
26
|
+
module: str
|
|
27
|
+
# local name -> the absolute name it was imported as
|
|
28
|
+
imported: dict[str, str] = field(default_factory=dict)
|
|
29
|
+
defined: frozenset[str] = frozenset()
|
|
30
|
+
# defined name -> dotted names its value refers to: ``("chat", "feature")``
|
|
31
|
+
references: dict[str, tuple[tuple[str, ...], ...]] = field(default_factory=dict)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
type Loader = Callable[[str], "Namespace | None"]
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def namespace_of(tree: ast.Module, module: str, *, is_package: bool) -> Namespace:
|
|
38
|
+
imported: dict[str, str] = {}
|
|
39
|
+
defined: set[str] = set()
|
|
40
|
+
references: dict[str, tuple[tuple[str, ...], ...]] = {}
|
|
41
|
+
for node in _top_level(tree.body):
|
|
42
|
+
if isinstance(node, ast.Import):
|
|
43
|
+
for alias in node.names:
|
|
44
|
+
name = alias.asname or alias.name.split(".")[0]
|
|
45
|
+
imported[name] = alias.name if alias.asname else name
|
|
46
|
+
defined.discard(name)
|
|
47
|
+
references.pop(name, None)
|
|
48
|
+
elif isinstance(node, ast.ImportFrom):
|
|
49
|
+
base = resolve_relative(
|
|
50
|
+
module, is_package=is_package, level=node.level, target=node.module
|
|
51
|
+
)
|
|
52
|
+
for alias in node.names:
|
|
53
|
+
if alias.name != "*":
|
|
54
|
+
qualified = f"{base}.{alias.name}" if base else alias.name
|
|
55
|
+
name = alias.asname or alias.name
|
|
56
|
+
imported[name] = qualified
|
|
57
|
+
defined.discard(name)
|
|
58
|
+
references.pop(name, None)
|
|
59
|
+
else:
|
|
60
|
+
for name in _defined_names(node):
|
|
61
|
+
defined.add(name)
|
|
62
|
+
imported.pop(name, None)
|
|
63
|
+
references[name] = tuple(_dotted_names(node))
|
|
64
|
+
return Namespace(module, imported, frozenset(defined), references)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def resolve_export(
|
|
68
|
+
module: str, name: str, load: Loader
|
|
69
|
+
) -> tuple[str, str | None] | None:
|
|
70
|
+
"""The module defining ``module.name`` and its name there.
|
|
71
|
+
|
|
72
|
+
``(submodule, None)`` when the name is a module itself; None for names outside
|
|
73
|
+
the loader's modules, star imports, and chains that cycle or run too long.
|
|
74
|
+
"""
|
|
75
|
+
seen: set[tuple[str, str]] = set()
|
|
76
|
+
while len(seen) < MAX_HOPS and (module, name) not in seen:
|
|
77
|
+
seen.add((module, name))
|
|
78
|
+
namespace = load(module)
|
|
79
|
+
if namespace is None:
|
|
80
|
+
return None
|
|
81
|
+
if name in namespace.defined:
|
|
82
|
+
return module, name
|
|
83
|
+
target = namespace.imported.get(name) or f"{module}.{name}"
|
|
84
|
+
if load(target) is not None:
|
|
85
|
+
return target, None
|
|
86
|
+
if target == f"{module}.{name}":
|
|
87
|
+
return None
|
|
88
|
+
module, _, name = target.rpartition(".")
|
|
89
|
+
if not module:
|
|
90
|
+
return None
|
|
91
|
+
return None
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def resolve_dotted(qualified: str, load: Loader) -> tuple[str, str | None] | None:
|
|
95
|
+
"""Like :func:`resolve_export` for a dotted name such as ``app.billing.Invoice``.
|
|
96
|
+
|
|
97
|
+
The longest module prefix short of the last segment is imported as a module;
|
|
98
|
+
the remaining segments are attributes, as in ``from app.billing import
|
|
99
|
+
Invoice``, so a facade binding wins over a submodule of the same name. Access
|
|
100
|
+
on anything but a module stops at that object, which is where it is defined.
|
|
101
|
+
"""
|
|
102
|
+
parts = qualified.split(".")
|
|
103
|
+
end = next(
|
|
104
|
+
(
|
|
105
|
+
end
|
|
106
|
+
for end in range(len(parts) - 1, 0, -1)
|
|
107
|
+
if load(".".join(parts[:end])) is not None
|
|
108
|
+
),
|
|
109
|
+
None,
|
|
110
|
+
)
|
|
111
|
+
if end is None:
|
|
112
|
+
return (qualified, None) if load(qualified) is not None else None
|
|
113
|
+
current: tuple[str, str | None] | None = (".".join(parts[:end]), None)
|
|
114
|
+
for attribute in parts[end:]:
|
|
115
|
+
if current is None or current[1] is not None:
|
|
116
|
+
break
|
|
117
|
+
current = resolve_export(current[0], attribute, load)
|
|
118
|
+
return current
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def _top_level(body: list[ast.stmt]) -> Iterator[ast.stmt]:
|
|
122
|
+
"""Statements at module level, including those under ``if`` and ``try``."""
|
|
123
|
+
for node in body:
|
|
124
|
+
if isinstance(node, ast.If):
|
|
125
|
+
yield from _top_level(node.body)
|
|
126
|
+
yield from _top_level(node.orelse)
|
|
127
|
+
elif isinstance(node, ast.Try):
|
|
128
|
+
for block in (node.body, node.orelse, node.finalbody):
|
|
129
|
+
yield from _top_level(block)
|
|
130
|
+
for handler in node.handlers:
|
|
131
|
+
yield from _top_level(handler.body)
|
|
132
|
+
else:
|
|
133
|
+
yield node
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def _defined_names(node: ast.stmt) -> list[str]:
|
|
137
|
+
if isinstance(node, ast.FunctionDef | ast.AsyncFunctionDef | ast.ClassDef):
|
|
138
|
+
return [node.name]
|
|
139
|
+
targets: list[ast.expr] = []
|
|
140
|
+
if isinstance(node, ast.Assign):
|
|
141
|
+
targets = node.targets
|
|
142
|
+
elif isinstance(node, ast.AnnAssign | ast.AugAssign):
|
|
143
|
+
targets = [node.target]
|
|
144
|
+
elif isinstance(node, ast.TypeAlias):
|
|
145
|
+
targets = [node.name]
|
|
146
|
+
return [
|
|
147
|
+
name.id
|
|
148
|
+
for target in targets
|
|
149
|
+
for name in ast.walk(target)
|
|
150
|
+
if isinstance(name, ast.Name)
|
|
151
|
+
]
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def _dotted_names(node: ast.stmt) -> Iterator[tuple[str, ...]]:
|
|
155
|
+
"""``chat.feature`` -> ``("chat", "feature")`` for every name the value reads."""
|
|
156
|
+
for child in ast.walk(node):
|
|
157
|
+
if isinstance(child, ast.Attribute):
|
|
158
|
+
parts: list[str] = []
|
|
159
|
+
current: ast.expr = child
|
|
160
|
+
while isinstance(current, ast.Attribute):
|
|
161
|
+
parts.append(current.attr)
|
|
162
|
+
current = current.value
|
|
163
|
+
if isinstance(current, ast.Name):
|
|
164
|
+
yield (current.id, *reversed(parts))
|
|
165
|
+
elif isinstance(child, ast.Name) and isinstance(child.ctx, ast.Load):
|
|
166
|
+
yield (child.id,)
|
smelt/analysis/files.py
ADDED
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import hashlib
|
|
4
|
+
import os
|
|
5
|
+
from dataclasses import dataclass, field
|
|
6
|
+
from functools import cached_property
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from typing import TYPE_CHECKING
|
|
9
|
+
|
|
10
|
+
from smelt.analysis.errors import AnalysisError
|
|
11
|
+
from smelt.config.patterns import path_matches
|
|
12
|
+
from smelt.config.source import SourceStore
|
|
13
|
+
|
|
14
|
+
if TYPE_CHECKING:
|
|
15
|
+
from collections.abc import Iterable, Iterator
|
|
16
|
+
|
|
17
|
+
from smelt.config.models import SmeltConfig
|
|
18
|
+
|
|
19
|
+
_SKIP_DIRS = frozenset({"__pycache__", ".git", ".venv", "venv", "node_modules"})
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@dataclass(frozen=True, slots=True)
|
|
23
|
+
class SourceFile:
|
|
24
|
+
module: str
|
|
25
|
+
path: str # POSIX, relative to the project root
|
|
26
|
+
absolute: Path
|
|
27
|
+
is_package: bool # an ``__init__.py``
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@dataclass(frozen=True, slots=True)
|
|
31
|
+
class TestFile:
|
|
32
|
+
path: str
|
|
33
|
+
absolute: Path
|
|
34
|
+
test_root: str
|
|
35
|
+
|
|
36
|
+
@property
|
|
37
|
+
def name(self) -> str:
|
|
38
|
+
return self.path.rsplit("/", 1)[-1]
|
|
39
|
+
|
|
40
|
+
@property
|
|
41
|
+
def is_conftest(self) -> bool:
|
|
42
|
+
return self.name == "conftest.py"
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
@dataclass(slots=True)
|
|
46
|
+
class PackageDir:
|
|
47
|
+
module: str
|
|
48
|
+
path: str
|
|
49
|
+
source_root: str
|
|
50
|
+
has_init: bool
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
@dataclass
|
|
54
|
+
class FileIndex:
|
|
55
|
+
root: Path
|
|
56
|
+
config: SmeltConfig
|
|
57
|
+
sources: dict[str, SourceFile] = field(default_factory=dict)
|
|
58
|
+
packages: dict[str, PackageDir] = field(default_factory=dict)
|
|
59
|
+
package_parts: dict[str, list[PackageDir]] = field(default_factory=dict)
|
|
60
|
+
package_roots: dict[str, list[Path]] = field(default_factory=dict)
|
|
61
|
+
tests: dict[str, TestFile] = field(default_factory=dict)
|
|
62
|
+
missing_root_packages: list[str] = field(default_factory=list)
|
|
63
|
+
store: SourceStore = field(default_factory=SourceStore, repr=False)
|
|
64
|
+
_text_cache: dict[str, str] = field(default_factory=dict, repr=False)
|
|
65
|
+
_line_cache: dict[str, list[str]] = field(default_factory=dict, repr=False)
|
|
66
|
+
_content_ids: dict[str, str] = field(default_factory=dict, repr=False)
|
|
67
|
+
_resolved_paths: dict[Path, Path] = field(default_factory=dict, repr=False)
|
|
68
|
+
|
|
69
|
+
def __post_init__(self) -> None:
|
|
70
|
+
self.root = self.store.resolve(self.root)
|
|
71
|
+
|
|
72
|
+
@classmethod
|
|
73
|
+
def discover(
|
|
74
|
+
cls, root: Path, config: SmeltConfig, *, store: SourceStore | None = None
|
|
75
|
+
) -> FileIndex:
|
|
76
|
+
index = cls(root=root, config=config, store=store or SourceStore())
|
|
77
|
+
index._discover_sources()
|
|
78
|
+
index._discover_tests()
|
|
79
|
+
return index
|
|
80
|
+
|
|
81
|
+
def is_excluded(self, path: str) -> bool:
|
|
82
|
+
return any(
|
|
83
|
+
path_matches(pattern, path)
|
|
84
|
+
for pattern in self.config.project.exclude_patterns
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
def relative(self, path: Path) -> str:
|
|
88
|
+
absolute = self.resolve_path(path)
|
|
89
|
+
try:
|
|
90
|
+
return absolute.relative_to(self.root).as_posix()
|
|
91
|
+
except ValueError:
|
|
92
|
+
try:
|
|
93
|
+
return Path(os.path.relpath(absolute, self.root)).as_posix()
|
|
94
|
+
except ValueError: # different Windows drives
|
|
95
|
+
return absolute.as_posix()
|
|
96
|
+
|
|
97
|
+
def resolve_path(self, path: Path) -> Path:
|
|
98
|
+
resolved = self._resolved_paths.get(path)
|
|
99
|
+
if resolved is None:
|
|
100
|
+
resolved = self.store.resolve(path)
|
|
101
|
+
self._resolved_paths[path] = resolved
|
|
102
|
+
return resolved
|
|
103
|
+
|
|
104
|
+
def _discover_sources(self) -> None:
|
|
105
|
+
project = self.config.project
|
|
106
|
+
for package in project.root_packages:
|
|
107
|
+
candidates = {
|
|
108
|
+
self.resolve_path(self.root / source_root / package): source_root
|
|
109
|
+
for source_root in project.source_roots
|
|
110
|
+
if self.store.is_dir(self.root / source_root / package)
|
|
111
|
+
and not self.is_excluded(
|
|
112
|
+
self.relative(self.root / source_root / package)
|
|
113
|
+
)
|
|
114
|
+
}
|
|
115
|
+
regular = {
|
|
116
|
+
path: source_root
|
|
117
|
+
for path, source_root in candidates.items()
|
|
118
|
+
if self.store.is_file(path / "__init__.py")
|
|
119
|
+
}
|
|
120
|
+
# Python gives an ordinary package precedence over namespace portions,
|
|
121
|
+
# including workspace scaffolding found beneath a broad '.' root.
|
|
122
|
+
selected = regular or candidates
|
|
123
|
+
if len(regular) > 1:
|
|
124
|
+
paths = ", ".join(self.relative(path) for path in regular)
|
|
125
|
+
raise AnalysisError(f"ambiguous package {package}: {paths}")
|
|
126
|
+
self.package_roots[package] = list(selected)
|
|
127
|
+
for directory, source_root in selected.items():
|
|
128
|
+
self._walk_package(directory, package, source_root)
|
|
129
|
+
if not selected:
|
|
130
|
+
self.missing_root_packages.append(package)
|
|
131
|
+
|
|
132
|
+
def _walk_package(self, directory: Path, module: str, source_root: str) -> None:
|
|
133
|
+
rel_dir = self.relative(directory)
|
|
134
|
+
if self.is_excluded(rel_dir):
|
|
135
|
+
return
|
|
136
|
+
init = directory / "__init__.py"
|
|
137
|
+
portion = PackageDir(module, rel_dir, source_root, self.store.is_file(init))
|
|
138
|
+
previous = self.packages.get(module)
|
|
139
|
+
if previous is not None and (previous.has_init or portion.has_init):
|
|
140
|
+
raise AnalysisError(
|
|
141
|
+
f"ambiguous package {module}: {previous.path}, {rel_dir}"
|
|
142
|
+
)
|
|
143
|
+
self.packages.setdefault(module, portion)
|
|
144
|
+
self.package_parts.setdefault(module, []).append(portion)
|
|
145
|
+
for entry in sorted(self.store.iterdir(directory), key=lambda p: p.name):
|
|
146
|
+
if self.store.is_dir(entry):
|
|
147
|
+
if entry.name in _SKIP_DIRS or not entry.name.isidentifier():
|
|
148
|
+
continue
|
|
149
|
+
if _contains_python(entry, store=self.store):
|
|
150
|
+
self._walk_package(entry, f"{module}.{entry.name}", source_root)
|
|
151
|
+
elif entry.suffix == ".py" and self.store.is_file(entry):
|
|
152
|
+
rel = self.relative(entry)
|
|
153
|
+
if self.is_excluded(rel):
|
|
154
|
+
continue
|
|
155
|
+
if entry.name == "__init__.py":
|
|
156
|
+
self._add_source(SourceFile(module, rel, entry, is_package=True))
|
|
157
|
+
elif entry.stem.isidentifier():
|
|
158
|
+
name = f"{module}.{entry.stem}"
|
|
159
|
+
self._add_source(SourceFile(name, rel, entry, is_package=False))
|
|
160
|
+
|
|
161
|
+
def _add_source(self, source: SourceFile) -> None:
|
|
162
|
+
previous = self.sources.get(source.module)
|
|
163
|
+
if previous is not None:
|
|
164
|
+
raise AnalysisError(
|
|
165
|
+
f"ambiguous module {source.module}: {previous.path}, {source.path}"
|
|
166
|
+
)
|
|
167
|
+
self.sources[source.module] = source
|
|
168
|
+
|
|
169
|
+
def _discover_tests(self) -> None:
|
|
170
|
+
source_paths = {source.path for source in self.sources.values()}
|
|
171
|
+
for test_root in self.config.project.test_roots:
|
|
172
|
+
directory = self.root / test_root
|
|
173
|
+
if not self.store.is_dir(directory):
|
|
174
|
+
continue
|
|
175
|
+
for path in sorted(python_files(directory, store=self.store)):
|
|
176
|
+
rel = self.relative(path)
|
|
177
|
+
if rel in source_paths or self.is_excluded(rel):
|
|
178
|
+
continue
|
|
179
|
+
name = path.name
|
|
180
|
+
if (
|
|
181
|
+
name == "conftest.py"
|
|
182
|
+
or name.startswith("test_")
|
|
183
|
+
or path.stem.endswith("_test")
|
|
184
|
+
):
|
|
185
|
+
self.tests[rel] = TestFile(rel, path, test_root.strip("/"))
|
|
186
|
+
|
|
187
|
+
@cached_property
|
|
188
|
+
def by_path(self) -> dict[str, SourceFile]:
|
|
189
|
+
return {source.path: source for source in self.sources.values()}
|
|
190
|
+
|
|
191
|
+
def source_for_path(self, path: str) -> SourceFile | None:
|
|
192
|
+
return self.by_path.get(path)
|
|
193
|
+
|
|
194
|
+
def path_for_module(self, module: str) -> str | None:
|
|
195
|
+
source = self.sources.get(module)
|
|
196
|
+
if source is not None:
|
|
197
|
+
return source.path
|
|
198
|
+
package = self.packages.get(module)
|
|
199
|
+
return package.path if package else None
|
|
200
|
+
|
|
201
|
+
def read_text(self, path: str) -> str:
|
|
202
|
+
cached = self._text_cache.get(path)
|
|
203
|
+
if cached is None:
|
|
204
|
+
# Python accepts a UTF-8 BOM, commonly written by Windows editors.
|
|
205
|
+
try:
|
|
206
|
+
cached = self.store.read_text(self.root / path, encoding="utf-8-sig")
|
|
207
|
+
except (OSError, UnicodeError) as exc:
|
|
208
|
+
raise AnalysisError(f"cannot read source {path}: {exc}") from exc
|
|
209
|
+
self._text_cache[path] = cached
|
|
210
|
+
return cached
|
|
211
|
+
|
|
212
|
+
def lines(self, path: str) -> list[str]:
|
|
213
|
+
if path not in self._line_cache:
|
|
214
|
+
self._line_cache[path] = self.read_text(path).splitlines()
|
|
215
|
+
return self._line_cache[path]
|
|
216
|
+
|
|
217
|
+
def line(self, path: str, number: int) -> str:
|
|
218
|
+
lines = self.lines(path)
|
|
219
|
+
return lines[number - 1] if 0 < number <= len(lines) else ""
|
|
220
|
+
|
|
221
|
+
def sha256(self, path: str) -> str:
|
|
222
|
+
return hashlib.sha256(self.read_text(path).encode()).hexdigest()
|
|
223
|
+
|
|
224
|
+
def content_id(self, path: str) -> str:
|
|
225
|
+
if path not in self._content_ids:
|
|
226
|
+
self._content_ids[path] = self.store.identity(
|
|
227
|
+
self.root / path
|
|
228
|
+
) or self.sha256(path)
|
|
229
|
+
return self._content_ids[path]
|
|
230
|
+
|
|
231
|
+
def size(self, path: str) -> int:
|
|
232
|
+
return len(self.read_text(path).encode())
|
|
233
|
+
|
|
234
|
+
def all_python_paths(self) -> Iterator[str]:
|
|
235
|
+
yield from (source.path for source in self.sources.values())
|
|
236
|
+
yield from self.tests
|
|
237
|
+
|
|
238
|
+
def source_root_dir(self, module: str) -> str | None:
|
|
239
|
+
parts = module.split(".")
|
|
240
|
+
for end in range(len(parts), 0, -1):
|
|
241
|
+
package = self.packages.get(".".join(parts[:end]))
|
|
242
|
+
if package is not None:
|
|
243
|
+
return package.source_root
|
|
244
|
+
return None
|
|
245
|
+
|
|
246
|
+
def module_to_path(self, module: str, *, package: bool = False) -> str:
|
|
247
|
+
"""The expected repo-relative path for ``module``, whether or not it exists."""
|
|
248
|
+
root = (
|
|
249
|
+
self.source_root_dir(module)
|
|
250
|
+
or (self.config.project.source_roots or ["."])[0]
|
|
251
|
+
)
|
|
252
|
+
parts = [p for p in root.strip("/").split("/") if p and p != "."]
|
|
253
|
+
parts.extend(module.split("."))
|
|
254
|
+
return "/".join(parts) + ("/" if package else ".py")
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
def python_files(
|
|
258
|
+
directory: Path, *, store: SourceStore | None = None
|
|
259
|
+
) -> Iterable[Path]:
|
|
260
|
+
"""Python files below a root, skipping tool directories and symlink cycles."""
|
|
261
|
+
store = store or SourceStore()
|
|
262
|
+
visited: set[Path] = set()
|
|
263
|
+
pending = [directory]
|
|
264
|
+
while pending:
|
|
265
|
+
parent = pending.pop()
|
|
266
|
+
resolved = store.resolve(parent)
|
|
267
|
+
if resolved in visited:
|
|
268
|
+
continue
|
|
269
|
+
visited.add(resolved)
|
|
270
|
+
for entry in store.iterdir(parent):
|
|
271
|
+
if store.is_dir(entry):
|
|
272
|
+
if entry.name not in _SKIP_DIRS and not entry.name.startswith("."):
|
|
273
|
+
pending.append(entry)
|
|
274
|
+
elif entry.suffix == ".py":
|
|
275
|
+
yield entry
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
def _contains_python(directory: Path, *, store: SourceStore | None = None) -> bool:
|
|
279
|
+
return any(True for _ in python_files(directory, store=store))
|