Can't json encode some validation errors, "unorderable types: str() < int()"
See original GitHub issueI’m using marshmallow with flask, which has a jsonify() function that sorts keys, and i’m using that to return the validation errors to the client. In some situations, the dicts with validation errors returned by marshmallow combine int and str keys, which results in a TypeError while attempting to serialize them.
Here’s a somewhat minimized test case:
import json
import marshmallow as ma
class RoleSchema(ma.Schema):
name = ma.fields.Str()
class UserSchema(ma.Schema):
roles = ma.fields.Nested(RoleSchema, many=True)
user, errors = UserSchema().load({'roles':['name']})
print(errors)
print(json.dumps(errors))
print(json.dumps(errors, sort_keys=True))
Output:
{'roles': {0: {}, '_schema': ['Invalid input type.']}}
{"roles": {"0": {}, "_schema": ["Invalid input type."]}}
Traceback (most recent call last):
File "asd.py", line 13, in <module>
print(json.dumps(errors, sort_keys=True))
File "/usr/lib64/python3.5/json/__init__.py", line 237, in dumps
**kw).encode(obj)
File "/usr/lib64/python3.5/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib64/python3.5/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
TypeError: unorderable types: str() < int()
I think the relevant function is ErrorStore.get_errors(index=0)
https://github.com/marshmallow-code/marshmallow/blob/2.9.0/marshmallow/marshalling.py#L46
Stringifying the indexes in that function fixes it for me. Not sure if this is the best way to fix it though.
--- a/marshalling.py
+++ b/marshalling.py
@@ -45,6 +45,7 @@
def get_errors(self, index=None):
if index is not None:
+ index = str(index)
errors = self.errors.get(index, {})
self.errors[index] = errors
else:
Issue Analytics
- State:
- Created 7 years ago
- Comments:14 (13 by maintainers)
Top Results From Across the Web
Why am I getting "TypeError: unorderable types: str() < int()" in ...
So the question is why is json.dump() calling sorted() . The only reason I can think of is because of sort_keys=True , and...
Read more >[Example code]-TypeError: unorderable types: int() > str()
The program will first randomly generate a number unknown to the user. The user needs to guess what that number is. (In other...
Read more >TypeError: unorderable types: str() > int() please help in ...
TypeError: unorderable types: str() > int() please help in explaining this error ... You can't compare a string with an int :).
Read more >Changelog — Python 3.5.9 documentation
bpo-8256: Fixed possible failing or crashing input() if attributes “encoding” or “errors” of sys.stdin or sys.stdout are not set or are not strings....
Read more >sqlalchemy.sql.sqltypes - Ax · Adaptive Experimentation Platform
Comparator): def _setup_getitem(self, index): raise NotImplementedError() def ... String` type will assume that input is to be passed as Python Unicode ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This issue can probably be removed from the v3 milestone.
The output of
List(Nested(Schema))
for this use case is interesting:That is probably how
Nested(many=True)
should be formatted. Roles shouldn’t have a schema error, because it is correctly supplied as a list. This isn’t the first time we have ran into inconsistencies withmany=True
whereList(Nested)
did the right thing. https://github.com/marshmallow-code/marshmallow/issues/315#issuecomment-228788849