Skip to content

Commit

Permalink
rename Testing Schema, add Value
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-q committed Apr 24, 2024
1 parent a9e0df8 commit 837acde
Show file tree
Hide file tree
Showing 10 changed files with 785 additions and 103 deletions.
4 changes: 2 additions & 2 deletions hugr-py/src/hugr/serialization/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
PolyFuncType,
Type,
TypeRow,
SumTypeBase,
SumType,
TypeBound,
)

Expand Down Expand Up @@ -128,7 +128,7 @@ class SumValue(BaseModel):

c: Literal["Sum"] = Field("Sum", title="ValueTag")
tag: int
typ: SumTypeBase
typ: SumType
vs: list["Value"]

class Config:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
from typing import Any, Literal
from pydantic import BaseModel, Field
from .tys import Type, USize, SumTypeBase
from .tys import Type, USize, SumType
from .ops import Value


class HugrType(BaseModel):
"""A serializable representation of a Hugr Type. Intended for testing only."""
class TestingHugr(BaseModel):
"""A serializable representation of a Hugr Type, SumType, or Value. Intended for testing only."""

typ: Type | SumTypeBase
version: Literal["v1"] = "v1"
typ: Type | SumType | Value
version: Literal["v2"] = "v2"

@classmethod
def get_version(cls) -> str:
"""Return the version of the schema."""
return cls(typ=Type(USize())).version

class Config:
title = "HugrType"
title = "HugrTesting"
json_schema_extra = {
"required": ["typ"],
}
8 changes: 4 additions & 4 deletions hugr-py/src/hugr/serialization/tys.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,13 @@ class GeneralSum(BaseModel):
rows: list["TypeRow"]


class SumTypeBase(RootModel):
class SumType(RootModel):
root: Union[UnitSum, GeneralSum] = Field(discriminator="s")


class SumType(BaseModel):
class TaggedSumType(BaseModel):
t: Literal["Sum"] = "Sum"
st: SumTypeBase
st: SumType


# ----------------------------------------------
Expand Down Expand Up @@ -287,7 +287,7 @@ class Type(RootModel):
"""A HUGR type."""

root: Annotated[
Qubit | Variable | USize | FunctionType | Array | SumType | Opaque | Alias,
Qubit | Variable | USize | FunctionType | Array | TaggedSumType | Opaque | Alias,
WrapValidator(_json_custom_error_validator),
] = Field(discriminator="t")

Expand Down
2 changes: 1 addition & 1 deletion hugr/src/hugr/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ pub mod test {
};
static ref TYPE_SCHEMA: JSONSchema = {
let schema_val: serde_json::Value = serde_json::from_str(include_str!(
"../../../specification/schema/test_hugrtype_schema_v1.json"
"../../../specification/schema/testing_hugr_schema_v1.json"
))
.unwrap();
JSONSchema::options()
Expand Down
3 changes: 2 additions & 1 deletion hugr/src/hugr/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ impl<'a, 'b> ValidationContext<'a, 'b> {
// In tests we take the opportunity to verify that the hugr
// serialization round-trips. We verify the schema of the serialisation
// format only when an environment variable is set. This allows
// development while the schema is broken.
// a developer to modify the definition of serialised types locally
// without having to change the schema.
#[cfg(test)]
{
let test_schema = std::env::var("HUGR_TEST_SCHEMA").is_ok_and(|x| !x.is_empty());
Expand Down
4 changes: 2 additions & 2 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ check:
poetry run pre-commit run --all-files

# Run all the tests.
test language="[rust|python]": (_run_lang language \
"HUGR_TEST_SCHEMA=1 cargo test --all-features" \
test language="[rust|python]" : (_run_lang language \
"HUGR_TEST_SCHEMA=\"1\" cargo test --all-features" \
"poetry run pytest"
)

Expand Down
121 changes: 61 additions & 60 deletions poetry.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions scripts/generate_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from pydantic import TypeAdapter

from hugr.serialization import SerialHugr
from hugr.serialization.hugrtype import HugrType
from hugr.serialization.testing_hugr import TestingHugr


def write_schema(out_dir: Path, name_prefix: str, schema):
Expand All @@ -37,4 +37,4 @@ def write_schema(out_dir: Path, name_prefix: str, schema):
sys.exit(1)

write_schema(out_dir, "hugr_schema", SerialHugr)
write_schema(out_dir, "test_hugrtype_schema", HugrType)
write_schema(out_dir, "testing_hugr_schema", TestingHugr)
50 changes: 25 additions & 25 deletions specification/schema/hugr_schema_v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -1241,27 +1241,6 @@
"type": "object"
},
"SumType": {
"properties": {
"t": {
"const": "Sum",
"default": "Sum",
"enum": [
"Sum"
],
"title": "T",
"type": "string"
},
"st": {
"$ref": "#/$defs/SumTypeBase"
}
},
"required": [
"st"
],
"title": "SumType",
"type": "object"
},
"SumTypeBase": {
"discriminator": {
"mapping": {
"General": "#/$defs/GeneralSum",
Expand All @@ -1277,7 +1256,7 @@
"$ref": "#/$defs/GeneralSum"
}
],
"title": "SumTypeBase"
"title": "SumType"
},
"SumValue": {
"description": "A Sum variant For any Sum type where this value meets the type of the variant indicated by the tag.",
Expand All @@ -1296,7 +1275,7 @@
"type": "integer"
},
"typ": {
"$ref": "#/$defs/SumTypeBase"
"$ref": "#/$defs/SumType"
},
"vs": {
"items": {
Expand Down Expand Up @@ -1357,6 +1336,27 @@
"title": "Tag",
"type": "object"
},
"TaggedSumType": {
"properties": {
"t": {
"const": "Sum",
"default": "Sum",
"enum": [
"Sum"
],
"title": "T",
"type": "string"
},
"st": {
"$ref": "#/$defs/SumType"
}
},
"required": [
"st"
],
"title": "TaggedSumType",
"type": "object"
},
"TailLoop": {
"description": "Tail-controlled loop.",
"properties": {
Expand Down Expand Up @@ -1466,7 +1466,7 @@
"I": "#/$defs/USize",
"Opaque": "#/$defs/Opaque",
"Q": "#/$defs/Qubit",
"Sum": "#/$defs/SumType",
"Sum": "#/$defs/TaggedSumType",
"V": "#/$defs/Variable"
},
"propertyName": "t"
Expand All @@ -1488,7 +1488,7 @@
"$ref": "#/$defs/Array"
},
{
"$ref": "#/$defs/SumType"
"$ref": "#/$defs/TaggedSumType"
},
{
"$ref": "#/$defs/Opaque"
Expand Down
Loading

0 comments on commit 837acde

Please sign in to comment.