Skip to content

Commit

Permalink
fix + format
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-q committed Apr 30, 2024
1 parent 0bd4f6f commit e094512
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 131 deletions.
11 changes: 8 additions & 3 deletions hugr-py/src/hugr/serialization/tys.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class TypeParam(RootModel):
# --------------- TypeArg ------------------
# ------------------------------------------


class TypeTypeArg(BaseModel):
tya: Literal["Type"] = "Type"
ty: "Type"
Expand Down Expand Up @@ -125,6 +126,7 @@ class ExtensionsArg(BaseModel):
tya: Literal["Extensions"] = "Extensions"
es: ExtensionSet


class VariableArg(BaseModel):
tya: Literal["Variable"] = "Variable"
idx: int
Expand All @@ -135,7 +137,12 @@ class TypeArg(RootModel):
"""A type argument."""

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

Expand Down Expand Up @@ -266,8 +273,6 @@ def join(*bs: "TypeBound") -> "TypeBound":
return res




class Opaque(BaseModel):
"""An opaque Type that can be downcasted by the extensions that define it."""

Expand Down
22 changes: 12 additions & 10 deletions hugr/src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,16 +544,18 @@ mod test {
fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
let vars = any::<HashSet<usize>>();
let extensions = any::<HashSet<ExtensionId>>();
(vars,extensions).prop_map(|(vars,extensions)| {
let mut r = Self::new();
for v in vars {
r.insert_type_var(v);
}
for e in extensions {
r.insert(&e)
}
r
}).boxed()
(vars, extensions)
.prop_map(|(vars, extensions)| {
let mut r = Self::new();
for v in vars {
r.insert_type_var(v);
}
for e in extensions {
r.insert(&e)
}
r
})
.boxed()
}
}
}
6 changes: 3 additions & 3 deletions hugr/src/hugr/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use regex::Regex;
use smol_str::SmolStr;
use thiserror::Error;

