Skip to content

Commit

Permalink
fix failures from voluptuous update
Browse files Browse the repository at this point in the history
Mostly these have to do with validation of default inputs.

Also, alecthomas/voluptuous#397, which I assume is a bug
  • Loading branch information
ChristopherChudzicki committed Jun 27, 2019
1 parent 2f80efc commit 3f8246f
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 12 deletions.
4 changes: 2 additions & 2 deletions docs/grading_math/sampling.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ Sample real matrices of a specific shape and norm. (`RealMatrices` uses the Frob
>>> sampler = RealMatrices(shape=[3, 2], norm=[5, 10])
>>> # the default is shape=[2, 2] and norm=[1, 5]
>>> default_sampler = RealMatrices()
>>> default_sampler
RealMatrices({'norm': [1, 5], 'shape': (2, 2)})
>>> default_sampler == RealMatrices(shape=[2, 2], norm=[1, 5])
True

```

Expand Down
4 changes: 2 additions & 2 deletions mitxgraders/formulagrader/formulagrader.py
Original file line number Diff line number Diff line change
Expand Up @@ -947,8 +947,8 @@ def schema_config(self):
Required('user_functions', default={}): {Extra: is_callable},
Required('tolerance', default='5%'): Any(PercentageString, NonNegative(Number)),
Required('samples', default=1): 1,
Required('variables', default=[]): [],
Required('numbered_vars', default=[]): [],
Required('variables', default=[]): All(Length(max=0), []),
Required('numbered_vars', default=[]): All(Length(max=0), []),
Required('sample_from', default={}): {},
Required('failable_evals', default=0): 0
})
6 changes: 3 additions & 3 deletions mitxgraders/formulagrader/matrixgrader.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from voluptuous import Required, Any
from mitxgraders.exceptions import InputTypeError
from mitxgraders.formulagrader.formulagrader import FormulaGrader
from mitxgraders.helpers.validatorfuncs import NonNegative
from mitxgraders.helpers.validatorfuncs import NonNegative, Nullable
from mitxgraders.helpers.calc import MathArray, within_tolerance, identity
from mitxgraders.helpers.calc.exceptions import (
MathArrayShapeError as ShapeError, MathArrayError, DomainError, ArgumentShapeError)
Expand Down Expand Up @@ -68,8 +68,8 @@ class MatrixGrader(FormulaGrader):
def schema_config(self):
schema = super(MatrixGrader, self).schema_config
return schema.extend({
Required('identity_dim', default=None): NonNegative(int),
Required('max_array_dim', default=1): NonNegative(int),
Required('identity_dim', default=None): Nullable(NonNegative(int)),
Required('max_array_dim', default=1): Nullable(NonNegative(int)),
Required('negative_powers', default=True): bool,
Required('shape_errors', default=True): bool,
Required('suppress_matrix_messages', default=False): bool,
Expand Down
4 changes: 2 additions & 2 deletions mitxgraders/helpers/calc/specify_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""
from numbers import Number
from voluptuous import Schema, Invalid, Required, Any
from mitxgraders.helpers.validatorfuncs import is_shape_specification
from mitxgraders.helpers.validatorfuncs import is_shape_specification, Nullable
from mitxgraders.baseclasses import ObjectWithSchema
from mitxgraders.helpers.calc.exceptions import ArgumentShapeError, ArgumentError
from mitxgraders.helpers.calc.math_array import (
Expand Down Expand Up @@ -189,7 +189,7 @@ class SpecifyDomain(ObjectWithSchema):
Required('input_shapes'): [Schema(
Any(is_shape_specification(), 'square')
)],
Required('display_name', default=None): str
Required('display_name', default=None): Nullable(str)
})

def __init__(self, config=None, **kwargs):
Expand Down
6 changes: 6 additions & 0 deletions mitxgraders/helpers/validatorfuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,9 @@ def is_shape_specification(min_dim=1, max_dim=None):
),
Length(min=min_dim, max=max_dim),
)

def Nullable(schema):
"""
Indicates that a value could be None or satisfy schema.
"""
return Any(None, schema)
6 changes: 3 additions & 3 deletions tests/formulagrader/test_formulagrader.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,15 +660,15 @@ def test_ng_config():
samples=2
)

expect = "not a valid value for dictionary value @ data\['variables'\]. Got \['x'\]"
expect = "length of value must be at most 0 for dictionary value @ data\['variables'\]. Got \['x'\]"
with raises(Error, match=expect):
NumericalGrader(
answers="1",
variables=["x"]
)

expect = "not a valid value for dictionary value @ data\['sample_from'\]. " + \
"Got {'x': RealInterval\({'start': 1, 'stop': 5}\)}"
expect = ("extra keys not allowed @ data\['sample_from'\]\['x'\]. Got "
"RealInterval\({'start': 1, 'stop': 5}\)")
with raises(Error, match=expect):
NumericalGrader(
answers="1",
Expand Down

0 comments on commit 3f8246f

Please sign in to comment.