Repository navigation
Conversation
|
Mmmh.
so they have slightly different definitions of what constitute a mapping. @sobolevn do you happen to know if there's a rationale for this? Otherwise, I'm inclined to say we maybe should support "mappings" without |
|
I'll leave it up to the msgspec maintainers to decide on how it handles mappings and key-less mappings. My goal for this PR was very narrow: The current dict conversion requires |
|
|
||
| with pytest.raises(ValidationError) as rec: | ||
| convert({"table": Table()}, Ex, strict=strict) | ||
| assert str(rec.value) == "Expected `object`, got `Table` - at `$.table`" |
There was a problem hiding this comment.
Should this be
"Expected `mapping`, got `Table`"
?
There was a problem hiding this comment.
This uses the existing ms_validation_error function which outputs object for dict fields. We can change that but it seems separate to this fix. Perhaps one for a different issue/pr?
|
@provinzkraut I don't know the exact reason, but I would say that legacy is probably a good guess :) Maybe we should switch to https://docs.python.org/3/c-api/dict.html#c.PyDict_Update instead? It does not require Checking for |
We should! Its docs say
which is how we call it, so it feels like the proper fix for this. @maldoinc would you be willing to make that change in this PR? Otherwise I suggest we close this one and implement the other fix in a new PR. |
|
Unless I'm misunderstanding the ask here is to use static PyObject *
convert_mapping_to_dict(
ConvertState *self, PyObject *obj, TypeNode *type, PathNode *path
) {
PyObject *out = NULL;
PyObject *temp = PyDict_New();
if (temp == NULL) return NULL;
- if (PyDict_Merge(temp, obj, 1) == 0) {
+ if (PyDict_Update(temp, obj) == 0) {
out = convert_dict_to_dict(self, temp, type, path);
}
Py_DECREF(temp);
return out;
}After running the test suite via |
|
Interesting. That means we require |
|
Hm, looks like |
|
Wait, I misread it :)
So, it means that according to the docs: But, it doesn't, does it? This is a bug. |
|
I created python/cpython#160669 |
|
Alright, let's leave this one open then until we can clarify upstream. |
|
Wait (sorry, I am dumb today, I've spent the whole day working on vscode themes, it drained all of me), but how is that possible to merge a mapping without keys? We don't know what to merge in this case. It is different from sequences, where indexes start from |
Well.. I guess you have a point there 😅 Right, soo.. We keep the |
|
Docs pipeline is failing with |
When converting into a
dict,convertalso accepts mapping objects providing akeys()method and__getitem__for retrieving each key's value. Those implementing only__getitem__now correctly fail validation instead of erroring on an invalid.keys()call.Fixes #1308