-
Notifications
You must be signed in to change notification settings - Fork 215
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
Wrong priority for Optional param #23
Comments
So you propose switching around the priorities? I wonder, what impact that would have on other cases... |
I am afraid of that impact will bite too.Currently, I think "hasattr(s, 'validate')"(Optional, And, Or...) is more specifical than "is type"(str, object...) |
Same issue here. But in my case, even worse! Here is what I have: import schema
s = schema.Schema({
'a': Use(int),
'b': Use(int),
schema.Optional('c'): Use(int),
schema.Optional(object): object,
}) Here I expect that keys = sorted([(schema.priority(k), k) for k, v in s._schema.items()])
import pprint; pprint.pprint(keys)
It means, that |
I also have the same issue. I'm trying to validate some query arguments from a web request: s = Schema({
Optional('start'): Use(int),
basestring: object
})
s.validate({'start': '1'})
{'start': '1'} # matched basestring: object
As already pointed out by @apieceofredcloth, I would suggest to increase the priority of the Another solution may be to have the classes derived from I can create a pull request if needed. |
This isn't fully fixed by #52. Here is a unit test that (sometimes) reproduces the problem i'm seeing on my machine. I'm not sure why it doesn't consistently hit?
Test output:
The reason for this is the ordering. I printed out the ordered keys and their relative priority with this diff:
Output:
The correct ordering should be: I whipped together a quick patch, but it feels dirty and I don't think it fixes the general case...
|
These days, I have encountered the same problem as below def test_optional_key_convert_failed_randomly_while_with_another_optional_object():
"""
In this test, created_at string "2015-10-10 00:00:00" is expected to be converted to a datetime instance.
- it works when the schema is
s = Schema({
'created_at': _datetime_validator,
Optional(basestring): object,
})
- but when wrapping the key 'created_at' with Optional, it fails randomly
:return:
"""
import datetime
datetime_fmt = '%Y-%m-%d %H:%M:%S'
_use_datetime = lambda i: datetime.datetime.strptime(i, datetime_fmt)
_datetime_validator = Or(None, Use(_use_datetime))
s = Schema({
Optional('created_at'): _datetime_validator,
Optional(basestring): object,
})
data = {
'created_at': '2015-10-10 00:00:00'
}
validated_data = s.validate(data)
# is expected to be converted to a datetime instance, but fails randomly(most of the time)
# assert isinstance(validated_data['created_at'], datetime.datetime)
assert isinstance(validated_data['created_at'], basestring) |
I think value of id should be converted to integer 23 instead of string '23'("by Use(int)").But "str: str" has a higher priority than "Optional('id')", so it just ignores the "Use(int)" statement.
I check the code about priority
Optional has a 'validate' attribute which is inherited from Schema class, so it return 4 here, higher than 3 of the str case.
The text was updated successfully, but these errors were encountered: