python-constricter 0.4.1__py3-none-any.whl → 0.4.3__py3-none-any.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.
- constricter/__init__.py +0 -1
- constricter/__main__.py +3 -3
- constricter/_version.py +1 -2
- constricter/cli/__init__.py +0 -1
- constricter/cli/baseline.py +0 -1
- constricter/cli/collecting.py +6 -4
- constricter/cli/command.py +22 -8
- constricter/cli/config.py +0 -1
- constricter/cli/explain.py +13 -1
- constricter/cli/lsp/__init__.py +0 -1
- constricter/cli/lsp/connection.py +14 -12
- constricter/cli/lsp/edits.py +0 -1
- constricter/cli/lsp/guard.py +0 -1
- constricter/cli/lsp/hints.py +151 -44
- constricter/cli/lsp/protocol.py +14 -2
- constricter/cli/lsp/remembered.py +0 -1
- constricter/cli/lsp/stored.py +0 -1
- constricter/cli/options.py +15 -3
- constricter/cli/paths.py +0 -1
- constricter/cli/reading.py +0 -1
- constricter/cli/report.py +1 -1
- constricter/cli/rounds.py +14 -9
- constricter/cli/runs.py +33 -2
- constricter/cli/schedule.py +30 -7
- constricter/cli/traced.py +0 -1
- constricter/cli/workers.py +0 -1
- constricter/empty.py +0 -1
- constricter/fix/__init__.py +0 -1
- constricter/fix/core/__init__.py +0 -1
- constricter/fix/core/asked.py +0 -1
- constricter/fix/core/fixes.py +11 -3
- constricter/fix/core/imports.py +27 -11
- constricter/fix/core/inherited.py +0 -1
- constricter/fix/core/known.py +20 -2
- constricter/fix/core/signatures.py +0 -1
- constricter/fix/index/__init__.py +0 -1
- constricter/fix/index/annotated.py +83 -0
- constricter/fix/index/awaits.py +0 -1
- constricter/fix/index/beyond.py +0 -1
- constricter/fix/index/callers.py +0 -1
- constricter/fix/index/consistent.py +142 -0
- constricter/fix/index/crossed.py +27 -7
- constricter/fix/index/decorated.py +0 -1
- constricter/fix/index/fixtures.py +15 -8
- constricter/fix/index/linked.py +0 -1
- constricter/fix/index/loose.py +0 -1
- constricter/fix/index/managed.py +0 -1
- constricter/fix/index/modules.py +25 -2
- constricter/fix/index/offers.py +0 -1
- constricter/fix/index/order.py +0 -1
- constricter/fix/index/origins.py +2 -1
- constricter/fix/index/own_methods.py +0 -1
- constricter/fix/index/own_overloads.py +0 -1
- constricter/fix/index/own_types.py +0 -1
- constricter/fix/index/params.py +0 -1
- constricter/fix/index/plain.py +0 -1
- constricter/fix/index/project.py +80 -12
- constricter/fix/index/sides.py +0 -1
- constricter/fix/index/strings.py +0 -1
- constricter/fix/index/stubs/__init__.py +0 -1
- constricter/fix/index/stubs/atoms.py +0 -1
- constricter/fix/index/stubs/chained.py +0 -1
- constricter/fix/index/stubs/classnames.py +0 -1
- constricter/fix/index/stubs/declared.py +0 -1
- constricter/fix/index/stubs/installed.py +45 -19
- constricter/fix/index/stubs/names.py +0 -1
- constricter/fix/index/stubs/read.py +0 -1
- constricter/fix/index/stubs/signatures.py +0 -1
- constricter/fix/index/stubs/stubbed.py +4 -3
- constricter/fix/index/taken.py +0 -1
- constricter/fix/index/tuples.py +0 -1
- constricter/fix/libraries/__init__.py +0 -1
- constricter/fix/libraries/generics.py +50 -0
- constricter/fix/libraries/library.py +50 -12
- constricter/fix/libraries/opened.py +0 -1
- constricter/fix/libraries/overloads.py +93 -63
- constricter/fix/libraries/receivers.py +0 -1
- constricter/fix/libraries/stdlib.py +32 -4
- constricter/fix/libraries/walks.py +0 -1
- constricter/fix/tables/source.json +1 -1
- constricter/fix/values/__init__.py +0 -1
- constricter/fix/values/aliased.py +0 -1
- constricter/fix/values/bodies.py +0 -1
- constricter/fix/values/callables.py +0 -1
- constricter/fix/values/called.py +0 -1
- constricter/fix/values/classvars.py +10 -1
- constricter/fix/values/decided.py +0 -1
- constricter/fix/values/displays.py +0 -1
- constricter/fix/values/doubts.py +25 -4
- constricter/fix/values/entered.py +0 -1
- constricter/fix/values/fills.py +158 -13
- constricter/fix/values/guesses.py +13 -7
- constricter/fix/values/hinted.py +174 -34
- constricter/fix/values/inference.py +20 -2
- constricter/fix/values/loops.py +2 -3
- constricter/fix/values/members.py +152 -15
- constricter/fix/values/narrowed.py +274 -13
- constricter/fix/values/operated.py +0 -1
- constricter/fix/values/ordered.py +0 -1
- constricter/fix/values/reads.py +356 -0
- constricter/fix/values/refused.py +87 -0
- constricter/fix/values/returned.py +0 -1
- constricter/fix/values/returns.py +0 -1
- constricter/fix/values/shapes.py +20 -9
- constricter/fix/values/stored.py +40 -13
- constricter/fix/values/targets.py +0 -1
- constricter/fix/values/unchecked.py +25 -12
- constricter/jsonc.py +0 -1
- constricter/noqa.py +0 -1
- constricter/notebook.py +0 -1
- constricter/offences.py +18 -3
- constricter/overrides.py +0 -1
- constricter/plugins/__init__.py +0 -1
- constricter/plugins/flake8.py +19 -3
- constricter/plugins/pylint.py +7 -2
- constricter/recording.py +0 -1
- constricter/rules/__init__.py +0 -1
- constricter/rules/annotations.py +0 -1
- constricter/rules/attributes.py +259 -0
- constricter/rules/binding.py +119 -35
- constricter/rules/calls.py +62 -7
- constricter/rules/checker.py +36 -7
- constricter/rules/declarations.py +161 -3
- constricter/rules/decorators.py +0 -1
- constricter/rules/flow.py +0 -1
- constricter/rules/keys.py +0 -1
- constricter/rules/late.py +32 -13
- constricter/rules/narrowing.py +0 -1
- constricter/rules/parsed.py +1 -2
- constricter/rules/quoted.py +0 -1
- constricter/rules/rebinding.py +0 -1
- constricter/rules/recorded.py +6 -2
- constricter/rules/redundant.py +0 -1
- constricter/rules/returning.py +231 -0
- constricter/rules/scope.py +180 -32
- constricter/rules/scoped.py +7 -2
- constricter/rules/scoping.py +2 -4
- constricter/rules/settings.py +35 -4
- constricter/rules/syntax.py +63 -18
- constricter/rules/tables.py +0 -1
- constricter/rules/walked.py +17 -11
- constricter/rules/widened.py +0 -1
- constricter/trace.py +94 -28
- {python_constricter-0.4.1.dist-info → python_constricter-0.4.3.dist-info}/METADATA +28 -11
- python_constricter-0.4.3.dist-info/RECORD +169 -0
- python_constricter-0.4.1.dist-info/RECORD +0 -162
- {python_constricter-0.4.1.dist-info → python_constricter-0.4.3.dist-info}/WHEEL +0 -0
- {python_constricter-0.4.1.dist-info → python_constricter-0.4.3.dist-info}/entry_points.txt +0 -0
- {python_constricter-0.4.1.dist-info → python_constricter-0.4.3.dist-info}/licenses/LICENSE.md +0 -0
constricter/__init__.py
CHANGED
constricter/__main__.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
# SPDX-License-Identifier: MIT
|
|
2
1
|
"""`python -m constricter`, and the `constricter` script."""
|
|
3
2
|
|
|
4
3
|
import contextlib
|
|
@@ -13,9 +12,10 @@ def run() -> None:
|
|
|
13
12
|
|
|
14
13
|
Not by the interpreter's own exit, which frees every parsed file and the index an object at a
|
|
15
14
|
time: a fifth of a check of a thousand files. What the command wrote is flushed first; nothing
|
|
16
|
-
else waits on the exit (its workers have ended, its output file is closed).
|
|
15
|
+
else waits on the exit (its workers have ended, its output file is closed). Nor is the collector
|
|
16
|
+
turned on again for it (`leaving`).
|
|
17
17
|
"""
|
|
18
|
-
status: int = main()
|
|
18
|
+
status: int = main(leaving=True)
|
|
19
19
|
with contextlib.suppress(OSError, ValueError): # a reader that left, a stream closed already
|
|
20
20
|
_ = sys.stdout.flush()
|
|
21
21
|
_ = sys.stderr.flush()
|
constricter/_version.py
CHANGED
constricter/cli/__init__.py
CHANGED
constricter/cli/baseline.py
CHANGED
constricter/cli/collecting.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
# SPDX-License-Identifier: MIT
|
|
2
1
|
"""The garbage collector, as the command runs it: by hand, once per file checked.
|
|
3
2
|
|
|
4
3
|
A check keeps every file's tree (see `parsed`), and allocates a great deal for each file, little of it
|
|
@@ -24,9 +23,12 @@ _YOUNGER: Final = not sysconfig.get_config_var("Py_GIL_DISABLED")
|
|
|
24
23
|
|
|
25
24
|
|
|
26
25
|
@contextlib.contextmanager
|
|
27
|
-
def by_hand() -> Generator[None]:
|
|
26
|
+
def by_hand(*, leaving: bool = False) -> Generator[None]:
|
|
28
27
|
"""Turn automatic collection off for the command's run, and back as it was after.
|
|
29
28
|
|
|
29
|
+
Not for a process that leaves once the run's done (`leaving`): turned on again, the collector
|
|
30
|
+
looked through all the run left alive, at once (a check of pandas: 3.4s, for nothing).
|
|
31
|
+
|
|
30
32
|
Yields:
|
|
31
33
|
Nothing: the run.
|
|
32
34
|
|
|
@@ -36,9 +38,9 @@ def by_hand() -> Generator[None]:
|
|
|
36
38
|
try:
|
|
37
39
|
yield
|
|
38
40
|
finally:
|
|
39
|
-
if _FREEZES:
|
|
41
|
+
if _FREEZES and not leaving:
|
|
40
42
|
gc.unfreeze()
|
|
41
|
-
if was:
|
|
43
|
+
if was and not leaving:
|
|
42
44
|
gc.enable()
|
|
43
45
|
|
|
44
46
|
|
constricter/cli/command.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
# SPDX-License-Identifier: MIT
|
|
2
1
|
"""The `constricter` command (see README)."""
|
|
3
2
|
|
|
4
3
|
import codecs
|
|
@@ -36,6 +35,8 @@ from constricter.rules import parsed
|
|
|
36
35
|
from constricter.rules.checker import Checked, Coverage, annotation_coverage, checked_source
|
|
37
36
|
|
|
38
37
|
if TYPE_CHECKING: # slow to import, and only needed for `--infer-with`
|
|
38
|
+
from types import ModuleType
|
|
39
|
+
|
|
39
40
|
from constricter.cli.lsp import hints
|
|
40
41
|
|
|
41
42
|
EXIT_CLEAN: Final = 0
|
|
@@ -370,12 +371,17 @@ def _check_rounds(options: Options) -> tuple[list[Path], list[FileRun]]:
|
|
|
370
371
|
return names, settled(paths, check, options, None)
|
|
371
372
|
session: hints.Session
|
|
372
373
|
# Imported only for `--infer-with`: it's slow to import, for every run's start.
|
|
373
|
-
|
|
374
|
+
hinting: ModuleType = importlib.import_module("constricter.cli.lsp.hints")
|
|
375
|
+
asking: hints.Asking = cast("type[hints.Asking]", hinting.Asking)(
|
|
376
|
+
kept=not options.infer_fresh,
|
|
377
|
+
in_turn=options.infer_in_turn,
|
|
378
|
+
)
|
|
379
|
+
with cast("type[hints.Session]", hinting.Session)(
|
|
374
380
|
options.infer_with,
|
|
375
381
|
Path.cwd(),
|
|
376
382
|
options.jobs,
|
|
377
383
|
options.infer_memory,
|
|
378
|
-
|
|
384
|
+
asking,
|
|
379
385
|
) as session:
|
|
380
386
|
runs: list[FileRun] = settled(paths, check, options, session)
|
|
381
387
|
checker: str
|
|
@@ -383,6 +389,11 @@ def _check_rounds(options: Options) -> tuple[list[Path], list[FileRun]]:
|
|
|
383
389
|
for checker, path in session.abandoned:
|
|
384
390
|
name: Path = options.input.name(path)
|
|
385
391
|
_ = sys.stderr.write(f"constricter: warning: {checker} hung on {name} twice: no hints for it\n")
|
|
392
|
+
read: int
|
|
393
|
+
rounds: int
|
|
394
|
+
for checker, read, rounds in session.reused:
|
|
395
|
+
told: str = f"{checker}'s hints for {read} of {rounds} rounds were read from the store"
|
|
396
|
+
_ = sys.stderr.write(f"constricter: note: {told} (`--infer-fresh` asks again)\n")
|
|
386
397
|
return names, runs
|
|
387
398
|
|
|
388
399
|
|
|
@@ -485,8 +496,8 @@ def _typed(found: Coverage) -> str:
|
|
|
485
496
|
return f"{found.typed}/{found.total} typed ({found.percent:.1f}%){wider}"
|
|
486
497
|
|
|
487
498
|
|
|
488
|
-
def _run(options: Options) -> int:
|
|
489
|
-
"""Check, fix, diff, count or baseline, printing what that finds.
|
|
499
|
+
def _run(options: Options, *, leaving: bool) -> int:
|
|
500
|
+
"""Check, fix, diff, count or baseline, printing what that finds (`leaving`: see `main`).
|
|
490
501
|
|
|
491
502
|
Returns:
|
|
492
503
|
The exit status.
|
|
@@ -494,7 +505,7 @@ def _run(options: Options) -> int:
|
|
|
494
505
|
"""
|
|
495
506
|
names: list[Path]
|
|
496
507
|
runs: list[FileRun]
|
|
497
|
-
with collecting.by_hand():
|
|
508
|
+
with collecting.by_hand(leaving=leaving):
|
|
498
509
|
try:
|
|
499
510
|
names, runs = _check_all(options)
|
|
500
511
|
except HintError as error:
|
|
@@ -527,9 +538,12 @@ def _run(options: Options) -> int:
|
|
|
527
538
|
return EXIT_ERROR if failed else status
|
|
528
539
|
|
|
529
540
|
|
|
530
|
-
def main(argv: Sequence[str] | None = None) -> int:
|
|
541
|
+
def main(argv: Sequence[str] | None = None, *, leaving: bool = False) -> int:
|
|
531
542
|
"""Run the command.
|
|
532
543
|
|
|
544
|
+
`leaving`: the process leaves once it's done (`constricter.__main__.run`), its collector as the
|
|
545
|
+
run left it (see `collecting.by_hand`).
|
|
546
|
+
|
|
533
547
|
Returns:
|
|
534
548
|
Its exit status.
|
|
535
549
|
|
|
@@ -543,5 +557,5 @@ def main(argv: Sequence[str] | None = None) -> int:
|
|
|
543
557
|
else contextlib.nullcontext(sys.stdout) as stream,
|
|
544
558
|
contextlib.redirect_stdout(stream),
|
|
545
559
|
):
|
|
546
|
-
status: int = _run(options)
|
|
560
|
+
status: int = _run(options, leaving=leaving)
|
|
547
561
|
return EXIT_CLEAN if options.output.exit_zero and status == EXIT_FOUND else status
|
constricter/cli/config.py
CHANGED
constricter/cli/explain.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
# SPDX-License-Identifier: MIT
|
|
2
1
|
"""Each code's rationale and fix, for `constricter --explain`."""
|
|
3
2
|
|
|
4
3
|
from typing import Final
|
|
@@ -14,7 +13,9 @@ from constricter.offences import (
|
|
|
14
13
|
OPT_IN,
|
|
15
14
|
REDUNDANT_TYPE,
|
|
16
15
|
UNANNOTATED,
|
|
16
|
+
UNANNOTATED_ATTRIBUTE,
|
|
17
17
|
UNANNOTATED_MEMBER,
|
|
18
|
+
UNANNOTATED_RETURN,
|
|
18
19
|
UNTYPED_TARGET,
|
|
19
20
|
UNUSED_UNION_MEMBER,
|
|
20
21
|
VAGUE_TYPE,
|
|
@@ -83,6 +84,17 @@ _WHY: Final = {
|
|
|
83
84
|
"(not from a nested function either) could say so: `limit: Final = 10`. Off unless selected\n"
|
|
84
85
|
"by its full code (`--extend-select LVA012`): most locals are bound once."
|
|
85
86
|
),
|
|
87
|
+
UNANNOTATED_ATTRIBUTE: (
|
|
88
|
+
"An instance attribute a plain class's method first stores (`self.count = 0`), declared\n"
|
|
89
|
+
"nowhere in the class, is typed by whatever a type checker infers from that store. Annotate\n"
|
|
90
|
+
"it there: `self.count: int = 0`. Off unless selected by its full code (`--extend-select LVA013`)."
|
|
91
|
+
),
|
|
92
|
+
UNANNOTATED_RETURN: (
|
|
93
|
+
"A plain function with no return annotation leaves its callers to whatever a type checker\n"
|
|
94
|
+
"infers. `--fix` writes what its own `return`s give (`-> int`), or `-> None` for one that\n"
|
|
95
|
+
"returns nothing. Off unless selected by its full code (`--extend-select LVA014`): ruff's ANN201\n"
|
|
96
|
+
"reports it too; this is for the fix."
|
|
97
|
+
),
|
|
86
98
|
}
|
|
87
99
|
|
|
88
100
|
|
constricter/cli/lsp/__init__.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
# SPDX-License-Identifier: MIT
|
|
2
1
|
"""A running language server, and the JSON-RPC conversation with it (`Connection`), for `hints`.
|
|
3
2
|
|
|
4
3
|
The files it has open, the hint requests sent and not yet answered, and what it has said.
|
|
@@ -22,7 +21,7 @@ from pathlib import Path
|
|
|
22
21
|
from typing import IO, Final, NamedTuple, TypeAlias, cast
|
|
23
22
|
|
|
24
23
|
from constricter.cli.lsp import edits, guard, protocol
|
|
25
|
-
from constricter.cli.lsp.protocol import HintError, HungError
|
|
24
|
+
from constricter.cli.lsp.protocol import ExitedError, HintError, HungError
|
|
26
25
|
from constricter.fix.core.known import Offered
|
|
27
26
|
|
|
28
27
|
_Json: TypeAlias = protocol.Json
|
|
@@ -79,22 +78,24 @@ class _Documents:
|
|
|
79
78
|
texts: dict[str, str] = field(default_factory=dict[str, str])
|
|
80
79
|
|
|
81
80
|
|
|
82
|
-
class
|
|
81
|
+
class Asked(NamedTuple):
|
|
83
82
|
"""Hint requests sent, not yet answered: each request's file, and each file's lines."""
|
|
84
83
|
|
|
85
84
|
numbers: dict[int, Path]
|
|
86
85
|
lines: dict[Path, list[str]]
|
|
87
86
|
|
|
88
87
|
|
|
89
|
-
Sent: TypeAlias = tuple[dict[Path, str],
|
|
88
|
+
Sent: TypeAlias = tuple[dict[Path, str], Asked] # a batch of files, and its hint requests
|
|
90
89
|
|
|
91
90
|
|
|
92
91
|
class Connection:
|
|
93
92
|
"""A running language server, and the JSON-RPC conversation with it."""
|
|
94
93
|
|
|
95
|
-
def __init__(self, argv: Sequence[str], root: Path) -> None:
|
|
94
|
+
def __init__(self, argv: Sequence[str], root: Path, environment: Mapping[str, str] | None = None) -> None:
|
|
96
95
|
"""Start the server (`argv`) and initialize it, with `root` as its one workspace folder.
|
|
97
96
|
|
|
97
|
+
In `environment`, or else this process's.
|
|
98
|
+
|
|
98
99
|
Raises:
|
|
99
100
|
HintError: It can't be started, or fails to initialize.
|
|
100
101
|
|
|
@@ -115,6 +116,7 @@ class Connection:
|
|
|
115
116
|
stdout=subprocess.PIPE,
|
|
116
117
|
stderr=subprocess.DEVNULL,
|
|
117
118
|
cwd=root,
|
|
119
|
+
env=environment,
|
|
118
120
|
),
|
|
119
121
|
)
|
|
120
122
|
self.inbox: _Inbox = _Inbox()
|
|
@@ -188,7 +190,7 @@ class Connection:
|
|
|
188
190
|
return False
|
|
189
191
|
return True
|
|
190
192
|
|
|
191
|
-
def ask(self, files: Mapping[Path, str]) -> "
|
|
193
|
+
def ask(self, files: Mapping[Path, str]) -> "Asked":
|
|
192
194
|
"""Open each file (or send its new text), and ask for its hints over the whole of it.
|
|
193
195
|
|
|
194
196
|
It doesn't wait for them. Each file is left open: basedpyright (1.40) fails the next file's
|
|
@@ -204,7 +206,7 @@ class Connection:
|
|
|
204
206
|
text: str
|
|
205
207
|
for path, text in files.items():
|
|
206
208
|
self._opened(path.resolve().as_uri(), text)
|
|
207
|
-
return
|
|
209
|
+
return Asked({self._ask_hints(path, lines[path]): path for path in files}, lines)
|
|
208
210
|
|
|
209
211
|
def _ask_hints(self, path: Path, lines: Sequence[str]) -> int:
|
|
210
212
|
"""Ask for an open file's hints over the whole of it, without waiting for them.
|
|
@@ -221,7 +223,7 @@ class Connection:
|
|
|
221
223
|
},
|
|
222
224
|
)
|
|
223
225
|
|
|
224
|
-
def answers(self, asked: "
|
|
226
|
+
def answers(self, asked: "Asked") -> Found:
|
|
225
227
|
"""Wait for the answers to hint requests `ask` sent, asking again for those the server dropped.
|
|
226
228
|
|
|
227
229
|
Returns:
|
|
@@ -391,7 +393,7 @@ class Connection:
|
|
|
391
393
|
It.
|
|
392
394
|
|
|
393
395
|
Raises:
|
|
394
|
-
|
|
396
|
+
ExitedError: The server exited (`_waited` raises `HungError` where it took too long).
|
|
395
397
|
|
|
396
398
|
"""
|
|
397
399
|
# `_waited` raises rather than end: there is always a next message, or an error.
|
|
@@ -400,7 +402,7 @@ class Connection:
|
|
|
400
402
|
)
|
|
401
403
|
if message is None:
|
|
402
404
|
error: str = f"{self.name} exited while answering `{method}`"
|
|
403
|
-
raise
|
|
405
|
+
raise ExitedError(error)
|
|
404
406
|
return message
|
|
405
407
|
|
|
406
408
|
def _waited(self, method: str) -> _Object | None:
|
|
@@ -460,7 +462,7 @@ class Connection:
|
|
|
460
462
|
"""Frame and send one message.
|
|
461
463
|
|
|
462
464
|
Raises:
|
|
463
|
-
|
|
465
|
+
ExitedError: The server has exited.
|
|
464
466
|
|
|
465
467
|
"""
|
|
466
468
|
body: bytes = json.dumps(message).encode()
|
|
@@ -472,4 +474,4 @@ class Connection:
|
|
|
472
474
|
)
|
|
473
475
|
except (OSError, ValueError) as error: # it has gone, or its input was closed to stop it
|
|
474
476
|
failure: str = f"{self.name} exited: {error}"
|
|
475
|
-
raise
|
|
477
|
+
raise ExitedError(failure) from error
|
constricter/cli/lsp/edits.py
CHANGED
constricter/cli/lsp/guard.py
CHANGED
constricter/cli/lsp/hints.py
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
# SPDX-License-Identifier: MIT
|
|
2
1
|
"""`--infer-with`: ask a type checker's language server what type each untyped name has.
|
|
3
2
|
|
|
4
|
-
Each checker's servers are started once,
|
|
5
|
-
their workspace, and work at once (see `Session`); each
|
|
6
|
-
read, and its inlay hints asked for over the whole file. A
|
|
7
|
-
after the name it types: that end is its key, as the line
|
|
8
|
-
gives the name's end. What the hint says is
|
|
9
|
-
text.
|
|
3
|
+
Each checker's servers are started once (each round, asked in turn: see `Asking`), over standard
|
|
4
|
+
input and output, with the project root as their workspace, and work at once (see `Session`); each
|
|
5
|
+
file is opened with the text constricter read, and its inlay hints asked for over the whole file. A
|
|
6
|
+
variable-type hint (`: int`) sits just after the name it types: that end is its key, as the line
|
|
7
|
+
(from 1) and the UTF-8 byte column `ast` gives the name's end. What the hint says is
|
|
8
|
+
`constricter.fix.values.hinted`'s to judge: here it's only text.
|
|
10
9
|
|
|
11
10
|
A server that can't be started, exits or answers an error stops the run: the option asked for its
|
|
12
11
|
types, and silently offering none would look like a checker that found nothing. One that hangs is
|
|
13
12
|
restarted and asked about each of its unanswered files alone; a file it hangs on again gets no hints
|
|
14
13
|
(`Session.abandoned`, which the command reports), and a checker that hangs on more than
|
|
15
|
-
`_MOST_ABANDONED` files stops the run. A
|
|
16
|
-
|
|
14
|
+
`_MOST_ABANDONED` files stops the run. A Node server whose heap is capped (`Checker.share`) and
|
|
15
|
+
exits is taken for one past its cap, as one that hung. A round of files whose hints are kept from an
|
|
16
|
+
earlier run, nothing they rest on changed since, is read from the store instead (see `stored`).
|
|
17
17
|
"""
|
|
18
18
|
|
|
19
19
|
import contextlib
|
|
@@ -24,11 +24,11 @@ from collections.abc import Callable, Mapping, Sequence
|
|
|
24
24
|
from concurrent.futures import Future, ThreadPoolExecutor
|
|
25
25
|
from functools import partial
|
|
26
26
|
from pathlib import Path
|
|
27
|
-
from typing import Final, Self, TypeAlias
|
|
27
|
+
from typing import Final, NamedTuple, Self, TypeAlias
|
|
28
28
|
|
|
29
29
|
from constricter.cli.lsp import protocol, remembered, stored
|
|
30
30
|
from constricter.cli.lsp.connection import Connection, FileHints, Found, Sent
|
|
31
|
-
from constricter.cli.lsp.protocol import SERVERS, HintError, HungError, Server
|
|
31
|
+
from constricter.cli.lsp.protocol import SERVERS, ExitedError, HintError, HungError, Server
|
|
32
32
|
from constricter.fix.core.known import Hints
|
|
33
33
|
|
|
34
34
|
_Task: TypeAlias = Callable[[], Found] # one server's work: the hints of each file it's asked about
|
|
@@ -39,6 +39,22 @@ MEMORY: Final = 8 << 30 # the most a checker's servers use together, by default
|
|
|
39
39
|
_MEMINFO: Final = Path("/proc/meminfo")
|
|
40
40
|
_FILES_PER_SERVER: Final = 32 # fewer files than this each don't pay for another server's start
|
|
41
41
|
_MOST_ABANDONED: Final = 3 # files a checker may hang on twice before the run stops
|
|
42
|
+
_NODE_OPTIONS: Final = "NODE_OPTIONS" # where a Node server's heap is capped (`Checker.share`)
|
|
43
|
+
_HEAP: Final = 9 # tenths of a Node server's share of the memory its heap may take
|
|
44
|
+
_WHOLE: Final = 10
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class Asking(NamedTuple):
|
|
48
|
+
"""How a session asks its checkers.
|
|
49
|
+
|
|
50
|
+
With `kept`, a round's hints are read from the store where it has them, and kept there where it
|
|
51
|
+
doesn't. With `in_turn`, a round asks one checker after another, each one's servers shut down
|
|
52
|
+
before the next one's start: the most memory held is the largest checker's, not their sum, for
|
|
53
|
+
their starts every round.
|
|
54
|
+
"""
|
|
55
|
+
|
|
56
|
+
kept: bool = True
|
|
57
|
+
in_turn: bool = False
|
|
42
58
|
|
|
43
59
|
|
|
44
60
|
class Session:
|
|
@@ -55,16 +71,17 @@ class Session:
|
|
|
55
71
|
root: Path,
|
|
56
72
|
servers: int = 1,
|
|
57
73
|
memory: int | None = None,
|
|
58
|
-
|
|
59
|
-
kept: bool = True,
|
|
74
|
+
how: Asking | None = None,
|
|
60
75
|
) -> None:
|
|
61
76
|
"""Get ready to start `checkers`' servers, with `root` as their workspace.
|
|
62
77
|
|
|
63
|
-
Each checker gets up to `servers`, within `memory` bytes (see `Checker`)
|
|
64
|
-
|
|
78
|
+
Each checker gets up to `servers`, within `memory` bytes (see `Checker`), and is asked `how`
|
|
79
|
+
says (by default, every round's hints kept, all checkers at once).
|
|
65
80
|
"""
|
|
66
81
|
self.checkers: list[Checker] = [Checker(checker, root, servers, memory) for checker in checkers]
|
|
67
|
-
self.
|
|
82
|
+
self.how: Asking = how or Asking()
|
|
83
|
+
# Each checker's rounds, by its name: whether the store had each one's hints.
|
|
84
|
+
self.rounds: dict[str, list[bool]] = {checker.name: [] for checker in self.checkers}
|
|
68
85
|
|
|
69
86
|
def __enter__(self) -> Self:
|
|
70
87
|
"""Use the servers.
|
|
@@ -95,26 +112,28 @@ class Session:
|
|
|
95
112
|
names: dict[str, str] = {}
|
|
96
113
|
found: dict[str, Found] = {}
|
|
97
114
|
checker: Checker
|
|
98
|
-
for checker in self.checkers if self.kept else ():
|
|
115
|
+
for checker in self.checkers if self.how.kept else ():
|
|
99
116
|
names[checker.name] = stored.key(checker.name, checker.command, checker.root, files)
|
|
100
117
|
read: tuple[Found, frozenset[Path]] | None
|
|
101
118
|
if (read := stored.load(names[checker.name], checker.root, files)) is not None:
|
|
102
119
|
found[checker.name] = read[0]
|
|
103
120
|
checker.abandoned |= read[1]
|
|
104
121
|
asking: list[Checker] = [checker for checker in self.checkers if checker.name not in found]
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
]
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
122
|
+
for checker in self.checkers:
|
|
123
|
+
self.rounds[checker.name].append(checker.name in found)
|
|
124
|
+
found |= {checker.name: {} for checker in asking}
|
|
125
|
+
turns: list[list[Checker]] = [[checker] for checker in asking] if self.how.in_turn else [asking]
|
|
126
|
+
turn: list[Checker]
|
|
127
|
+
for turn in turns:
|
|
128
|
+
_asked(files, turn, found)
|
|
129
|
+
for checker in turn if self.how.in_turn else ():
|
|
130
|
+
checker.stop()
|
|
131
|
+
# An abandoned file has no hints: one abandoned before, or as it was asked (an earlier run's hang).
|
|
132
|
+
for checker in asking:
|
|
133
|
+
found[checker.name] = {path: {} for path in checker.abandoned & files.keys()} | found[
|
|
134
|
+
checker.name
|
|
135
|
+
]
|
|
136
|
+
for checker in asking if self.how.kept else ():
|
|
118
137
|
stored.store(
|
|
119
138
|
names[checker.name],
|
|
120
139
|
checker.root,
|
|
@@ -136,24 +155,56 @@ class Session:
|
|
|
136
155
|
"""
|
|
137
156
|
return [(checker.name, path) for checker in self.checkers for path in sorted(checker.abandoned)]
|
|
138
157
|
|
|
158
|
+
@property
|
|
159
|
+
def reused(self) -> list[tuple[str, int, int]]:
|
|
160
|
+
"""List the checkers some of whose rounds' hints were read from the store, not asked for.
|
|
161
|
+
|
|
162
|
+
Returns:
|
|
163
|
+
Each one's name, the rounds read from the store and all its rounds, in the order the
|
|
164
|
+
checkers were named.
|
|
165
|
+
|
|
166
|
+
"""
|
|
167
|
+
return [(name, sum(rounds), len(rounds)) for name, rounds in self.rounds.items() if any(rounds)]
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def _asked(files: Mapping[Path, str], checkers: Sequence["Checker"], found: dict[str, Found]) -> None:
|
|
171
|
+
"""Ask `checkers` about `files` at once, each over its servers (started as needed), into `found`."""
|
|
172
|
+
_start(files, checkers)
|
|
173
|
+
work: list[tuple[Checker, _Task]] = [
|
|
174
|
+
(checker, task) for checker in checkers for task in checker.tasks(files)
|
|
175
|
+
]
|
|
176
|
+
if work:
|
|
177
|
+
pool: ThreadPoolExecutor
|
|
178
|
+
with ThreadPoolExecutor(len(work)) as pool:
|
|
179
|
+
futures: list[Future[Found]] = [pool.submit(task) for _, task in work]
|
|
180
|
+
checker: Checker
|
|
181
|
+
future: Future[Found]
|
|
182
|
+
for (checker, _), future in zip(work, futures, strict=True):
|
|
183
|
+
found[checker.name].update(future.result())
|
|
184
|
+
|
|
139
185
|
|
|
140
186
|
def _start(files: Mapping[Path, str], checkers: Sequence["Checker"]) -> None:
|
|
141
187
|
"""Start the servers `checkers` need for `files` that aren't running yet, all at once.
|
|
142
188
|
|
|
143
189
|
Raises `HintError` if one can't be started; those that could be are shut down with the session.
|
|
144
190
|
"""
|
|
145
|
-
starting: list[Checker] = [
|
|
146
|
-
|
|
147
|
-
|
|
191
|
+
starting: list[Checker] = []
|
|
192
|
+
checker: Checker
|
|
193
|
+
wanted: int
|
|
194
|
+
for checker in checkers:
|
|
195
|
+
# None for no files: its heap is set as its first server starts.
|
|
196
|
+
if wanted := checker.wanted(files):
|
|
197
|
+
checker.share(wanted)
|
|
198
|
+
starting += [checker] * (wanted - len(checker.servers))
|
|
148
199
|
if not starting:
|
|
149
200
|
return
|
|
150
201
|
pool: ThreadPoolExecutor
|
|
151
202
|
with ThreadPoolExecutor(len(starting)) as pool:
|
|
152
203
|
futures: list[Future[Connection]] = [
|
|
153
|
-
pool.submit(Connection, checker.command, checker.root
|
|
204
|
+
pool.submit(Connection, checker.command, checker.root, checker.environment)
|
|
205
|
+
for checker in starting
|
|
154
206
|
]
|
|
155
207
|
failures: list[HintError] = []
|
|
156
|
-
checker: Checker
|
|
157
208
|
future: Future[Connection]
|
|
158
209
|
for checker, future in zip(starting, futures, strict=True):
|
|
159
210
|
try:
|
|
@@ -178,7 +229,7 @@ def _hints(checker: str, found: FileHints) -> Hints:
|
|
|
178
229
|
)
|
|
179
230
|
|
|
180
231
|
|
|
181
|
-
class Checker:
|
|
232
|
+
class Checker: # pylint: disable=too-many-instance-attributes
|
|
182
233
|
"""One checker's servers, and which of them has each file.
|
|
183
234
|
|
|
184
235
|
It has as many as its files need (one per `_FILES_PER_SERVER`), up to the command's `--jobs`, the
|
|
@@ -203,6 +254,19 @@ class Checker:
|
|
|
203
254
|
self.servers: list[Connection] = []
|
|
204
255
|
self.assigned: dict[Path, int] = {} # each file's server, by its place in `servers`
|
|
205
256
|
self.abandoned: set[Path] = set() # the files a server hung on twice: never asked about again
|
|
257
|
+
self.environment: dict[str, str] | None = None # its servers', where their heap is capped (`share`)
|
|
258
|
+
|
|
259
|
+
def share(self, servers: int) -> None:
|
|
260
|
+
"""Cap each server's heap at its share of the memory, as the first starts, where they run on Node.
|
|
261
|
+
|
|
262
|
+
Of `servers`' share, a tenth is left for what isn't the heap; never under a server's own
|
|
263
|
+
(`Server.memory`). Past the cap, basedpyright empties its caches, and else exits: restarted
|
|
264
|
+
then, its files asked about alone (`_alone`).
|
|
265
|
+
"""
|
|
266
|
+
if self.server.node and self.environment is None:
|
|
267
|
+
heap: int = max(self.server.memory, self.memory // servers * _HEAP // _WHOLE) >> 20
|
|
268
|
+
given: str = os.environ.get(_NODE_OPTIONS, "")
|
|
269
|
+
self.environment = {**os.environ, _NODE_OPTIONS: f"{given} --max-old-space-size={heap}".strip()}
|
|
206
270
|
|
|
207
271
|
def wanted(self, files: Mapping[Path, str]) -> int:
|
|
208
272
|
"""Count the servers `files` want, as many as fit in memory (never fewer than it has).
|
|
@@ -257,6 +321,9 @@ class Checker:
|
|
|
257
321
|
Returns:
|
|
258
322
|
Each file's hints.
|
|
259
323
|
|
|
324
|
+
Raises:
|
|
325
|
+
HintError: The server failed as no restart gets past (see `_restartable`).
|
|
326
|
+
|
|
260
327
|
"""
|
|
261
328
|
found: Found = {}
|
|
262
329
|
pending: list[Sent] = []
|
|
@@ -264,8 +331,18 @@ class Checker:
|
|
|
264
331
|
# Two batches in flight: the next is asked before the last's answers are read, so the server
|
|
265
332
|
# always has work while they're read and the next batch is taken.
|
|
266
333
|
for batch in itertools.chain([own] if own else [], iter(partial(_taken, batches), None)):
|
|
267
|
-
pending.append((batch, self.servers[index].ask(batch)))
|
|
268
334
|
self.assigned.update(dict.fromkeys(batch, index)) # each file's taken once: no two write one
|
|
335
|
+
try:
|
|
336
|
+
pending.append((batch, self.servers[index].ask(batch)))
|
|
337
|
+
except HintError as failure: # it has gone since its last answers: past its cap, say
|
|
338
|
+
if not self._restartable(failure):
|
|
339
|
+
raise
|
|
340
|
+
unanswered: dict[Path, str] = {
|
|
341
|
+
path: text for sent, _ in pending for path, text in sent.items()
|
|
342
|
+
}
|
|
343
|
+
pending.clear()
|
|
344
|
+
found.update(self._alone(index, unanswered | batch))
|
|
345
|
+
continue
|
|
269
346
|
if len(pending) > 1:
|
|
270
347
|
found.update(self._answers(index, pending))
|
|
271
348
|
if pending:
|
|
@@ -275,15 +352,21 @@ class Checker:
|
|
|
275
352
|
def _answers(self, index: int, pending: list[Sent]) -> Found:
|
|
276
353
|
"""Collect the answers to the first of `pending`'s batches from server `index`, and take it off.
|
|
277
354
|
|
|
278
|
-
If the server hangs, every pending file is asked about again
|
|
355
|
+
If the server hangs, or exits past its heap's cap, every pending file is asked about again
|
|
356
|
+
alone (see `_alone`).
|
|
279
357
|
|
|
280
358
|
Returns:
|
|
281
359
|
Each file's hints: the batch's, or every pending file's.
|
|
282
360
|
|
|
361
|
+
Raises:
|
|
362
|
+
HintError: The server failed as no restart gets past (see `_restartable`).
|
|
363
|
+
|
|
283
364
|
"""
|
|
284
365
|
try:
|
|
285
366
|
found: Found = self.servers[index].answers(pending[0][1])
|
|
286
|
-
except
|
|
367
|
+
except HintError as failure:
|
|
368
|
+
if not self._restartable(failure):
|
|
369
|
+
raise
|
|
287
370
|
unanswered: dict[Path, str] = {path: text for batch, _ in pending for path, text in batch.items()}
|
|
288
371
|
pending.clear()
|
|
289
372
|
return self._alone(index, unanswered)
|
|
@@ -291,10 +374,11 @@ class Checker:
|
|
|
291
374
|
return found
|
|
292
375
|
|
|
293
376
|
def _alone(self, index: int, files: Mapping[Path, str]) -> Found:
|
|
294
|
-
"""Restart server `index`, which hung, and ask it about each of `files` alone.
|
|
377
|
+
"""Restart server `index`, which hung or exited past its cap, and ask it about each of `files` alone.
|
|
295
378
|
|
|
296
379
|
A file it hangs on again is abandoned, with no hints, and the server restarted for the rest:
|
|
297
|
-
one file's analysis needn't cost every other file's hints.
|
|
380
|
+
one file's analysis needn't cost every other file's hints. As is one it exits on again past
|
|
381
|
+
its cap, though not remembered (see `remembered`): a larger share may fit it.
|
|
298
382
|
|
|
299
383
|
Returns:
|
|
300
384
|
Each file's hints.
|
|
@@ -311,9 +395,12 @@ class Checker:
|
|
|
311
395
|
server: Connection = self.servers[index]
|
|
312
396
|
try:
|
|
313
397
|
found.update(server.answers(server.ask({path: text})))
|
|
314
|
-
except
|
|
398
|
+
except HintError as hung:
|
|
399
|
+
if not self._restartable(hung):
|
|
400
|
+
raise
|
|
315
401
|
self.abandoned.add(path)
|
|
316
|
-
|
|
402
|
+
if isinstance(hung, HungError):
|
|
403
|
+
remembered.remember(self.name, self.command, text, path)
|
|
317
404
|
if len(self.abandoned) > _MOST_ABANDONED:
|
|
318
405
|
error: str = f"{self.name} hung on {len(self.abandoned)} file(s), each asked about alone"
|
|
319
406
|
raise HintError(error) from hung
|
|
@@ -321,10 +408,30 @@ class Checker:
|
|
|
321
408
|
self._restart(index)
|
|
322
409
|
return found
|
|
323
410
|
|
|
411
|
+
def stop(self) -> None:
|
|
412
|
+
"""Shut its servers down, at once: the next round starts others, which open each file anew."""
|
|
413
|
+
pool: ThreadPoolExecutor
|
|
414
|
+
# None where its turn had no file to ask about (a round with none, or every one abandoned).
|
|
415
|
+
with ThreadPoolExecutor(max(1, len(self.servers))) as pool:
|
|
416
|
+
_ = list(pool.map(Connection.close, self.servers))
|
|
417
|
+
self.servers.clear()
|
|
418
|
+
self.assigned.clear()
|
|
419
|
+
|
|
420
|
+
def _restartable(self, failure: HintError) -> bool:
|
|
421
|
+
"""Decide whether a server's `failure` is one a restart may get past: a hang, or an exit past its cap.
|
|
422
|
+
|
|
423
|
+
Returns:
|
|
424
|
+
Whether it is; any other failure stops the run.
|
|
425
|
+
|
|
426
|
+
"""
|
|
427
|
+
return isinstance(failure, HungError) or (
|
|
428
|
+
isinstance(failure, ExitedError) and self.environment is not None
|
|
429
|
+
)
|
|
430
|
+
|
|
324
431
|
def _restart(self, index: int) -> None:
|
|
325
432
|
"""Stop server `index` and start another in its place, which opens each file it's asked about."""
|
|
326
433
|
self.servers[index].close(graceful=False)
|
|
327
|
-
self.servers[index] = Connection(self.command, self.root)
|
|
434
|
+
self.servers[index] = Connection(self.command, self.root, self.environment)
|
|
328
435
|
|
|
329
436
|
|
|
330
437
|
def _taken(batches: "queue.SimpleQueue[dict[Path, str]]") -> dict[Path, str] | None:
|