vm-tests: the typecheck fails a step that rebinds a function of its script
A VM test script and `lib/k3s.py` share one namespace, so a step that assigns `pod = ...` replaces the library's `pod()` for every later call and the test dies mid-run with `TypeError: 'dict' object is not callable`. `ty` and `ruff --select F` accept a module-level rebinding, so this cost a full VM round on #6947. `vm-test-shadow-scan.py` parses the script and fails when a name bound at module level (assignment, `for` or `with` target, `import ... as`, walrus, `except ... as`) is also a module-level `def`. It looks into `with`, `if`, `for`, `while` and `try` bodies, where `with subtest(...)` steps live, and not into function or comprehension bodies. Every `vm-test-typecheck-<name>` runs it on the script alone. `vm-test-typecheck-shadow-control` passes only when it rejects each form of rebinding and accepts the same names where they are local. The one existing case, `sources` in `k8s-services`, is renamed `cred_sources`. Closes #6951
This commit is contained in:
parent
358a13752c
commit
132d876668
1 changed files with 175 additions and 4 deletions
|
|
@ -74,9 +74,89 @@ let
|
|||
ty check testScriptWithTypes
|
||||
# driver.nix:91-99
|
||||
ruff check --select F testScriptWithTypes
|
||||
# What neither of them sees: a step that rebinds a function the script
|
||||
# defines. The script alone, so a name of the prelude or a hint isn't read
|
||||
# as the script's own.
|
||||
echo -n "$testScript" > testScriptOnly
|
||||
${shadowScan} testScriptOnly
|
||||
touch $out
|
||||
'';
|
||||
|
||||
# `vm-test-shadow-scan.py` as a command; the scan reads nothing of the machine
|
||||
# it runs on, so `-I`.
|
||||
shadowScan = "${pkgs.python3}/bin/python3 -I ${./vm-test-shadow-scan.py}";
|
||||
|
||||
# Succeeds only when the scan rejects every way a step rebinds a function of
|
||||
# its script and accepts the same names where they are local. The first
|
||||
# script is accepted whole: if it were not, the rejections below could be the
|
||||
# scan failing on anything.
|
||||
shadowControl =
|
||||
let
|
||||
script = body: pkgs.writeText "script.py" body;
|
||||
accepted = script ''
|
||||
def pod(name):
|
||||
return {"name": name}
|
||||
|
||||
def other():
|
||||
pod = 1 # local to the function
|
||||
return [pod for pod in range(3)] # a comprehension has its own scope
|
||||
|
||||
with subtest("a variable with a name of its own"):
|
||||
result = pod("a")
|
||||
'';
|
||||
rejected = {
|
||||
"a plain assignment" = ''
|
||||
def pod(name):
|
||||
return {}
|
||||
pod = pod("a")
|
||||
'';
|
||||
"an assignment inside a subtest" = ''
|
||||
def pod(name):
|
||||
return {}
|
||||
with subtest("x"):
|
||||
pod = pod("a")
|
||||
'';
|
||||
"a for target" = ''
|
||||
def pod(name):
|
||||
return {}
|
||||
for pod in range(3):
|
||||
pass
|
||||
'';
|
||||
"a with target" = ''
|
||||
def pod(name):
|
||||
return {}
|
||||
with open("x") as pod:
|
||||
pass
|
||||
'';
|
||||
"an import" = ''
|
||||
def pod(name):
|
||||
return {}
|
||||
import json as pod
|
||||
'';
|
||||
"an except target" = ''
|
||||
def pod(name):
|
||||
return {}
|
||||
try:
|
||||
pass
|
||||
except Exception as pod:
|
||||
pass
|
||||
'';
|
||||
};
|
||||
in
|
||||
pkgs.runCommand "vm-test-typecheck-shadow-control" { } ''
|
||||
${shadowScan} ${accepted}
|
||||
${lib.concatStrings (
|
||||
lib.mapAttrsToList (what: body: ''
|
||||
if ${shadowScan} ${script body} 2> scan.err; then
|
||||
echo "the scan accepted ${what}" >&2
|
||||
exit 1
|
||||
fi
|
||||
grep -q '`pod` is bound at module level' scan.err
|
||||
'') rejected
|
||||
)}
|
||||
touch $out
|
||||
'';
|
||||
|
||||
# Succeeds only when `ty` rejects an `Optional` dereference and three calls on
|
||||
# the stub machines of `hintsFor`. If `test_driver` stops resolving, every
|
||||
# name is `Unknown`, the real checks pass on anything, and this one fails.
|
||||
|
|
@ -151,4 +231,5 @@ in
|
|||
(lib.mapAttrs' (n: t: lib.nameValuePair "vm-test-typecheck-${n}" (typecheck n t)) tests)
|
||||
// {
|
||||
vm-test-typecheck-control = control;
|
||||
vm-test-typecheck-shadow-control = shadowControl;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue