Skip to content

Commit

Permalink
Support version v4.17.3 Jsonschema (#625)
Browse files Browse the repository at this point in the history
* Test installation with jsonschema v4.17.3

* Adding referencing in requirements

* Reintroduce Resolver

* Fixing typo

* Fixing typo

* Skipping coverage of validate args

* Relaxing constraint on jsonschema to >= 4.17.3

* Rename DEPRECATED to LEGACY, restrict v4.17 to v4.17.3

* Bump version to v0.16.1

* Handle hot fix of jsonschema v4.17

* add or condition on jsonschema version

* Defining jsonschema with condition

* Adding version to requirement

* Reverting requirement on jsonschema to >= 4.17.3

* Testing with 4.17.3

* Reverting jsonschema version to >=4.17.3

* Add extra assert

---------

Co-authored-by: HGSilveri <henrique.silverio@tecnico.ulisboa.pt>
  • Loading branch information
a-corni and HGSilveri authored Dec 14, 2023
1 parent 40ab0b5 commit 104801a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
2 changes: 1 addition & 1 deletion VERSION.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.16.0
0.16.1
18 changes: 16 additions & 2 deletions pulser-core/pulser/json/abstract_repr/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,23 @@
# limitations under the License.
"""Function for validation of JSON serialization to abstract representation."""
import json
from importlib.metadata import version
from typing import Literal

import jsonschema
from referencing import Registry, Resource

from pulser.json.abstract_repr import SCHEMAS
from pulser.json.abstract_repr import SCHEMAS, SCHEMAS_PATH

LEGACY_JSONSCHEMA = "4.18" > version("jsonschema") >= "4.17.3"
RESOLVER = (
jsonschema.validators.RefResolver(
base_uri=f"{SCHEMAS_PATH.resolve().as_uri()}/",
referrer=SCHEMAS["sequence"],
)
if LEGACY_JSONSCHEMA
else None
)
REGISTRY: Registry = Registry().with_resources(
[("device-schema.json", Resource.from_contents(SCHEMAS["device"]))]
)
Expand All @@ -37,5 +47,9 @@ def validate_abstract_repr(
obj = json.loads(obj_str)
validate_args = dict(instance=obj, schema=SCHEMAS[name])
if name == "sequence":
validate_args["registry"] = REGISTRY
if LEGACY_JSONSCHEMA: # pragma: no cover
validate_args["resolver"] = RESOLVER
else: # pragma: no cover
assert RESOLVER is None
validate_args["registry"] = REGISTRY
jsonschema.validate(**validate_args)
3 changes: 2 additions & 1 deletion pulser-core/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
jsonschema >= 4.18
jsonschema >= 4.17.3
referencing
matplotlib < 3.8
# Numpy 1.20 introduces type hints, 1.24.0 breaks matplotlib < 3.6.1
numpy >= 1.20, != 1.24.0
Expand Down
17 changes: 15 additions & 2 deletions tests/test_abstract_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@
AbstractReprEncoder,
abstract_repr,
)
from pulser.json.abstract_repr.validation import REGISTRY
from pulser.json.abstract_repr.validation import (
LEGACY_JSONSCHEMA,
REGISTRY,
RESOLVER,
)
from pulser.json.exceptions import AbstractReprError, DeserializeDeviceError
from pulser.parametrized.decorators import parametrize
from pulser.parametrized.paramobj import ParamObj
Expand Down Expand Up @@ -278,7 +282,16 @@ def validate_schema(instance):
encoding="utf-8",
) as f:
schema = json.load(f)
jsonschema.validate(instance=instance, schema=schema, registry=REGISTRY)
if LEGACY_JSONSCHEMA:
assert RESOLVER is not None
jsonschema.validate(
instance=instance, schema=schema, resolver=RESOLVER
)
else:
assert RESOLVER is None
jsonschema.validate(
instance=instance, schema=schema, registry=REGISTRY
)


class TestSerialization:
Expand Down

0 comments on commit 104801a

Please sign in to comment.