Skip to content

Commit 68aed7c

Browse files
committed
Lazily import jsonschema in contract helpers
1 parent 95d39ea commit 68aed7c

1 file changed

Lines changed: 13 additions & 10 deletions

File tree

‎src/agent_machine/contracts.py‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@
66
from pathlib import Path
77
from typing import Any
88

9-
try:
10-
import jsonschema
11-
from jsonschema.validators import validator_for
12-
except ImportError as exc: # pragma: no cover - exercised in environments without deps
13-
raise RuntimeError(
14-
"Missing dependency: jsonschema. Install with `python -m pip install -r requirements-dev.txt`."
15-
) from exc
16-
179
from agent_machine.paths import repo_root_from_file
1810

1911

12+
def jsonschema_validator_for() -> Any:
13+
"""Import jsonschema lazily so non-validation commands stay dependency-light."""
14+
try:
15+
from jsonschema.validators import validator_for
16+
except ImportError as exc: # pragma: no cover - exercised in environments without deps
17+
raise RuntimeError(
18+
"Missing dependency: jsonschema. Install with `python -m pip install -r requirements-dev.txt`."
19+
) from exc
20+
return validator_for
21+
22+
2023
def repo_root() -> Path:
2124
"""Return the repository root for the current checkout/package context."""
2225
return repo_root_from_file(__file__)
@@ -59,15 +62,15 @@ def iter_json_files(directory: Path) -> list[Path]:
5962

6063
def check_schema(path: Path) -> dict[str, Any]:
6164
schema = load_json(path)
62-
validator_cls = validator_for(schema)
65+
validator_cls = jsonschema_validator_for()(schema)
6366
validator_cls.check_schema(schema)
6467
return schema
6568

6669

6770
def validate_instance(instance_path: Path, schema_path: Path) -> None:
6871
schema = load_json(schema_path)
6972
instance = load_json(instance_path)
70-
validator_cls = validator_for(schema)
73+
validator_cls = jsonschema_validator_for()(schema)
7174
validator_cls.check_schema(schema)
7275
validator = validator_cls(schema)
7376
errors = sorted(validator.iter_errors(instance), key=lambda err: list(err.path))

0 commit comments

Comments
 (0)