mitodl / mitx-grading-library

The MITx Grading Library, a python grading library for edX
https://edge.edx.org/courses/course-v1:MITx+grading-library+examples/
BSD 3-Clause "New" or "Revised" License
14 stars 9 forks source link

Update volutpuous #209

Closed ChristopherChudzicki closed 5 years ago

ChristopherChudzicki commented 5 years ago

Updates voluptuous to version 0.11.5.

Largely, this update is motivated by my desire to validate default values in dictionaries. In particular, some of our validators also transform the input. I was surprised today by:

from mitxgraders import RealMatrices

x = RealMatrices()
y = RealMatrices({'norm': [1, 5], 'shape': (2, 2)})

print(x) # RealMatrices({'norm': [1, 5], 'shape': (2, 2)})
print(y) # RealMatrices({'norm': {'start': 1, 'stop': 5}, 'shape': (2, 2)})
jolyonb commented 5 years ago

This all looks fine to me, but Travis is taking a long time (which I suspect means it's going to fail miserably).

Largely, this update is motivated by my desire to validate default values in dictionaries. In particular, some of our validators also transform the input. I was surprised today by:

from mitxgraders import RealMatrices

x = RealMatrices()
y = RealMatrices({'norm': [1, 5], 'shape': (2, 2)})

print(x) # RealMatrices({'norm': [1, 5], 'shape': (2, 2)})
print(y) # RealMatrices({'norm': {'start': 1, 'stop': 5}, 'shape': (2, 2)})

I can't quite parse your statement or your example. Can you elaborate?

jolyonb commented 5 years ago

Ok, Travis just succeeded. I'm happy to merge, but if you can elaborate, that would be great ;-) (Also, I thought you'd be more excited about the inclusion of sets in voluptuous...)

ChristopherChudzicki commented 5 years ago

Can you elaborate?

@jolyonb The only difference between x = RealMatrices() and y = RealMatrices({'norm': [1, 5], 'shape': (2, 2)}) is that y had the default values explicitly passed. I was surprised that the configuration dictionaries were not equal. In x the default values were not being transformed, but in y since they are explicitly passed, the config values are validated+transformed.

("Annoyed" is maybe a better word than "surprised", since I knew that some our validators transform and old voluptuous did not validate default values.)

ChristopherChudzicki commented 5 years ago

(To be clearer, x.config['norm']=[1, 5] whereas y.config['norm'] = { 'start': 1, 'stop': 5}

jolyonb commented 5 years ago

Ah, I see. So when you said "default values in dictionaries", you didn't mean dictionary entries, but the configuration dictionary as a whole?

ChristopherChudzicki commented 5 years ago

So when you said "default values in dictionaries", you didn't mean dictionary entries, but the configuration dictionary as a whole?

I think I meant the individual entries. For example, in the example below, the config dict as a whole is validated in the sense that all keys are validated, except the ones where default values are used by voluptuous to populate the dict.

But maybe we're trying to say the same thing 😛

from voluptuous import Schema, All, Required

schema = Schema({
    Required('a', default='cat'): All(str, lambda s: s.upper()),
    Required('b', default=0): int
})

print(schema({'b': 5}))
# 0.10.5: {'a': 'cat', 'b': 5} 5 is verified to be an int, but 'ca't is NOT upper-cased.
# 0.11.5: {'a': 'CAT', 'b': 5} 5 is verified, AND the value 'CAT' is uppercase
jolyonb commented 5 years ago

I think we are. I was confused because I thought you were thinking of something like schema({'b': {'anotherdict': something}}) (ie, a dictionary for an individual element).