"""Every shipped module must compile on the Python the Space actually runs. This exists because it already happened: a backslash inside an f-string expression is legal from 3.12 and a SyntaxError on 3.11. It passed every local check on a 3.14 interpreter, deployed green, and took the live Space down with RUNTIME_ERROR at import. The obvious guard does NOT work. `ast.parse(src, feature_version=(3, 11))` happily parses that exact file — feature_version gates a handful of AST features, not the f-string grammar. Anything built on it would have passed while the site was down, which is worse than no test. So this compiles the source with a real 3.11 interpreter. If one is not installed it skips loudly rather than pretending to have checked. """ import shutil import subprocess from pathlib import Path import pytest # The interpreter in the Space's Docker image. Change both when that changes. DEPLOY_PYTHON = "3.11" DEPLOY_BIN = shutil.which(f"python{DEPLOY_PYTHON}") ROOT = Path(__file__).resolve().parent.parent # compile(), not py_compile — a pure syntax check that writes no bytecode. _COMPILE = ("import sys; src=open(sys.argv[1], encoding='utf-8').read(); " "compile(src, sys.argv[1], 'exec')") SHIPPED = sorted( p for p in [*ROOT.glob("*.py"), *(ROOT / "core").glob("*.py")] if "isolate" not in str(p) and p.name != "__init__.py" ) pytestmark = pytest.mark.skipif( DEPLOY_BIN is None, reason=(f"python{DEPLOY_PYTHON} is not installed, so deployment-grammar " f"checking is impossible here. Install it (brew install " f"python@{DEPLOY_PYTHON}) — without it a syntax error that only " f"appears on the Space can be pushed with every test green."), ) def test_there_is_something_to_check(): assert SHIPPED, "no shipped modules found — the glob is wrong, not the code" @pytest.mark.parametrize("path", SHIPPED, ids=lambda p: p.name) def test_compiles_on_the_deployment_interpreter(path): """A real compile, not ast.parse — only the interpreter sees its own grammar.""" r = subprocess.run( [DEPLOY_BIN, "-c", _COMPILE, str(path)], capture_output=True, text=True, ) if r.returncode != 0: pytest.fail( f"{path.relative_to(ROOT)} compiles on this interpreter but not on " f"Python {DEPLOY_PYTHON}, which is what the Space runs:\n\n" f"{(r.stderr or r.stdout).strip()}\n\n" f"This would deploy green and crash at import." ) def test_the_check_actually_catches_the_bug_that_caused_the_outage(tmp_path): """A guard nobody has seen fail is a guard nobody should trust.""" bad = tmp_path / "regression.py" bad.write_text('link = "x"\ns = f"{\'