Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Remove os-dependent pieces of schema validation #2357

Merged
merged 6 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/pyhf/schema/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def load_schema(schema_id: str):
Args:
schema_id (str): Relative path to schema from :attr:`pyhf.schema.path`

Example:
Examples:
>>> import pyhf
>>> schema = pyhf.schema.load_schema("1.0.0/defs.json")
>>> type(schema)
Expand All @@ -31,6 +31,18 @@ def load_schema(schema_id: str):
...
pyhf.exceptions.SchemaNotFound: ...

>>> import pyhf
>>> import os
>>> schema = pyhf.schema.load_schema(f"1.0.0{os.sep}defs.json")
>>> type(schema)
<class 'dict'>
>>> schema.keys()
dict_keys(['$schema', '$id', 'definitions'])
>>> pyhf.schema.load_schema(f"0.0.0{os.sep}defs.json") # doctest: +ELLIPSIS
Traceback (most recent call last):
...
pyhf.exceptions.SchemaNotFound: ...

Returns:
schema (dict): The loaded schema.

Expand Down
13 changes: 9 additions & 4 deletions src/pyhf/schema/validator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import numbers
import os
from pathlib import Path
from typing import Mapping, Union

import jsonschema
Expand Down Expand Up @@ -70,12 +72,15 @@ def validate(

version = version or variables.SCHEMA_VERSION

schema = load_schema(f'{version}/{schema_name}')
schema = load_schema(str(Path(version).joinpath(schema_name)))

# note: trailing slash needed for RefResolver to resolve correctly
# note: trailing slash needed for RefResolver to resolve correctly and by
# design, pathlib strips trailing slashes. See ref below:
# * https://bugs.python.org/issue21039
# * https://github.com/python/cpython/issues/65238
resolver = jsonschema.RefResolver(
base_uri=f"file://{variables.schemas}/{version}/",
referrer=f"{schema_name}",
base_uri=f"{Path(variables.schemas).joinpath(version).as_uri()}{os.sep}",
referrer=schema_name,
store=variables.SCHEMA_CACHE,
)

Expand Down
Loading