Extend schema validation to support tensor types
Summary
Schema validation for lists looks just at regular python lists. It should be possible to extend it to support the tensor types that pyhf supports.
We can get this working if each tensor backend gives the tensor type: (list, numpy_backend.array_type, jax_backend.array_type, ...).
I'm not sure if we can or can't rely on the backend here. We've got the actual tensor object that we need to validate against... and the validator won't necessarily have access to the current backend -- but we can teach it that if needed.
isinstance(instance, list) or isinstance(instance, pyhf.tensorlib.array_type)
and this should fix some common issues where we need to pass in validate=False explicitly to build the models...
Additional Information
>>> from jsonschema import Draft6Validator
>>> from jsonschema.validators import extend
>>> import numpy as np
>>> def is_array_or_tensor(checker, instance):
... return isinstance(instance, (list, np.ndarray))
...
>>> type_checker = Draft6Validator.TYPE_CHECKER
>>> type_checker = Draft6Validator.TYPE_CHECKER.redefine('array', is_array_or_tensor)
>>> Validator = extend(Draft6Validator, type_checker=type_checker)
>>> validator_foo_required = Validator(schema={"type": "array"})
>>> example = [1, 2, 3]
>>> example_np = np.array([1,2,3])
>>> isinstance(example_np, np.ndarray)
True
>>> validator_foo_required.validate(example)
>>> validator_foo_required.validate(example_np)
Inspired by Julian/jsonschema#727
Code of Conduct
- [X] I agree to follow the Code of Conduct
Also related: #882.