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,126 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import ast
|
|
4
|
+
import re
|
|
5
|
+
import sys
|
|
6
|
+
import tomllib
|
|
7
|
+
from dataclasses import dataclass, field
|
|
8
|
+
from typing import TYPE_CHECKING
|
|
9
|
+
|
|
10
|
+
from smelt.analysis.errors import AnalysisError
|
|
11
|
+
from smelt.config.source import SourceStore
|
|
12
|
+
|
|
13
|
+
__all__ = [
|
|
14
|
+
"AnalysisError",
|
|
15
|
+
"AstCache",
|
|
16
|
+
"is_stdlib",
|
|
17
|
+
"python_note",
|
|
18
|
+
"resolve_relative",
|
|
19
|
+
"type_checking_lines",
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
if TYPE_CHECKING:
|
|
23
|
+
from pathlib import Path
|
|
24
|
+
|
|
25
|
+
from smelt.analysis.cache import FactCache
|
|
26
|
+
from smelt.analysis.files import FileIndex
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@dataclass
|
|
30
|
+
class AstCache:
|
|
31
|
+
files: FileIndex
|
|
32
|
+
cache: FactCache | None = None
|
|
33
|
+
_trees: dict[str, ast.Module] = field(default_factory=dict)
|
|
34
|
+
|
|
35
|
+
def parse(self, path: str) -> ast.Module:
|
|
36
|
+
tree = self._trees.get(path)
|
|
37
|
+
if tree is None:
|
|
38
|
+
source = self.files.read_text(path)
|
|
39
|
+
try:
|
|
40
|
+
tree = ast.parse(source, filename=path)
|
|
41
|
+
except SyntaxError as exc:
|
|
42
|
+
msg = f"{path}:{exc.lineno}: syntax error: {exc.msg}"
|
|
43
|
+
raise AnalysisError(
|
|
44
|
+
msg + python_note(self.files.root, store=self.files.store)
|
|
45
|
+
) from exc
|
|
46
|
+
self._trees[path] = tree
|
|
47
|
+
return tree
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def is_stdlib(module: str) -> bool:
|
|
51
|
+
return module.split(".", maxsplit=1)[0] in sys.stdlib_module_names
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
_VERSION_BOUND = re.compile(r"(?:>=|~=|==)\s*3\.(\d+)")
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def python_note(root: Path, *, store: SourceStore | None = None) -> str:
|
|
58
|
+
"""Explain a syntax error that may just be newer syntax than the running Python.
|
|
59
|
+
|
|
60
|
+
``ast`` only knows the grammar of the interpreter smelt runs on, so a 3.14
|
|
61
|
+
project checked by smelt on 3.12 fails on ``except A, B:`` or t-strings.
|
|
62
|
+
"""
|
|
63
|
+
running = sys.version_info[:2]
|
|
64
|
+
note = f" (parsed by Python {running[0]}.{running[1]})"
|
|
65
|
+
target = _target_python(root, store=store or SourceStore())
|
|
66
|
+
if target is not None and target > running:
|
|
67
|
+
version = f"{target[0]}.{target[1]}"
|
|
68
|
+
note += (
|
|
69
|
+
f"; the project targets Python {version}, so run smelt on it, "
|
|
70
|
+
f"e.g. `uvx -p {version} --from smelt-cli smelt check`"
|
|
71
|
+
)
|
|
72
|
+
return note
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def type_checking_lines(tree: ast.Module) -> frozenset[int]:
|
|
76
|
+
"""Line numbers inside ``if TYPE_CHECKING:`` blocks."""
|
|
77
|
+
lines: set[int] = set()
|
|
78
|
+
for node in ast.walk(tree):
|
|
79
|
+
if isinstance(node, ast.If) and _is_type_checking_test(node.test):
|
|
80
|
+
for child in node.body:
|
|
81
|
+
end = child.end_lineno or child.lineno
|
|
82
|
+
lines.update(range(child.lineno, end + 1))
|
|
83
|
+
return frozenset(lines)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def resolve_relative(
|
|
87
|
+
module: str, *, is_package: bool, level: int, target: str | None
|
|
88
|
+
) -> str:
|
|
89
|
+
"""Resolve ``from <level dots><target> import ...`` inside ``module``."""
|
|
90
|
+
if level == 0:
|
|
91
|
+
return target or ""
|
|
92
|
+
parts = module.split(".")
|
|
93
|
+
if not is_package:
|
|
94
|
+
parts = parts[:-1]
|
|
95
|
+
if level > 1:
|
|
96
|
+
parts = parts[: len(parts) - (level - 1)]
|
|
97
|
+
base = ".".join(parts)
|
|
98
|
+
if target:
|
|
99
|
+
return f"{base}.{target}" if base else target
|
|
100
|
+
return base
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _target_python(root: Path, *, store: SourceStore) -> tuple[int, int] | None:
|
|
104
|
+
found: list[tuple[int, int]] = []
|
|
105
|
+
try:
|
|
106
|
+
data = tomllib.loads(store.read_text(root / "pyproject.toml"))
|
|
107
|
+
except (OSError, tomllib.TOMLDecodeError):
|
|
108
|
+
data = {}
|
|
109
|
+
requires = data.get("project", {}).get("requires-python", "")
|
|
110
|
+
if isinstance(requires, str) and (match := _VERSION_BOUND.search(requires)):
|
|
111
|
+
found.append((3, int(match.group(1))))
|
|
112
|
+
try:
|
|
113
|
+
pinned = store.read_text(root / ".python-version").split()
|
|
114
|
+
except OSError:
|
|
115
|
+
pinned = []
|
|
116
|
+
if pinned and (match := re.match(r"3\.(\d+)", pinned[0])):
|
|
117
|
+
found.append((3, int(match.group(1))))
|
|
118
|
+
return max(found, default=None)
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def _is_type_checking_test(test: ast.expr) -> bool:
|
|
122
|
+
if isinstance(test, ast.Name):
|
|
123
|
+
return test.id == "TYPE_CHECKING"
|
|
124
|
+
if isinstance(test, ast.Attribute):
|
|
125
|
+
return test.attr == "TYPE_CHECKING"
|
|
126
|
+
return False
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
"""Declared minimum Python versions, with per-project provenance and no execution."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import tomllib
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
from typing import TYPE_CHECKING, Any
|
|
8
|
+
|
|
9
|
+
from packaging.specifiers import InvalidSpecifier, SpecifierSet
|
|
10
|
+
from packaging.version import InvalidVersion, Version
|
|
11
|
+
|
|
12
|
+
if TYPE_CHECKING:
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
|
|
15
|
+
from smelt.analysis.files import FileIndex
|
|
16
|
+
|
|
17
|
+
_DEFERRED_ANNOTATIONS = (3, 14)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass(frozen=True)
|
|
21
|
+
class PythonTarget:
|
|
22
|
+
minimum: tuple[int, int] | None
|
|
23
|
+
source: str | None
|
|
24
|
+
requires_python: str | None = None
|
|
25
|
+
uncertainty: str | None = None
|
|
26
|
+
|
|
27
|
+
@property
|
|
28
|
+
def deferred_annotations(self) -> bool:
|
|
29
|
+
return (
|
|
30
|
+
self.minimum is not None
|
|
31
|
+
and self.minimum[0] == _DEFERRED_ANNOTATIONS[0]
|
|
32
|
+
and self.minimum >= _DEFERRED_ANNOTATIONS
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
def to_json(self) -> dict[str, Any]:
|
|
36
|
+
return {
|
|
37
|
+
"minimum": ".".join(map(str, self.minimum)) if self.minimum else None,
|
|
38
|
+
"source": self.source,
|
|
39
|
+
"requires_python": self.requires_python,
|
|
40
|
+
"uncertainty": self.uncertainty,
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class PythonTargetIndex:
|
|
45
|
+
def __init__(self, files: FileIndex) -> None:
|
|
46
|
+
self.files = files
|
|
47
|
+
self._cache: dict[Path, PythonTarget] = {}
|
|
48
|
+
|
|
49
|
+
def for_path(self, path: Path | None = None) -> PythonTarget:
|
|
50
|
+
override = self.files.config.project.python_version
|
|
51
|
+
if override is not None:
|
|
52
|
+
major, minor = override.split(".")
|
|
53
|
+
return PythonTarget((int(major), int(minor)), "project.python_version")
|
|
54
|
+
directory = (
|
|
55
|
+
(path.parent if path.suffix == ".py" else path)
|
|
56
|
+
if path is not None
|
|
57
|
+
else self.files.root
|
|
58
|
+
)
|
|
59
|
+
directory = self.files.resolve_path(directory)
|
|
60
|
+
boundary = self.files.root
|
|
61
|
+
if not directory.is_relative_to(self.files.root):
|
|
62
|
+
source_roots = [
|
|
63
|
+
self.files.resolve_path(self.files.root / entry)
|
|
64
|
+
for entry in self.files.config.project.source_roots
|
|
65
|
+
if directory.is_relative_to(
|
|
66
|
+
self.files.resolve_path(self.files.root / entry)
|
|
67
|
+
)
|
|
68
|
+
]
|
|
69
|
+
if not source_roots:
|
|
70
|
+
return PythonTarget(
|
|
71
|
+
None, None, uncertainty="path is outside the declared source roots"
|
|
72
|
+
)
|
|
73
|
+
boundary = max(source_roots, key=lambda entry: len(entry.parts)).parent
|
|
74
|
+
if directory in self._cache:
|
|
75
|
+
return self._cache[directory]
|
|
76
|
+
visited: list[Path] = []
|
|
77
|
+
target: PythonTarget | None = None
|
|
78
|
+
for current in (directory, *directory.parents):
|
|
79
|
+
if not current.is_relative_to(boundary):
|
|
80
|
+
break
|
|
81
|
+
visited.append(current)
|
|
82
|
+
metadata = current / "pyproject.toml"
|
|
83
|
+
if current in self._cache:
|
|
84
|
+
target = self._cache[current]
|
|
85
|
+
break
|
|
86
|
+
if self.files.store.is_file(metadata):
|
|
87
|
+
target = self._read(metadata)
|
|
88
|
+
if target is not None:
|
|
89
|
+
break
|
|
90
|
+
else:
|
|
91
|
+
target = None
|
|
92
|
+
result = target or PythonTarget(
|
|
93
|
+
None, None, uncertainty="no declared minimum Python version"
|
|
94
|
+
)
|
|
95
|
+
for current in visited:
|
|
96
|
+
self._cache[current] = result
|
|
97
|
+
return result
|
|
98
|
+
|
|
99
|
+
def describe(self) -> list[dict[str, Any]]:
|
|
100
|
+
targets = {
|
|
101
|
+
self.for_path(source.absolute) for source in self.files.sources.values()
|
|
102
|
+
}
|
|
103
|
+
return [
|
|
104
|
+
target.to_json()
|
|
105
|
+
for target in sorted(
|
|
106
|
+
targets, key=lambda item: (item.source or "", item.minimum or (0, 0))
|
|
107
|
+
)
|
|
108
|
+
]
|
|
109
|
+
|
|
110
|
+
def _read(self, path: Path) -> PythonTarget | None:
|
|
111
|
+
source = self.files.relative(path)
|
|
112
|
+
try:
|
|
113
|
+
data = tomllib.loads(self.files.store.read_text(path))
|
|
114
|
+
except (OSError, UnicodeError, tomllib.TOMLDecodeError) as exc:
|
|
115
|
+
return PythonTarget(
|
|
116
|
+
None, source, uncertainty=f"cannot read Python metadata: {exc}"
|
|
117
|
+
)
|
|
118
|
+
project = data.get("project")
|
|
119
|
+
if project is None:
|
|
120
|
+
return None
|
|
121
|
+
if not isinstance(project, dict):
|
|
122
|
+
return PythonTarget(
|
|
123
|
+
None, source, uncertainty="project metadata is not a table"
|
|
124
|
+
)
|
|
125
|
+
spec = project.get("requires-python")
|
|
126
|
+
if not isinstance(spec, str):
|
|
127
|
+
return PythonTarget(
|
|
128
|
+
None, source, uncertainty="project has no string requires-python"
|
|
129
|
+
)
|
|
130
|
+
return _minimum_from_requires_python(spec, source)
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def _minimum_from_requires_python(spec: str, source: str) -> PythonTarget:
|
|
134
|
+
"""A conservative lower bound. Exclusions never raise the inferred minimum."""
|
|
135
|
+
bounds: list[Version] = []
|
|
136
|
+
try:
|
|
137
|
+
for item in SpecifierSet(spec):
|
|
138
|
+
if item.operator in {">=", ">", "~=", "=="}:
|
|
139
|
+
version = Version(item.version.removesuffix(".*"))
|
|
140
|
+
if version.is_prerelease or version.is_devrelease:
|
|
141
|
+
return PythonTarget(
|
|
142
|
+
None,
|
|
143
|
+
source,
|
|
144
|
+
spec,
|
|
145
|
+
"prerelease minimum is not a stable Python target",
|
|
146
|
+
)
|
|
147
|
+
bounds.append(version)
|
|
148
|
+
elif item.operator == "===":
|
|
149
|
+
return PythonTarget(
|
|
150
|
+
None,
|
|
151
|
+
source,
|
|
152
|
+
spec,
|
|
153
|
+
"arbitrary version equality has no inferred Python minimum",
|
|
154
|
+
)
|
|
155
|
+
except (InvalidSpecifier, InvalidVersion) as exc:
|
|
156
|
+
return PythonTarget(None, source, spec, f"invalid requires-python: {exc}")
|
|
157
|
+
if not bounds:
|
|
158
|
+
return PythonTarget(
|
|
159
|
+
None, source, spec, "requires-python has no explicit lower bound"
|
|
160
|
+
)
|
|
161
|
+
release = max(bounds).release
|
|
162
|
+
return PythonTarget(
|
|
163
|
+
(release[0], release[1] if len(release) > 1 else 0), source, spec
|
|
164
|
+
)
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"""Bounded, exclusively owned dependency state for serialized worker requests."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections import OrderedDict
|
|
6
|
+
from contextlib import contextmanager
|
|
7
|
+
from contextvars import ContextVar
|
|
8
|
+
from typing import TYPE_CHECKING
|
|
9
|
+
|
|
10
|
+
if TYPE_CHECKING:
|
|
11
|
+
from collections.abc import Iterator
|
|
12
|
+
|
|
13
|
+
from smelt.analysis.member_state import MemberIndexState
|
|
14
|
+
|
|
15
|
+
type StateKey = tuple[str, str]
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class RetainedAnalysis:
|
|
19
|
+
def __init__(
|
|
20
|
+
self, *, max_bytes: int = 8 * 1024 * 1024, max_entries: int = 2
|
|
21
|
+
) -> None:
|
|
22
|
+
self.max_bytes = max_bytes
|
|
23
|
+
self.max_entries = max_entries
|
|
24
|
+
self._states: OrderedDict[StateKey, tuple[MemberIndexState, int]] = (
|
|
25
|
+
OrderedDict()
|
|
26
|
+
)
|
|
27
|
+
self._bytes = 0
|
|
28
|
+
|
|
29
|
+
def take(self, key: StateKey) -> MemberIndexState | None:
|
|
30
|
+
# Transfer ownership before mutation: a failed update cannot poison the
|
|
31
|
+
# next request. Only completed states are put back into the session.
|
|
32
|
+
item = self._states.pop(key, None)
|
|
33
|
+
if item is None:
|
|
34
|
+
return None
|
|
35
|
+
state, size = item
|
|
36
|
+
self._bytes -= size
|
|
37
|
+
return state
|
|
38
|
+
|
|
39
|
+
def put(self, key: StateKey, state: MemberIndexState, *, size: int) -> None:
|
|
40
|
+
self.take(key)
|
|
41
|
+
if size > self.max_bytes or self.max_entries < 1:
|
|
42
|
+
return
|
|
43
|
+
while self._states and (
|
|
44
|
+
self._bytes + size > self.max_bytes or len(self._states) >= self.max_entries
|
|
45
|
+
):
|
|
46
|
+
_, (_, previous_size) = self._states.popitem(last=False)
|
|
47
|
+
self._bytes -= previous_size
|
|
48
|
+
self._states[key] = state, size
|
|
49
|
+
self._bytes += size
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class _RequestState:
|
|
53
|
+
def __init__(self, state: RetainedAnalysis) -> None:
|
|
54
|
+
self.state = state
|
|
55
|
+
self.taken: set[StateKey] = set()
|
|
56
|
+
|
|
57
|
+
def take(self, key: StateKey) -> MemberIndexState | None:
|
|
58
|
+
# Current and historical analyses can use the same disk key within one
|
|
59
|
+
# request. They must never borrow the same live mutable dependency graph.
|
|
60
|
+
if key in self.taken:
|
|
61
|
+
return None
|
|
62
|
+
self.taken.add(key)
|
|
63
|
+
return self.state.take(key)
|
|
64
|
+
|
|
65
|
+
def put(self, key: StateKey, state: MemberIndexState, *, size: int) -> None:
|
|
66
|
+
self.state.put(key, state, size=size)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
_CURRENT: ContextVar[_RequestState | None] = ContextVar(
|
|
70
|
+
"retained_analysis", default=None
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def current() -> _RequestState | None:
|
|
75
|
+
return _CURRENT.get()
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
@contextmanager
|
|
79
|
+
def retain(state: RetainedAnalysis | None) -> Iterator[None]:
|
|
80
|
+
token = _CURRENT.set(_RequestState(state) if state is not None else None)
|
|
81
|
+
try:
|
|
82
|
+
yield
|
|
83
|
+
finally:
|
|
84
|
+
_CURRENT.reset(token)
|
smelt/analysis/syntax.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import ast
|
|
4
|
+
from dataclasses import dataclass, field
|
|
5
|
+
from typing import TYPE_CHECKING
|
|
6
|
+
|
|
7
|
+
from pydantic import TypeAdapter
|
|
8
|
+
|
|
9
|
+
from smelt.analysis.exports import Namespace, namespace_of
|
|
10
|
+
from smelt.analysis.parsing import AstCache, resolve_relative
|
|
11
|
+
|
|
12
|
+
if TYPE_CHECKING:
|
|
13
|
+
from smelt.analysis.cache import FactCache
|
|
14
|
+
from smelt.analysis.files import FileIndex, SourceFile
|
|
15
|
+
|
|
16
|
+
_NAMESPACE = TypeAdapter(Namespace)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@dataclass
|
|
20
|
+
class ModuleSyntax:
|
|
21
|
+
module: str
|
|
22
|
+
path: str
|
|
23
|
+
tree: ast.Module
|
|
24
|
+
is_package: bool
|
|
25
|
+
# local name -> qualified name it was imported as
|
|
26
|
+
bindings: dict[str, str] = field(default_factory=dict)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class SyntaxIndex:
|
|
30
|
+
def __init__(
|
|
31
|
+
self, files: FileIndex, asts: AstCache, *, cache: FactCache | None = None
|
|
32
|
+
) -> None:
|
|
33
|
+
self.files = files
|
|
34
|
+
self._asts = asts
|
|
35
|
+
self.cache = cache
|
|
36
|
+
self._by_path: dict[str, ModuleSyntax] = {}
|
|
37
|
+
self._namespaces: dict[str, Namespace | None] = {}
|
|
38
|
+
|
|
39
|
+
def for_path(self, path: str) -> ModuleSyntax | None:
|
|
40
|
+
cached = self._by_path.get(path)
|
|
41
|
+
if cached is not None:
|
|
42
|
+
return cached
|
|
43
|
+
source = self.files.source_for_path(path)
|
|
44
|
+
if source is not None:
|
|
45
|
+
name, is_package = source.module, source.is_package
|
|
46
|
+
elif path in self.files.tests:
|
|
47
|
+
name = path.removesuffix(".py").replace("/", ".")
|
|
48
|
+
is_package = name.endswith(".__init__")
|
|
49
|
+
else:
|
|
50
|
+
return None
|
|
51
|
+
syntax = ModuleSyntax(name, path, self._asts.parse(path), is_package)
|
|
52
|
+
_collect_bindings(syntax)
|
|
53
|
+
self._by_path[path] = syntax
|
|
54
|
+
return syntax
|
|
55
|
+
|
|
56
|
+
def namespace(self, module: str) -> Namespace | None:
|
|
57
|
+
"""Module bindings for ``analysis.exports``; namespaces have no initializer bindings."""
|
|
58
|
+
if module not in self._namespaces:
|
|
59
|
+
source = self.files.sources.get(module)
|
|
60
|
+
self._namespaces[module] = (
|
|
61
|
+
(Namespace(module) if module in self.files.packages else None)
|
|
62
|
+
if source is None
|
|
63
|
+
else self._namespace(source)
|
|
64
|
+
)
|
|
65
|
+
return self._namespaces[module]
|
|
66
|
+
|
|
67
|
+
def _namespace(self, source: SourceFile) -> Namespace:
|
|
68
|
+
def build() -> Namespace:
|
|
69
|
+
return namespace_of(
|
|
70
|
+
self._asts.parse(source.path),
|
|
71
|
+
source.module,
|
|
72
|
+
is_package=source.is_package,
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
if self.cache is None:
|
|
76
|
+
return build()
|
|
77
|
+
return self.cache.get(
|
|
78
|
+
"namespace",
|
|
79
|
+
[
|
|
80
|
+
source.module,
|
|
81
|
+
source.path,
|
|
82
|
+
source.is_package,
|
|
83
|
+
self.files.content_id(source.path),
|
|
84
|
+
],
|
|
85
|
+
_NAMESPACE,
|
|
86
|
+
build,
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def _collect_bindings(syntax: ModuleSyntax) -> None:
|
|
91
|
+
for node in ast.walk(syntax.tree):
|
|
92
|
+
if isinstance(node, ast.Import):
|
|
93
|
+
for alias in node.names:
|
|
94
|
+
if alias.asname:
|
|
95
|
+
syntax.bindings[alias.asname] = alias.name
|
|
96
|
+
else:
|
|
97
|
+
top = alias.name.split(".")[0]
|
|
98
|
+
syntax.bindings.setdefault(top, top)
|
|
99
|
+
elif isinstance(node, ast.ImportFrom):
|
|
100
|
+
base = resolve_relative(
|
|
101
|
+
syntax.module,
|
|
102
|
+
is_package=syntax.is_package,
|
|
103
|
+
level=node.level,
|
|
104
|
+
target=node.module,
|
|
105
|
+
)
|
|
106
|
+
for alias in node.names:
|
|
107
|
+
if alias.name != "*":
|
|
108
|
+
qualified = f"{base}.{alias.name}" if base else alias.name
|
|
109
|
+
syntax.bindings[alias.asname or alias.name] = qualified
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"""Call-only assertions on known async mocks, with bounded local evidence."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import ast
|
|
6
|
+
from collections import Counter
|
|
7
|
+
from typing import TYPE_CHECKING
|
|
8
|
+
|
|
9
|
+
if TYPE_CHECKING:
|
|
10
|
+
from smelt.analysis.test_syntax import MockObservation, TestFunction
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def call_only_async_mocks(test: TestFunction) -> list[MockObservation]:
|
|
14
|
+
asserted = _asserted_nodes(test)
|
|
15
|
+
parents = {
|
|
16
|
+
child: parent
|
|
17
|
+
for parent in ast.walk(test.node)
|
|
18
|
+
for child in ast.iter_child_nodes(parent)
|
|
19
|
+
}
|
|
20
|
+
observations = [
|
|
21
|
+
item
|
|
22
|
+
for item in test.mock_observations
|
|
23
|
+
if isinstance(item.node, ast.Call) or item.node in asserted
|
|
24
|
+
]
|
|
25
|
+
awaited = {item.origin for item in observations if item.kind == "await"}
|
|
26
|
+
risks: dict[str, MockObservation] = {}
|
|
27
|
+
for item in observations:
|
|
28
|
+
if item.kind == "call" and item.origin not in awaited:
|
|
29
|
+
if isinstance(item.node, ast.Attribute) and _checks_no_call(
|
|
30
|
+
item.node, parents
|
|
31
|
+
):
|
|
32
|
+
continue
|
|
33
|
+
risks.setdefault(item.origin, item)
|
|
34
|
+
return list(risks.values())
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _asserted_nodes(test: TestFunction) -> set[ast.AST]:
|
|
38
|
+
writes = Counter(
|
|
39
|
+
node.id
|
|
40
|
+
for node in ast.walk(test.node)
|
|
41
|
+
if isinstance(node, ast.Name) and isinstance(node.ctx, ast.Store | ast.Del)
|
|
42
|
+
)
|
|
43
|
+
assignments: dict[str, list[ast.expr]] = {}
|
|
44
|
+
for node in ast.walk(test.node):
|
|
45
|
+
if isinstance(node, ast.Assign | ast.AnnAssign) and node.value:
|
|
46
|
+
targets = node.targets if isinstance(node, ast.Assign) else [node.target]
|
|
47
|
+
for target in targets:
|
|
48
|
+
for name in ast.walk(target):
|
|
49
|
+
if isinstance(name, ast.Name) and isinstance(name.ctx, ast.Store):
|
|
50
|
+
assignments.setdefault(name.id, []).append(node.value)
|
|
51
|
+
# Follow only single-assignment local values, such as unpacked call_args.
|
|
52
|
+
pending: list[ast.AST] = list(test.assertions)
|
|
53
|
+
pending.extend(
|
|
54
|
+
call.node
|
|
55
|
+
for call in test.calls
|
|
56
|
+
if call.qualified.startswith("unittest.TestCase.assert")
|
|
57
|
+
)
|
|
58
|
+
asserted: set[ast.AST] = set()
|
|
59
|
+
followed: set[str] = set()
|
|
60
|
+
while pending:
|
|
61
|
+
expression = pending.pop()
|
|
62
|
+
for node in ast.walk(expression):
|
|
63
|
+
asserted.add(node)
|
|
64
|
+
if isinstance(node, ast.Name) and node.id not in followed:
|
|
65
|
+
followed.add(node.id)
|
|
66
|
+
values = assignments.get(node.id, [])
|
|
67
|
+
if (
|
|
68
|
+
len(values) == 1
|
|
69
|
+
and writes[node.id] == 1
|
|
70
|
+
and values[0].lineno < node.lineno
|
|
71
|
+
):
|
|
72
|
+
pending.append(values[0])
|
|
73
|
+
return asserted
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _checks_no_call(node: ast.Attribute, parents: dict[ast.AST, ast.AST]) -> bool:
|
|
77
|
+
parent = parents.get(node)
|
|
78
|
+
if isinstance(parent, ast.UnaryOp) and isinstance(parent.op, ast.Not):
|
|
79
|
+
return True
|
|
80
|
+
if isinstance(parent, ast.Compare) and len(parent.ops) == 1:
|
|
81
|
+
left, right = parent.left, parent.comparators[0]
|
|
82
|
+
other = right if left is node else left if right is node else None
|
|
83
|
+
return isinstance(parent.ops[0], ast.Eq | ast.Is) and (
|
|
84
|
+
(isinstance(other, ast.Constant) and other.value in (None, False, 0))
|
|
85
|
+
or (
|
|
86
|
+
node.attr == "call_args_list"
|
|
87
|
+
and isinstance(other, ast.List)
|
|
88
|
+
and not other.elts
|
|
89
|
+
)
|
|
90
|
+
)
|
|
91
|
+
return False
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"""Narrow candidates for independent literal test cases, never lifecycle replay."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import ast
|
|
6
|
+
from typing import TYPE_CHECKING
|
|
7
|
+
|
|
8
|
+
if TYPE_CHECKING:
|
|
9
|
+
from smelt.analysis.test_syntax import TestFunction
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def independent_case_loops(test: TestFunction) -> list[ast.For]:
|
|
13
|
+
candidates: list[ast.For] = []
|
|
14
|
+
for node in test.node.body:
|
|
15
|
+
if (
|
|
16
|
+
not isinstance(node, ast.For)
|
|
17
|
+
or node.orelse
|
|
18
|
+
or not isinstance(node.iter, ast.List | ast.Tuple)
|
|
19
|
+
):
|
|
20
|
+
continue
|
|
21
|
+
if not _MIN_CASES <= len(node.iter.elts) <= _MAX_CASES or not all(
|
|
22
|
+
_literal(value) for value in node.iter.elts
|
|
23
|
+
):
|
|
24
|
+
continue
|
|
25
|
+
if _independent_body(node):
|
|
26
|
+
candidates.append(node)
|
|
27
|
+
return candidates
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
_MAX_CASES = 12
|
|
31
|
+
_MIN_CASES = 2
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _literal(node: ast.AST) -> bool:
|
|
35
|
+
if isinstance(node, ast.Constant):
|
|
36
|
+
return True
|
|
37
|
+
return isinstance(node, ast.List | ast.Tuple | ast.Dict) and all(
|
|
38
|
+
_literal(child)
|
|
39
|
+
for child in ast.iter_child_nodes(node)
|
|
40
|
+
if not isinstance(child, ast.expr_context)
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _independent_body(loop: ast.For) -> bool:
|
|
45
|
+
assigned = {node.id for node in ast.walk(loop.target) if isinstance(node, ast.Name)}
|
|
46
|
+
locals_in_loop = {
|
|
47
|
+
node.id
|
|
48
|
+
for statement in loop.body
|
|
49
|
+
for node in ast.walk(statement)
|
|
50
|
+
if isinstance(node, ast.Name) and isinstance(node.ctx, ast.Store)
|
|
51
|
+
}
|
|
52
|
+
action = assertion = False
|
|
53
|
+
for statement in loop.body:
|
|
54
|
+
if not isinstance(statement, ast.Assign | ast.AnnAssign | ast.Assert):
|
|
55
|
+
return False
|
|
56
|
+
targets = (
|
|
57
|
+
statement.targets
|
|
58
|
+
if isinstance(statement, ast.Assign)
|
|
59
|
+
else [statement.target]
|
|
60
|
+
if isinstance(statement, ast.AnnAssign)
|
|
61
|
+
else []
|
|
62
|
+
)
|
|
63
|
+
if any(not isinstance(target, ast.Name) for target in targets):
|
|
64
|
+
return False
|
|
65
|
+
value = statement.test if isinstance(statement, ast.Assert) else statement.value
|
|
66
|
+
if value is None:
|
|
67
|
+
return False
|
|
68
|
+
for child in ast.walk(value):
|
|
69
|
+
if (
|
|
70
|
+
isinstance(child, ast.Name)
|
|
71
|
+
and isinstance(child.ctx, ast.Load)
|
|
72
|
+
and child.id in locals_in_loop
|
|
73
|
+
and child.id not in assigned
|
|
74
|
+
):
|
|
75
|
+
return False
|
|
76
|
+
if isinstance(child, ast.Call) and isinstance(child.func, ast.Attribute):
|
|
77
|
+
# A method on pre-existing state can participate in replay/handoff.
|
|
78
|
+
receiver = child.func.value
|
|
79
|
+
if not isinstance(receiver, ast.Name) or receiver.id not in assigned:
|
|
80
|
+
return False
|
|
81
|
+
assertion |= isinstance(statement, ast.Assert)
|
|
82
|
+
action |= any(isinstance(child, ast.Call) for child in ast.walk(value))
|
|
83
|
+
assigned.update(target.id for target in targets if isinstance(target, ast.Name))
|
|
84
|
+
return action and assertion
|