-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Make dict validation stricter #1268
Comments
Ok, then I'll split out |
More cases (continuation of previous snippet): # list
In [5]: model2 = RequestModel.validate({"value": []})
In [6]: model2
Out[6]: RequestModel(value={})
# tuple
In [7]: model3 = RequestModel.validate({"value": ()})
In [8]: model3
Out[8]: RequestModel(value={})
# set
In [10]: model4 = RequestModel.validate({"value": set()})
In [11]: model4
Out[11]: RequestModel(value={})
# empty range
In [14]: model5 = RequestModel.validate({"value": range(100, 0)})
In [15]: model5
Out[15]: RequestModel(value={}) So I guess any empty iterable can be coerced to dictionary. |
Yes, so can a list of pairs. All we really do is call |
It may be a perfect use case for |
The problem is that python can and will convert any iterable to a dictionary via As a result, any iterable that has no items will be converted to I personally would vote to change this behavior in v2 so that anything that isn't a subclass of I'd be curious if there are any common uses of this functionality that could pose a more serious obstacle to making this change. (For example, if we need pydantic to be able to parse a model to a If we are going to change this behavior in v2, I would suggest we don't add Thoughts? |
Connected issue: #558 |
Do we have any plans or estimation about when v2 will be ready? I agree with your arguments @dmontagu. Do we want to save current behaviour of |
I agree with @dmontagu. I too thought there must be a situation where lists of pairs are coerced to a dict, but then couldn't think of one. In terms of v2, I was originally hoping for the end of March, but I think that's no unrealistic. Hopefully not too long through. |
I have another case: "options": [
{
"value": "OPEN",
"label": "Open"
},
{
"value": "CLOSED",
"label": "Closed"
}
] validated with options: Dict[str, str] = {} becomes
which is quite wrong! |
Agreed, I think for simplicity we should not allow lists as inputs to dicts in V2. |
Yeah 🤔 Agreed with @dmontagu as well. ☝️ |
Here's a slightly less intrusive tweak that would fix many of these bugs while remaining backwards-compatible with the existing ability to coerce a dict from a list of tuples. Maybe something like this can be added sooner than v2? #2513 (comment) |
This is fixed on main and will be included in V2, you can try it now on the alpha release. |
Bug
Output of
python -c "import pydantic.utils; print(pydantic.utils.version_info())"
:Consider following code snippet:
What would you expect
model.value
to be equal? I would expect it to raiseValidationError
, because empty strings are not allowed, but apparently it coerces to dictionary:The text was updated successfully, but these errors were encountered: