Skip to content

Commit ea2a7bd

Browse files
jnsdlsclaude
andcommitted
fix: hint at pathlib.Path when load_schema gets a non-JSON str
load_schema parses a str as JSON text, so passing a file path as a str failed with a bare "Expecting value" decode error. The error is now still a JSONDecodeError, chained from the original, and says to pass a pathlib.Path to read a file. Closes #23 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent fecb718 commit ea2a7bd

2 files changed

Lines changed: 15 additions & 4 deletions

File tree

‎src/zero_server/schema.py‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,16 @@ class Schema(_SchemaModel):
5858

5959

6060
def load_schema(source: str | Path | Mapping[str, Any]) -> Schema:
61-
"""A schema from `JSON.stringify(schema)`: the JSON text, a path to it, or the parsed object."""
61+
"""A schema from `JSON.stringify(schema)`: the JSON text as a `str`, a `Path` to it, or the parsed object."""
6262
if isinstance(source, Path):
63-
source = source.read_text()
64-
if isinstance(source, str):
65-
source = json.loads(source)
63+
source = json.loads(source.read_text())
64+
elif isinstance(source, str):
65+
try:
66+
source = json.loads(source)
67+
except json.JSONDecodeError as e:
68+
raise json.JSONDecodeError(
69+
f"{e.msg} (a str is JSON text; pass a pathlib.Path to read a file)", e.doc, e.pos
70+
) from e
6671
return Schema.model_validate(source)
6772

6873

‎tests/unit/test_api.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ def test_load_schema_from_text_path_or_object(tmp_path: Path) -> None:
4343
assert load_schema(path) == load_schema(json.dumps(SCHEMA)) == load_schema(SCHEMA)
4444

4545

46+
def test_load_schema_str_that_is_not_json_hints_at_path() -> None:
47+
with pytest.raises(json.JSONDecodeError, match=r"pathlib\.Path") as raised:
48+
load_schema("schema.json")
49+
assert isinstance(raised.value.__cause__, json.JSONDecodeError)
50+
51+
4652
def test_name_mapper() -> None:
4753
schema = load_schema(SCHEMA)
4854
mapper = server_to_client(schema.tables)

0 commit comments

Comments
 (0)