Skip to content

Commit

Permalink
fix!: serialisation fixes (#997)
Browse files Browse the repository at this point in the history
These are the serialisation fixes from the `proptest` branch.

There we have tested all of the schema roundtrips, with the exception
of:
* OpDef
* CustomConst

BREAKING CHANGE: serialisation format
  • Loading branch information
doug-q committed May 7, 2024
1 parent acca7bf commit 9ce6e49
Show file tree
Hide file tree
Showing 12 changed files with 342 additions and 220 deletions.
31 changes: 21 additions & 10 deletions hugr-py/src/hugr/serialization/tys.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,6 @@ class TypeParam(RootModel):
# ------------------------------------------


class CustomTypeArg(ConfiguredBaseModel):
typ: None # TODO
value: str


class TypeTypeArg(ConfiguredBaseModel):
tya: Literal["Type"] = "Type"
ty: "Type"
Expand All @@ -122,24 +117,36 @@ class BoundedNatArg(ConfiguredBaseModel):

class OpaqueArg(ConfiguredBaseModel):
tya: Literal["Opaque"] = "Opaque"
arg: CustomTypeArg
typ: "Opaque"
value: Any


class SequenceArg(ConfiguredBaseModel):
tya: Literal["Sequence"] = "Sequence"
args: list["TypeArg"]
elems: list["TypeArg"]


class ExtensionsArg(ConfiguredBaseModel):
tya: Literal["Extensions"] = "Extensions"
es: ExtensionSet


class VariableArg(BaseModel):
tya: Literal["Variable"] = "Variable"
idx: int
cached_decl: TypeParam


class TypeArg(RootModel):
"""A type argument."""

root: Annotated[
TypeTypeArg | BoundedNatArg | OpaqueArg | SequenceArg | ExtensionsArg,
TypeTypeArg
| BoundedNatArg
| OpaqueArg
| SequenceArg
| ExtensionsArg
| VariableArg,
WrapValidator(_json_custom_error_validator),
] = Field(discriminator="tya")

Expand Down Expand Up @@ -273,13 +280,17 @@ def join(*bs: "TypeBound") -> "TypeBound":
class Opaque(ConfiguredBaseModel):
"""An opaque Type that can be downcasted by the extensions that define it."""

t: Literal["Opaque"] = "Opaque"
extension: ExtensionId
id: str # Unique identifier of the opaque type.
args: list[TypeArg]
bound: TypeBound


class TaggedOpaque(ConfiguredBaseModel):
t: Literal["Opaque"] = "Opaque"
o: Opaque


class Alias(ConfiguredBaseModel):
"""An Alias Type"""

Expand Down Expand Up @@ -309,7 +320,7 @@ class Type(RootModel):
| FunctionType
| Array
| TaggedSumType
| Opaque
| TaggedOpaque
| Alias,
WrapValidator(_json_custom_error_validator),
] = Field(discriminator="t")
Expand Down
5 changes: 1 addition & 4 deletions hugr/src/hugr/serialize/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::extension::prelude::{BOOL_T, PRELUDE_ID, QB_T, USIZE_T};
use crate::extension::simple_op::MakeRegisteredOp;
use crate::extension::{EMPTY_REG, PRELUDE_REGISTRY};
use crate::hugr::hugrmut::sealed::HugrMutInternals;
use crate::hugr::NodeType;
use crate::ops::custom::{ExtensionOp, OpaqueOp};
use crate::ops::{self, Value};
use crate::ops::{dataflow::IOTrait, Input, Module, Noop, Output, DFG};
Expand All @@ -22,9 +21,7 @@ use itertools::Itertools;
use jsonschema::{Draft, JSONSchema};
use lazy_static::lazy_static;
use portgraph::LinkView;
use portgraph::{
multiportgraph::MultiPortGraph, Hierarchy, LinkMut, PortMut, PortView, UnmanagedDenseMap,
};
use portgraph::{multiportgraph::MultiPortGraph, Hierarchy, LinkMut, PortMut, UnmanagedDenseMap};
use rstest::rstest;

const NAT: Type = crate::extension::prelude::USIZE_T;
Expand Down
1 change: 0 additions & 1 deletion hugr/src/hugr/validate/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,6 @@ fn test_polymorphic_load() -> Result<(), Box<dyn std::error::Error>> {
#[cfg(feature = "extension_inference")]
mod extension_tests {
use super::*;
use crate::builder::ModuleBuilder;
use crate::extension::ExtensionSet;
use crate::macros::const_extension_ids;

Expand Down
2 changes: 1 addition & 1 deletion hugr/src/ops/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ pub type ValueNameRef = str;

#[cfg(test)]
mod test {
use super::{Value, ValueName};
use super::Value;
use crate::builder::test::simple_dfg_hugr;
use crate::{
builder::{BuildError, DFGBuilder, Dataflow, DataflowHugr},
Expand Down
6 changes: 3 additions & 3 deletions hugr/src/types/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub(super) enum SerSimpleType {
G(Box<FunctionType>),
Sum { st: SumType },
Array { inner: Box<SerSimpleType>, len: u64 },
Opaque(CustomType),
Opaque { o: CustomType },
Alias(AliasDecl),
V { i: usize, b: TypeBound },
}
Expand All @@ -29,7 +29,7 @@ impl From<Type> for SerSimpleType {
// TODO short circuiting for array.
let Type(value, _) = value;
match value {
TypeEnum::Extension(c) => SerSimpleType::Opaque(c),
TypeEnum::Extension(o) => SerSimpleType::Opaque { o },
TypeEnum::Alias(a) => SerSimpleType::Alias(a),
TypeEnum::Function(sig) => SerSimpleType::G(sig),
TypeEnum::Variable(i, b) => SerSimpleType::V { i, b },
Expand All @@ -48,7 +48,7 @@ impl From<SerSimpleType> for Type {
SerSimpleType::Array { inner, len } => {
array_type(TypeArg::BoundedNat { n: len }, (*inner).into())
}
SerSimpleType::Opaque(custom) => Type::new_extension(custom),
SerSimpleType::Opaque { o } => Type::new_extension(o),
SerSimpleType::Alias(a) => Type::new_alias(a),
SerSimpleType::V { i, b } => Type::new_var_use(i, b),
}
Expand Down
2 changes: 2 additions & 0 deletions hugr/src/types/type_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ pub enum TypeArg {
///Instance of [TypeParam::Opaque] An opaque value, stored as serialized blob.
Opaque {
#[allow(missing_docs)]
#[serde(flatten)]
arg: CustomTypeArg,
},
/// Instance of [TypeParam::List] or [TypeParam::Tuple], defined by a
Expand All @@ -171,6 +172,7 @@ pub enum TypeArg {
/// or [TypeArg::Extensions] - see [TypeArg::new_var_use]
Variable {
#[allow(missing_docs)]
#[serde(flatten)]
v: TypeArgVariable,
},
}
Expand Down
Loading

0 comments on commit 9ce6e49

Please sign in to comment.