From ad56f81ca84bc139bb318a5ac7c63dd7089987d6 Mon Sep 17 00:00:00 2001 From: Giordon Stark Date: Tue, 24 Oct 2023 14:37:00 -0700 Subject: [PATCH 1/6] fix schema loading by making it strictly os-independent --- src/pyhf/schema/validator.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/pyhf/schema/validator.py b/src/pyhf/schema/validator.py index 1fbc36c686..a773e79289 100644 --- a/src/pyhf/schema/validator.py +++ b/src/pyhf/schema/validator.py @@ -7,6 +7,8 @@ from pyhf import tensor from pyhf.schema import variables from pyhf.schema.loader import load_schema +from pathlib import Path +import os def _is_array_or_tensor(checker, instance): @@ -70,12 +72,14 @@ 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 bewlow: + # - https://bugs.python.org/issue21039 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, ) From 8df78f280ea4d848a7462167f6e669e5406769f9 Mon Sep 17 00:00:00 2001 From: Giordon Stark Date: Tue, 24 Oct 2023 14:37:16 -0700 Subject: [PATCH 2/6] add an additional example that is os-independent --- src/pyhf/schema/loader.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/pyhf/schema/loader.py b/src/pyhf/schema/loader.py index 920766c4dc..0562af0960 100644 --- a/src/pyhf/schema/loader.py +++ b/src/pyhf/schema/loader.py @@ -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) @@ -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) + + >>> 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. From 21683f03be2b3cca18b3bac485839fec6bd53090 Mon Sep 17 00:00:00 2001 From: Matthew Feickert Date: Tue, 24 Oct 2023 16:50:13 -0500 Subject: [PATCH 3/6] isort as this file is already isorted --- src/pyhf/schema/validator.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pyhf/schema/validator.py b/src/pyhf/schema/validator.py index a773e79289..c7a887b87b 100644 --- a/src/pyhf/schema/validator.py +++ b/src/pyhf/schema/validator.py @@ -1,4 +1,6 @@ import numbers +import os +from pathlib import Path from typing import Mapping, Union import jsonschema @@ -7,8 +9,6 @@ from pyhf import tensor from pyhf.schema import variables from pyhf.schema.loader import load_schema -from pathlib import Path -import os def _is_array_or_tensor(checker, instance): From 8fc232668adf73e9359beddf0c98e68ceda6c919 Mon Sep 17 00:00:00 2001 From: Matthew Feickert Date: Tue, 24 Oct 2023 16:52:06 -0500 Subject: [PATCH 4/6] Fix typo and add link to CPython issue on GitHub as it is still being discussed --- src/pyhf/schema/validator.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/pyhf/schema/validator.py b/src/pyhf/schema/validator.py index c7a887b87b..c64f869c44 100644 --- a/src/pyhf/schema/validator.py +++ b/src/pyhf/schema/validator.py @@ -75,8 +75,9 @@ def validate( schema = load_schema(str(Path(version).joinpath(schema_name))) # note: trailing slash needed for RefResolver to resolve correctly and by - # design, pathlib strips trailing slashes. See ref bewlow: - # - https://bugs.python.org/issue21039 + # 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"{Path(variables.schemas).joinpath(version).as_uri()}{os.sep}", referrer=schema_name, From c6bbee61b413d1cb2d1b1bdf847029a8e047558d Mon Sep 17 00:00:00 2001 From: Giordon Stark Date: Tue, 24 Oct 2023 15:07:35 -0700 Subject: [PATCH 5/6] no need for os.sep --- src/pyhf/schema/validator.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/pyhf/schema/validator.py b/src/pyhf/schema/validator.py index c64f869c44..2540a3d002 100644 --- a/src/pyhf/schema/validator.py +++ b/src/pyhf/schema/validator.py @@ -1,5 +1,4 @@ import numbers -import os from pathlib import Path from typing import Mapping, Union @@ -79,7 +78,7 @@ def validate( # * https://bugs.python.org/issue21039 # * https://github.com/python/cpython/issues/65238 resolver = jsonschema.RefResolver( - base_uri=f"{Path(variables.schemas).joinpath(version).as_uri()}{os.sep}", + base_uri=f"{Path(variables.schemas).joinpath(version).as_uri()}/", referrer=schema_name, store=variables.SCHEMA_CACHE, ) From 05335e21ab13381951600db787f016d03ef1b883 Mon Sep 17 00:00:00 2001 From: Giordon Stark Date: Tue, 24 Oct 2023 15:11:05 -0700 Subject: [PATCH 6/6] drop example for os.sep, not needed --- src/pyhf/schema/loader.py | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/src/pyhf/schema/loader.py b/src/pyhf/schema/loader.py index 0562af0960..920766c4dc 100644 --- a/src/pyhf/schema/loader.py +++ b/src/pyhf/schema/loader.py @@ -19,7 +19,7 @@ def load_schema(schema_id: str): Args: schema_id (str): Relative path to schema from :attr:`pyhf.schema.path` - Examples: + Example: >>> import pyhf >>> schema = pyhf.schema.load_schema("1.0.0/defs.json") >>> type(schema) @@ -31,18 +31,6 @@ 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) - - >>> 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.