[pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
This commit is contained in:
pre-commit-ci[bot] 2026-07-26 17:29:04 +00:00
commit 848dfa2764
4 changed files with 107 additions and 44 deletions

View file

@ -59,8 +59,13 @@ def md(*lines):
def code(src):
return {"cell_type": "code", "metadata": {}, "execution_count": None,
"outputs": [], "source": [src]}
return {
"cell_type": "code",
"metadata": {},
"execution_count": None,
"outputs": [],
"source": [src],
}
def nb(*cells):

View file

@ -51,16 +51,16 @@ def test_the_refresh_is_still_detached(sync: str):
# The whole point of the child is that a 60s ls-remote + clone must not delay
# container startup. A fix that simply made the refresh synchronous would
# pass every other test here and regress boot time.
assert re.search(r'UNSLOTH_NB_REFRESH_CHILD=1 "\$0" >/dev/null 2>&1 &', sync), (
"the GitHub refresh must stay a detached child"
)
assert re.search(
r'UNSLOTH_NB_REFRESH_CHILD=1 "\$0" >/dev/null 2>&1 &', sync
), "the GitHub refresh must stay a detached child"
def test_an_exclusive_lock_serialises_the_two_processes(sync: str):
assert "lock_acquire()" in sync and "lock_release()" in sync
assert re.search(r"flock -w \"\$LOCK_WAIT\" 9", sync), (
"the lock must be a real exclusive flock, and must not block forever"
)
assert re.search(
r"flock -w \"\$LOCK_WAIT\" 9", sync
), "the lock must be a real exclusive flock, and must not block forever"
def test_the_lock_is_taken_before_anything_mutates_the_tree(sync: str):
@ -91,9 +91,9 @@ def test_the_parent_finalizes_before_it_forks(sync: str):
def test_finalize_runs_at_most_once(sync: str):
block = sync[sync.index("finalize() {") : sync.index("trap 'finalize; lock_release' EXIT")]
assert '[ "$_FINALIZED" = "1" ] && return 0' in block, (
"the explicit pre-fork call and the EXIT trap must not strip twice"
)
assert (
'[ "$_FINALIZED" = "1" ] && return 0' in block
), "the explicit pre-fork call and the EXIT trap must not strip twice"
assert "_FINALIZED=1" in block
@ -113,8 +113,9 @@ def test_the_child_does_not_repeat_the_parents_finalize(sync: str):
def test_the_child_re_arms_the_finalize_only_after_it_copies(sync: str):
tail = sync[sync.index("refreshed from GitHub") :]
assert re.search(r'if \[ "\$updated" -gt 0 \]; then\s*\n\s*_FINALIZED=0\s*\n\s*finalize',
tail), (
assert re.search(
r'if \[ "\$updated" -gt 0 \]; then\s*\n\s*_FINALIZED=0\s*\n\s*finalize', tail
), (
"freshly copied notebooks arrive with the upstream Colab intro and have "
"to be stripped, but only when something was actually copied"
)

View file

@ -73,7 +73,11 @@ def shim(tmp_path, monkeypatch):
return mod
def _run(shim, args, tool = "pip"):
def _run(
shim,
args,
tool = "pip",
):
"""Return the args that reached the real tool after `install`, or None when
the shim no-op'd. The always-injected protected-constraints pair is dropped."""
argv = ["uv", "pip", "install", *args] if tool == "uv" else ["pip", "install", *args]
@ -99,26 +103,54 @@ def _run(shim, args, tool = "pip"):
# --------------------------------------------------------------------------
@pytest.mark.parametrize(
"pkg",
["trl", "peft", "datasets", "accelerate", "torchao", "torchcodec",
"huggingface-hub", "tokenizers", "safetensors"],
[
"trl",
"peft",
"datasets",
"accelerate",
"torchao",
"torchcodec",
"huggingface-hub",
"tokenizers",
"safetensors",
],
)
def test_training_stack_is_protected(shim, pkg):
assert pkg in shim._KEEP, (
f"{pkg} is baked and tested; a notebook pin replacing it invalidates the image"
)
assert (
pkg in shim._KEEP
), f"{pkg} is baked and tested; a notebook pin replacing it invalidates the image"
def test_the_original_gpu_stack_is_still_protected(shim):
for pkg in ["torch", "torchvision", "torchaudio", "triton", "xformers",
"vllm", "bitsandbytes", "unsloth", "unsloth-zoo"]:
for pkg in [
"torch",
"torchvision",
"torchaudio",
"triton",
"xformers",
"vllm",
"bitsandbytes",
"unsloth",
"unsloth-zoo",
]:
assert pkg in shim._KEEP
def test_unrelated_packages_are_not_swept_in(shim):
# The criterion is "invalidates the tested stack", not "a notebook mentions
# it". These are all installed by shipped notebooks and must stay installable.
for pkg in ["snac", "causal-conv1d", "mamba-ssm", "omegaconf", "timm",
"librosa", "trackio", "open-spiel", "protobuf", "sentencepiece"]:
for pkg in [
"snac",
"causal-conv1d",
"mamba-ssm",
"omegaconf",
"timm",
"librosa",
"trackio",
"open-spiel",
"protobuf",
"sentencepiece",
]:
assert pkg not in shim._KEEP, f"{pkg} must still install for the notebooks that need it"
@ -133,8 +165,14 @@ def test_the_shipped_trl_cell_installs_nothing(shim):
def test_a_mixed_cell_keeps_only_the_unbaked_package(shim):
execd = _run(
shim,
["--no-deps", "trl==0.22.2", "peft==0.14.0", "datasets==3.0.0",
"accelerate==1.0.0", UNBAKED],
[
"--no-deps",
"trl==0.22.2",
"peft==0.14.0",
"datasets==3.0.0",
"accelerate==1.0.0",
UNBAKED,
],
)
assert execd == ["--no-deps", UNBAKED], execd
@ -148,8 +186,9 @@ def test_cuda_matched_wheels_are_not_replaced_by_pypi_builds(shim):
def test_transformers_companions_cannot_desynchronise_the_sidecars(shim):
# Each sidecar ships its own matched huggingface_hub/tokenizers/safetensors;
# replacing the base-venv copies desynchronises every sidecar at once.
assert _run(shim, ["huggingface_hub==0.30.0", "tokenizers==0.20.0",
"safetensors==0.4.0"]) is None
assert (
_run(shim, ["huggingface_hub==0.30.0", "tokenizers==0.20.0", "safetensors==0.4.0"]) is None
)
def test_an_unbaked_package_still_installs(shim):

View file

@ -50,9 +50,19 @@ COMPAT_PATH = REPO_ROOT / "docker" / "unsloth_nb_compat.py"
# Every distinct transformers pin across the 433 shipped notebooks, and the
# sidecar each must resolve to once 4.57.6 and 5.3.0 are gone.
SHIPPED_PINS = [
"4.48", "4.52.3", "4.55.4", "4.56.1", "4.56.2",
"4.57.0", "4.57.1", "4.57.3", "5.2.0", "5.3.0",
"5.5.0", "5.10.1", "5.11.0",
"4.48",
"4.52.3",
"4.55.4",
"4.56.1",
"4.56.2",
"4.57.0",
"4.57.1",
"4.57.3",
"5.2.0",
"5.3.0",
"5.5.0",
"5.10.1",
"5.11.0",
]
@ -125,13 +135,13 @@ def test_build_verifies_every_sidecar_against_the_baked_vllm(sidecar_block: str)
def test_build_verification_needs_no_gpu(sidecar_block: str):
# `import unsloth` raises NotImplementedError("cannot find any torch
# accelerator") on the build host, so it can never be the gate.
assert "import unsloth" not in sidecar_block, (
"the sidecar gate must not import unsloth: the build host has no GPU"
)
assert (
"import unsloth" not in sidecar_block
), "the sidecar gate must not import unsloth: the build host has no GPU"
def test_an_unverifiable_sidecar_is_deleted_not_shipped(sidecar_block: str):
assert re.search(r'DROPPED', sidecar_block), "a failed candidate must be reported"
assert re.search(r"DROPPED", sidecar_block), "a failed candidate must be reported"
assert re.search(r'rm -rf "\$DEST"', sidecar_block), (
"a sidecar the baked vLLM cannot import must be removed, not shipped: it "
"can never be selected safely and it costs image size"
@ -139,9 +149,9 @@ def test_an_unverifiable_sidecar_is_deleted_not_shipped(sidecar_block: str):
def test_build_records_the_selection_floor(sidecar_block: str):
assert ".vllm_min_transformers" in sidecar_block, (
"the lowest verified version must be recorded for unsloth_nb_compat"
)
assert (
".vllm_min_transformers" in sidecar_block
), "the lowest verified version must be recorded for unsloth_nb_compat"
assert "sort -V | head -1" in sidecar_block, "the floor is the LOWEST survivor"
@ -176,12 +186,19 @@ def test_floor_is_read_back(fixed_root):
"pin, expected",
[
# every pin below the floor clamps UP to the lowest eligible sidecar
("4.48", "t_5_5_0"), ("4.52.3", "t_5_5_0"), ("4.55.4", "t_5_5_0"),
("4.56.1", "t_5_5_0"), ("4.56.2", "t_5_5_0"), ("4.57.0", "t_5_5_0"),
("4.57.1", "t_5_5_0"), ("4.57.3", "t_5_5_0"), ("5.2.0", "t_5_5_0"),
("4.48", "t_5_5_0"),
("4.52.3", "t_5_5_0"),
("4.55.4", "t_5_5_0"),
("4.56.1", "t_5_5_0"),
("4.56.2", "t_5_5_0"),
("4.57.0", "t_5_5_0"),
("4.57.1", "t_5_5_0"),
("4.57.3", "t_5_5_0"),
("5.2.0", "t_5_5_0"),
("5.3.0", "t_5_5_0"),
# at and above the floor, the ceiling still decides
("5.5.0", "t_5_5_0"), ("5.10.1", "t_5_10_2"),
("5.5.0", "t_5_5_0"),
("5.10.1", "t_5_10_2"),
# newer than every sidecar -> the baked transformers
("5.11.0", None),
],
@ -196,9 +213,10 @@ def test_no_shipped_pin_can_reach_an_incompatible_sidecar(stale_root):
for pin in SHIPPED_PINS:
got = compat.sidecar_for(pin)
name = Path(got).name if got else None
assert name not in ("t_4_57_6", "t_5_3_0"), (
f"pin {pin} selected {name}, which the baked vLLM cannot import"
)
assert name not in (
"t_4_57_6",
"t_5_3_0",
), f"pin {pin} selected {name}, which the baked vLLM cannot import"
def test_model_tier_fallback_is_clamped_too(stale_root):