Skip to content

Commit

Permalink
fill in defaults for dataclass
Browse files Browse the repository at this point in the history
  • Loading branch information
prismofeverything committed May 10, 2024
1 parent b27e5f3 commit 5c0b33e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
6 changes: 5 additions & 1 deletion bigraph_schema/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,9 +598,13 @@ def dataclass_any(schema, path, core):
subschema,
path + [key])

def default(subschema=subschema):
return core.default(subschema)

branches[key] = (
key,
branch)
branch,
field(default_factory=default))

dataclass = make_dataclass(
dataclass_name,
Expand Down
7 changes: 7 additions & 0 deletions bigraph_schema/type_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from pint import Quantity
from pprint import pformat as pf
from typing import Any, Tuple, Union, Optional, Mapping, Callable, NewType, get_origin, get_args
from dataclasses import asdict

from bigraph_schema.units import units, render_units_type
from bigraph_schema.react import react_divide_counts
Expand Down Expand Up @@ -481,6 +482,7 @@ def merge_state(before):
return state


# TODO: maybe all fields are optional?
def dataclass(self, schema, path=None):
path = path or []

Expand Down Expand Up @@ -4555,6 +4557,7 @@ def test_dataclass(core):
'c': 'boolean',
'x': 'string'}

# TODO: accept just a string instead of only a path
simple_dataclass = core.dataclass(
simple_schema,
['simple'])
Expand Down Expand Up @@ -4641,6 +4644,10 @@ def test_dataclass(core):
complex_dataclass,
complex_state)

complex_dict = asdict(complex_from)

# assert complex_dict == complex_state ?

assert complex_from.a['x']['oooo'] is None
assert len(complex_from.c.d['A']['inputs']['GGG'])
assert isinstance(complex_from.c.e, np.ndarray)
Expand Down

0 comments on commit 5c0b33e

Please sign in to comment.