From 74d1b0618e74063d21968b3ce0f2eb3aea16ed35 Mon Sep 17 00:00:00 2001 From: Alexander Held Date: Thu, 18 Aug 2022 20:32:19 +0200 Subject: [PATCH 1/3] validate workspace specification before accessing parts of it --- src/pyhf/workspace.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/pyhf/workspace.py b/src/pyhf/workspace.py index 6339f71cc4..070f3966db 100644 --- a/src/pyhf/workspace.py +++ b/src/pyhf/workspace.py @@ -300,14 +300,15 @@ def __init__(self, spec, validate: bool = True, **config_kwargs): """ spec = copy.deepcopy(spec) - super().__init__(spec, channels=spec['channels']) self.schema = config_kwargs.pop('schema', 'workspace.json') self.version = config_kwargs.pop('version', spec.get('version', None)) # run jsonschema validation of input specification against the (provided) schema if validate: log.info(f"Validating spec against schema: {self.schema}") - schema.validate(self, self.schema, version=self.version) + schema.validate(spec, self.schema, version=self.version) + + super().__init__(spec, channels=spec['channels']) self.measurement_names = [] for measurement in self.get('measurements', []): From 37c6546ffda3a9e4d2b5d73b77d67a9d2d582251 Mon Sep 17 00:00:00 2001 From: Alexander Held Date: Fri, 19 Aug 2022 08:16:07 +0200 Subject: [PATCH 2/3] add test for invalid workspace specification --- tests/test_workspace.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test_workspace.py b/tests/test_workspace.py index 4ef6eed51a..8af44b4e7f 100644 --- a/tests/test_workspace.py +++ b/tests/test_workspace.py @@ -910,3 +910,10 @@ def test_workspace_without_validation(mocker, simplemodels_model_data): pyhf.Workspace(dict(ws), validate=False) assert pyhf.schema.validate.called is False + + +def test_workspace_invalid_specification(): + spec = {"channels": [{"name": "SR", "samples_wrong_name": []}]} + # ensure that an invalid specifications gets caught as such + with pytest.raises(pyhf.exceptions.InvalidSpecification): + pyhf.Workspace(spec) From 90e82a2c0dd3ce7e9a7ae55b4d81be6b388b1d8a Mon Sep 17 00:00:00 2001 From: Matthew Feickert Date: Mon, 22 Aug 2022 12:23:07 -0500 Subject: [PATCH 3/3] Add more context for test --- tests/test_workspace.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_workspace.py b/tests/test_workspace.py index 8af44b4e7f..6f1f380307 100644 --- a/tests/test_workspace.py +++ b/tests/test_workspace.py @@ -914,6 +914,7 @@ def test_workspace_without_validation(mocker, simplemodels_model_data): def test_workspace_invalid_specification(): spec = {"channels": [{"name": "SR", "samples_wrong_name": []}]} - # ensure that an invalid specifications gets caught as such + # Ensure that an invalid specifications gets caught as such + # before a KeyError: 'samples' could be reached. with pytest.raises(pyhf.exceptions.InvalidSpecification): pyhf.Workspace(spec)