static PATH_COMPONENT_REGEX_STR: &'static str = r"[\w--\d]\w*";
static PATH_COMPONENT_REGEX_STR: &str = r"[\w--\d]\w*";
lazy_static! {
pub static ref PATH_REGEX_STR: String = format!(r"^{0}(\.{0})*$", PATH_COMPONENT_REGEX_STR);
pub static ref PATH_REGEX: Regex = Regex::new(&self::PATH_REGEX_STR).unwrap();
Expand Down Expand Up @@ -102,8 +102,8 @@ mod test {
type Parameters = ();
type Strategy = BoxedStrategy<Self>;
fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
use proptest::collection::{vec, SizeRange};
let component_strategy = prop::string::string_regex(&PATH_COMPONENT_REGEX_STR)
use proptest::collection::vec;
let component_strategy = prop::string::string_regex(PATH_COMPONENT_REGEX_STR)
.unwrap()
.boxed();
vec(component_strategy.clone(), 1..4)
Expand Down
2 changes: 1 addition & 1 deletion hugr/src/hugr/serialize/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::builder::{
test::closed_dfg_root_hugr, Container, DFGBuilder, Dataflow, DataflowHugr,
DataflowSubContainer, HugrBuilder, ModuleBuilder,
};
use proptest::prelude::*;
use crate::extension::prelude::{BOOL_T, QB_T, USIZE_T};
use crate::extension::simple_op::MakeRegisteredOp;
use crate::extension::{EMPTY_REG, PRELUDE_REGISTRY};
Expand All @@ -26,6 +25,7 @@ use portgraph::LinkView;
use portgraph::{
multiportgraph::MultiPortGraph, Hierarchy, LinkMut, PortMut, PortView, UnmanagedDenseMap,
};
use proptest::prelude::*;
use rstest::rstest;

const NAT: Type = crate::extension::prelude::USIZE_T;
Expand Down
4 changes: 3 additions & 1 deletion hugr/src/ops/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ mod test {
fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
let name = proptest::string::string_regex(r".+").unwrap();
let bound = any::<TypeBound>();
(name,bound).prop_map(|(name,bound)| Self::new(name,bound)).boxed()
(name, bound)
.prop_map(|(name, bound)| Self::new(name, bound))
.boxed()
}
}
}
38 changes: 17 additions & 21 deletions hugr/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,18 +449,18 @@ pub(crate) fn check_typevar_decl(
pub(crate) mod test {
use super::*;
use proptest::prelude::*;
use crate::extension::ExtensionSet;

use crate::{extension::prelude::USIZE_T, ops::AliasDecl};

use crate::types::TypeBound;

#[derive(Clone,Copy,Debug)]
#[derive(Clone, Copy, Debug)]
pub struct TypeDepth(usize);

impl TypeDepth {
pub fn descend(&self) -> Self {
if self.leaf() {
self.clone()
*self
} else {
Self(self.0 - 1)
}
Expand All @@ -483,53 +483,50 @@ pub(crate) mod test {
}
}


pub fn to_typerow(type_enums: impl IntoIterator<Item=Type>) -> TypeRow {
pub fn to_typerow(type_enums: impl IntoIterator<Item = Type>) -> TypeRow {
type_enums.into_iter().collect_vec().into()

}

impl Arbitrary for super::TypeEnum {
type Parameters = TypeDepth;
type Strategy = BoxedStrategy<Self>;
fn arbitrary_with(depth: Self::Parameters) -> Self::Strategy {
dbg!("Arbitrary<TypeEnum>: {}", depth);
prop_oneof! [
prop_oneof![
any_with::<CustomType>(depth).prop_map(Self::Extension),
any::<AliasDecl>().prop_map(Self::Alias),
any::<(usize,TypeBound)>().prop_map(|(us,tb)| Self::Variable(us,tb)),
any::<(usize, TypeBound)>().prop_map(|(us, tb)| Self::Variable(us, tb)),
any_with::<FunctionType>(depth).prop_map(|x| Self::Function(Box::new(x))),
any_with::<SumType>(depth).prop_map(Self::Sum),
].boxed()
]
.boxed()
}
}

impl Arbitrary for super::SumType {
type Parameters = TypeDepth;
type Strategy = BoxedStrategy<Self>;
fn arbitrary_with(depth: Self::Parameters) -> Self::Strategy {
use proptest::collection::{vec,SizeRange};
dbg!("Arbitrary<SumType>: {}", depth);
use proptest::collection::vec;
let unit_strat = any::<u8>().prop_map(Self::new_unary);
let general_strat = vec(any_with::<TypeRow>(depth), 0..3).prop_map(SumType::new);
let recurse_weight = if depth.leaf() { 0 } else { 1 };
prop_oneof![
1 => unit_strat,
recurse_weight => general_strat
].boxed()
if depth.leaf() {
unit_strat.boxed()
} else {
general_strat.boxed()
}
}
}

impl Arbitrary for super::Type {
type Parameters = TypeDepth;
type Strategy = BoxedStrategy<Self>;
fn arbitrary_with(depth: Self::Parameters) -> Self::Strategy {
dbg!("Arbitrary<Type>: {}", depth);
any_with::<TypeEnum>(depth.descend()).prop_map(Self::new).boxed()
any_with::<TypeEnum>(depth.descend())
.prop_map(Self::new)
.boxed()
}
}


#[test]
fn construct() {
let t: Type = Type::new_tuple(vec![
Expand Down Expand Up @@ -559,5 +556,4 @@ pub(crate) mod test {
let pred_direct = SumType::Unit { size: 2 };
assert_eq!(pred1, pred_direct.into())
}

}
3 changes: 1 addition & 2 deletions hugr/src/types/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ mod test {
type Parameters = crate::types::test::TypeDepth;
type Strategy = BoxedStrategy<Self>;
fn arbitrary_with(depth: Self::Parameters) -> Self::Strategy {
use proptest::collection::{size_range, vec};
use proptest::collection::vec;
let extension = any::<ExtensionId>();
let id = prop::string::string_regex(r".+")
.unwrap()
Expand All @@ -161,7 +161,6 @@ mod test {
vec(any_with::<TypeArg>(depth.descend()), 0..3).boxed()
};
let bound = any::<TypeBound>();
dbg!("Arbitrary<CustomType>: {}", depth);
(id, args, extension, bound)
.prop_map(|(id, args, extension, bound)| Self::new(id, args, extension, bound))
.boxed()
Expand Down
8 changes: 7 additions & 1 deletion hugr/src/types/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,13 @@ mod test {
let input = any_with::<TypeRow>(depth);
let output = any_with::<TypeRow>(depth);
let extension = any::<ExtensionSet>();
(input,output,extension).prop_map(|(input,output,extension_reqs)| FunctionType {input,output,extension_reqs}).boxed()
(input, output, extension)
.prop_map(|(input, output, extension_reqs)| FunctionType {
input,
output,
extension_reqs,
})
.boxed()
}
}
}
Loading

0 comments on commit e094512

Please sign in to comment.