From 9f3f1cec3be5dddbefd505fa2b60dfb75c0d46b1 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Fri, 6 Nov 2026 08:19:36 +0100 Subject: [PATCH] fix: validate_schemas names its jsonschema install Without jsonschema the documented command stopped on an ImportError traceback. Run as a command it now prints the install pipeline.py names for the same step, with the format-nongpl extra, and exits 1. An import from the tests still raises ImportError, which they skip on. --- scripts/validate_schemas.py | 10 +++++++++- tests/test_schema_semantic_gate.py | 26 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/scripts/validate_schemas.py b/scripts/validate_schemas.py index 5380f64c6..6bb3167c0 100644 --- a/scripts/validate_schemas.py +++ b/scripts/validate_schemas.py @@ -14,7 +14,15 @@ from pathlib import Path, PurePosixPath import yaml from common import source_yaml_paths, yaml_load -from jsonschema import Draft202012Validator, FormatChecker + +try: + from jsonschema import Draft202012Validator, FormatChecker +except ImportError: + # Tests import this module and skip on the ImportError, the command + # names the install pipeline.py names for the same step. + if __name__ != "__main__": + raise + sys.exit('Error: jsonschema required (pip install "jsonschema[format-nongpl]")') ROOT = Path(__file__).resolve().parent.parent SCHEMAS = ROOT / "schemas" diff --git a/tests/test_schema_semantic_gate.py b/tests/test_schema_semantic_gate.py index 2710455ac..9ccc9692c 100644 --- a/tests/test_schema_semantic_gate.py +++ b/tests/test_schema_semantic_gate.py @@ -120,5 +120,31 @@ class DataRootIsKeyedBySystem(unittest.TestCase): self.assertTrue(self._errors({})) +class WithoutJsonschema(unittest.TestCase): + """The documented command stopped on a bare ImportError traceback that + named neither the install nor the format-nongpl extra.""" + + def test_the_command_names_the_install(self): + import os + import subprocess + + scratch = REPO_ROOT / "tmp" / "tests" + scratch.mkdir(parents=True, exist_ok=True) + with tempfile.TemporaryDirectory(dir=scratch) as directory: + stub = Path(directory) / "jsonschema" + stub.mkdir() + (stub / "__init__.py").write_text( + 'raise ImportError("No module named jsonschema")\n', encoding="utf-8" + ) + env = {**os.environ, "PYTHONPATH": directory} + proc = subprocess.run( + [sys.executable, "scripts/validate_schemas.py", "--source-only"], + capture_output=True, text=True, check=False, cwd=REPO_ROOT, env=env, + ) + self.assertEqual(proc.returncode, 1) + self.assertNotIn("Traceback", proc.stderr) + self.assertIn('pip install "jsonschema[format-nongpl]"', proc.stderr) + + if __name__ == "__main__": unittest.main()