Skip to content

Commit

Permalink
Merge pull request #50 from PSLmodels/simple-error-val2
Browse files Browse the repository at this point in the history
Fix bug with simple parameter values and error message construction
  • Loading branch information
hdoupe authored Apr 24, 2019
2 parents 10065a9 + 85feb69 commit cacc7d3
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 7 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ install:
- conda create -n paramtools-dev python=$TRAVIS_PYTHON_VERSION;
- source activate paramtools-dev
- conda env update -f environment.yml
- pip install -e .

# command to run tests
script:
Expand Down
2 changes: 2 additions & 0 deletions paramtools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
get_leaves,
ravel,
consistent_labels,
ensure_value_object,
)


Expand Down Expand Up @@ -62,4 +63,5 @@
"get_leaves",
"ravel",
"consistent_labels",
"ensure_value_object",
]
7 changes: 2 additions & 5 deletions paramtools/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,15 +434,12 @@ def _parse_errors(self, ve, params):
}

for pname, data in ve.messages.items():
param_data = utils.ensure_value_object(params[pname])
error_labels = []
formatted_errors = []
for ix, marshmessages in data.items():
error_labels.append(
{
k: v
for k, v in params[pname][ix].items()
if k != "value"
}
{k: v for k, v in param_data[ix].items() if k != "value"}
)
formatted_errors_ix = []
for _, messages in marshmessages.items():
Expand Down
5 changes: 5 additions & 0 deletions paramtools/tests/test_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@ def test_errors(self, TestParams):
exp_labels = {"min_int_param": [{}]}
assert excinfo.value.labels == exp_labels

params = TestParams()
adj = {"min_int_param": "abc"}
with pytest.raises(ValidationError) as excinfo:
params.adjust(adj)

def test_errors_choice_param(self, TestParams):
params = TestParams()
adjustment = {"str_choice_param": [{"value": "not a valid choice"}]}
Expand Down
17 changes: 16 additions & 1 deletion paramtools/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
from paramtools import get_leaves, ravel, consistent_labels
from paramtools import (
get_leaves,
ravel,
consistent_labels,
ensure_value_object,
)


def test_get_leaves():
Expand Down Expand Up @@ -55,3 +60,13 @@ def test_consistent_labels():

v = [{"label0": 1, "label1": 2, "value": 3}, {"label0": 4, "value": 6}]
assert consistent_labels(v) is None


def test_ensure_value_object():
assert ensure_value_object("hello") == [{"value": "hello"}]
assert ensure_value_object([{"value": "hello"}]) == [{"value": "hello"}]
assert ensure_value_object([1, 2, 3]) == [{"value": [1, 2, 3]}]
assert ensure_value_object([[1, 2, 3]]) == [{"value": [[1, 2, 3]]}]
assert ensure_value_object({"hello": "world"}) == [
{"value": {"hello": "world"}}
]
8 changes: 8 additions & 0 deletions paramtools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,11 @@ def consistent_labels(value_items):
if used != set(k for k in vo if k != "value"):
return None
return used


def ensure_value_object(vo):
if not isinstance(vo, list) or (
isinstance(vo, list) and not isinstance(vo[0], dict)
):
vo = [{"value": vo}]
return vo

0 comments on commit cacc7d3

Please sign in to comment.