-
Notifications
You must be signed in to change notification settings - Fork 240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
'coerce' still ignores 'nullable': True #427
Comments
this behaviour is to be expected as would not coercing values that are |
sorry for not being very verbose on this. i gave it some thoughts: the behaviour you describe is to be expected because the current implementation works like this: to comply with what i understand as your requirement, it would be simpler and clearer: when a field has my concern with that second approach is, that there would be no way to coerce a a good counter-argument against my concern would be, that such resolutions should be handled with a i haven't looked into the implications if that ignorance would be changed, but my guts tell me that this would be the clearest concept:
does this make sense? would that break current uses? |
Good argument Perhaps the current behavior is good, but please describe it more explicitly in the documentation to avoid confusion in the future :)
Dunno |
okay, i investigated the 'cleaner' solution and it is doable. (if anyone wants to take a peek, i can open a pr.)
yes. or maybe not sure. it breaks the tests |
@funkyfuture The reasoning behind So in the test the document is always As for the other two, def _test_default_none_nonnullable(default):
bar_schema = {'type': 'string', 'nullable': False}
bar_schema.update(default)
schema = {'foo': {'type': 'string'}, 'bar': bar_schema}
document = {'foo': 'foo_value', 'bar': None}
expected = {'foo': 'foo_value', 'bar': 'bar_value'}
assert_normalized(document, expected, schema) But that's surely a matter of definition if in this case of an intially invalid field value ( |
cool, after fixing the tests, only i still would argue that the default setter should be called in any case and it is up to itself to consider a field's value and other constraints like the heat is really making everything a little harder, and delegating seems a good strategy to not mix things up ;-). i'll see that i work out an issue description for Cerberus 2. @andreymal would you propose an update of the documentation to describe the situation as it is now? @dkellner would you provide a bug fix for Cerberus 1.2 and 1.3? here is what i did: # after the imports, also usable by test_default_setter_existent
def must_not_be_called(*args, **kwargs):
raise RuntimeError('This shall not be called.')
…
@mark.parametrize('default',
({'default': 'bar_value'},
{'default_setter': must_not_be_called})
)
def test_default_none_nullable(default):
bar_schema = {'type': 'string', 'nullable': True}
bar_schema.update(default)
schema = {'foo': {'type': 'string'}, 'bar': bar_schema}
document = {'foo': 'foo_value', 'bar': None}
assert_normalized(document, document.copy(), schema)
@mark.parametrize('default',
({'default': 'bar_value'},
{'default_setter': lambda doc: 'bar_value'})
)
def test_default_none_not_nullable(default):
bar_schema = {'type': 'string', 'nullable': False}
bar_schema.update(default)
schema = {'foo': {'type': 'string'}, 'bar': bar_schema}
document = {'foo': 'foo_value', 'bar': None}
expected = {'foo': 'foo_value', 'bar': 'bar_value'}
assert_normalized(document, expected, schema) |
@funkyfuture You're delegating to Vietnam here, so much for the heat ;-). I will take a look at it and get back to you until wednesday. |
@funkyfuture Just to be sure, you want the behaviour you described before, right? So far I've refactored the tests using
|
not sure which of the previously behaviours you mean. ;-) anyway, no, for now we shouldn't change any of that. just fix your tests (and possibly code to comply w/ them). |
#269 was fixed, but only for integer:
Just replace integer to string:
Expected behavior:
{'foo': None}
Actual behavior:
{'foo': 'None'}
So I still have to use a workaround
'coerce': lambda x: str(x) if x is not None else None
But now I'm not sure if it's a bug or a feature (duplicate of #142?). If this is a feature, please describe this behavior in the documentation
The text was updated successfully, but these errors were encountered: