-
-
Notifications
You must be signed in to change notification settings - Fork 94
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
fix: convert python tuples into lists #312
fix: convert python tuples into lists #312
Conversation
Codecov Report
@@ Coverage Diff @@
## master #312 +/- ##
==========================================
- Coverage 81.88% 81.58% -0.31%
==========================================
Files 57 57
Lines 5494 5513 +19
==========================================
- Hits 4499 4498 -1
- Misses 995 1015 +20
Continue to review full report at Codecov.
|
Hi @blacha Thanks for opening a PR! Though I clearly see the value of this change for your use case, I think it might be a problematic change for the library as a whole. Unfortunately, I didn't share the main design points anywhere, so I'll try to put it here (and will later put it in the docs).
So, my main intention is to keep the library conservative to its inputs by default so it is less surprising and more explicit about deviations from the JSON Schema spec. However, I think that we still can add this change to the library but put it behind a feature flag, similarly to what So, the end-user API might look like this: jsonschema_rs.validate({"items": {"type": "integer"}}, [1, "2"], option=jsonschema_rs.OPT_TUPLE_AS_LIST)
compiled = jsonschema_rs.JSONSchema({"items": {"type": "integer"}}, option=jsonschema_rs.OPT_TUPLE_AS_LIST) What do you think? |
We are coming from I do see that I am for the automatic conversion of tuples to arrays, as there is no concept of tuples in JSON. If I was to take a tuple dict and seralize it to JSON using import json
json.dumps({"foo": (1,2,3)})
# '{"foo": [1, 2, 3]}'
validate(json.loads(json.dumps({"foo": (1,2,3)})), schema) # Works It looks somewhat easy to plumb the configuration option into the serialiser via the Interested in your thoughts if its worth the effort to create a config for this |
Generally, I am somewhat hesitant to follow Python's
I think so, yes. The On the public API side, I think it could be the Indeed, it should not be a big deal to extend the current public API to accept a new argument, process it, and forward the value to Let me know if I can help with the implementation! |
I had no idea that python's
I really think at least having this the default it would save a lot of issues in the long run, maybe a way to turn it off via options? Yesterday I found this library using the
Ill have a go at making it an optional bitmask today. |
@Stranger6667 Playing with import orjson
print(orjson.dumps({"foo": (1,2,3)}))
# b'{"foo":[1,2,3]}'
print(orjson.dumps({"foo": [1,2,3]}))
# b'{"foo":[1,2,3]}' It only avoids the named tuple type from collections import namedtuple
Person = namedtuple('Person', 'first_name last_name')
person_a = Person('Joe', 'Smith')
print(orjson.dumps(person_a)) # Dies with TypeError: Type is not JSON serializable: Person Which makes me think it should just by default convert, I'll add a test for named tuples. |
Good point! I didn't know that |
Also, we might get into the bitmask options later :) |
haha, I enjoy reverse engineering old games, so I love it when I get a chance to put a bitmask somewhere :) however if we just go for the default for tuples as lists I'm not really seeing a place where we need to turn that off, so to keep this PR somewhat small maybe the options logic could be added later? |
Yep, exactly :) It would be better to implement the tuple conversion for now and then if it will be needed, we can always get back into implementing those bitmask options later :) |
Thanks! I'll make a new release soon |
We use tuples for geographic coordinates and bounding boxes which doesn't seem to have a direct mapping in JSON Schema.
This pull requests converts the tuple into a list to fix
ValueError: Unsupported type: 'tuple'
when validating a object that contains a tuple.