question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't json encode some validation errors, "unorderable types: str() < int()"

See original GitHub issue

I’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:open
  • Created 7 years ago
  • Comments:14 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
deckar01commented, Aug 29, 2018

This issue can probably be removed from the v3 milestone.

1reaction
deckar01commented, Apr 11, 2018

The output of List(Nested(Schema)) for this use case is interesting:

{'roles': {0: {'_schema': ['Invalid input type.']}}}

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 with many=True where List(Nested) did the right thing. https://github.com/marshmallow-code/marshmallow/issues/315#issuecomment-228788849

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found