Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 8 additions & 23 deletions paramtools/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ class RangeSchema(Schema):
}
"""

_min = fields.Field(attribute="min", data_key="min")
_max = fields.Field(attribute="max", data_key="max")
step = fields.Field()
_min = fields.Raw(attribute="min", data_key="min")
_max = fields.Raw(attribute="max", data_key="max")
step = fields.Raw()
level = fields.String(validate=[validate.OneOf(["warn", "error"])])


class ChoiceSchema(Schema):
choices = fields.List(fields.Field)
choices = fields.List(fields.Raw)
level = fields.String(validate=[validate.OneOf(["warn", "error"])])


Expand All @@ -53,9 +53,9 @@ class ValueValidatorSchema(Schema):


class IsSchema(Schema):
equal_to = fields.Field(required=False)
greater_than = fields.Field(required=False)
less_than = fields.Field(required=False)
equal_to = fields.Raw(required=False)
greater_than = fields.Raw(required=False)
less_than = fields.Raw(required=False)

@validates_schema
def just_one(self, data, **kwargs):
Expand Down Expand Up @@ -107,15 +107,12 @@ class BaseParamSchema(Schema):
data_key="type",
)
number_dims = fields.Integer(required=False, load_default=0)
value = fields.Field(required=True) # will be specified later
value = fields.Raw(required=True) # will be specified later
validators = fields.Nested(
ValueValidatorSchema(), required=False, load_default={}
)
indexed = fields.Boolean(required=False)

class Meta:
ordered = True


class EmptySchema(Schema):
"""
Expand All @@ -126,15 +123,6 @@ class EmptySchema(Schema):
pass


class OrderedSchema(Schema):
"""
Same as `EmptySchema`, but preserves the order of its fields.
"""

class Meta:
ordered = True


class ValueObject(fields.Nested):
"""
Schema for value objects
Expand Down Expand Up @@ -182,9 +170,6 @@ class BaseValidatorSchema(Schema):
class.
"""

class Meta:
ordered = True

WRAPPER_MAP = {
"range": "_get_range_validator",
"date_range": "_get_range_validator",
Expand Down
9 changes: 3 additions & 6 deletions paramtools/schema_factory.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from marshmallow import fields
from marshmallow import fields, Schema

from paramtools.schema import (
OrderedSchema,
BaseValidatorSchema,
ValueObject,
get_type,
Expand Down Expand Up @@ -67,17 +66,15 @@ def schemas(self):
# if not isinstance(v["value"], list):
# v["value"] = [{"value": v["value"]}]

validator_dict[k] = type(
"ValidatorItem", (OrderedSchema,), classattrs
)
validator_dict[k] = type("ValidatorItem", (Schema,), classattrs)

classattrs = {"value": ValueObject(validator_dict[k], many=True)}
param_dict[k] = type(
"IndividualParamSchema", (self.BaseParamSchema,), classattrs
)

classattrs = {k: fields.Nested(v) for k, v in param_dict.items()}
DefaultsSchema = type("DefaultsSchema", (OrderedSchema,), classattrs)
DefaultsSchema = type("DefaultsSchema", (Schema,), classattrs)
defaults_schema = DefaultsSchema()

classattrs = {
Expand Down
13 changes: 3 additions & 10 deletions paramtools/tests/test_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ def get_defaults(self):
assert params.hello_world == "hello world"
assert params.label_grid == {"somelabel": [0, 1, 2, 3, 4, 5]}


def test_schema_not_dropped(self, defaults_spec_path):
with open(defaults_spec_path, "r") as f:
defaults_ = json.loads(f.read())
Expand Down Expand Up @@ -379,14 +378,6 @@ def test_specification(self, TestParams, defaults_spec_path):

assert spec1["min_int_param"] == exp["min_int_param"]["value"]

def test_is_ordered(self, TestParams):
params = TestParams()
spec1 = params.specification()
assert isinstance(spec1, OrderedDict)

spec2 = params.specification(meta_data=True, serializable=True)
assert isinstance(spec2, OrderedDict)

def test_specification_query(self, TestParams):
params = TestParams()
spec1 = params.specification()
Expand Down Expand Up @@ -1047,7 +1038,9 @@ def test_range_validation_on_list_param(self, TestParams):
]
}
params.adjust(adj, raise_errors=False)
exp = ["float_list_param[label0=zero, label1=1] [np.float64(-1.0), np.float64(1.0)] < min 0 "]
exp = [
"float_list_param[label0=zero, label1=1] [np.float64(-1.0), np.float64(1.0)] < min 0 "
]

assert params.errors["float_list_param"] == exp

Expand Down
Loading