-
Notifications
You must be signed in to change notification settings - Fork 22
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
Replace ujson with orjson #29
base: develop
Are you sure you want to change the base?
Conversation
Would you mind helping me with python-lsp/python-lsp-server#579 as well? That would be wonderful! :) |
@rumpelsepp Yep, was planning to help with that next. I want to push this one over the line first — I see tests are failing. |
The test failure seems unrelated to this change:
|
Implemented as requested in this comment: python-lsp/python-lsp-server#579 (review)
0477625
to
eec1388
Compare
@ccordoba12 Done; thanks for the fix and sorry for the delay! |
@valrus, it seems that this is ready, thanks! Now, the next step is to open a PR in the pylsp-server repo that pulls this one and checks our tests pass there with orjson. For that, you can also start with this previous PR from @rumpelsepp: python-lsp/python-lsp-server#579. |
@ccordoba12 OK, I gave it a try: python-lsp/python-lsp-server#591 |
This PR is an attempt to carry #28 to completion, making the necessary changes to ensure compatibility with
orjson
and adding tests against bothorjson
and the stdlibjson
to ensure that the writer works no matter which is imported.There were two main pieces necessary for the migration:
orjson
doesn't support thesort_keys
kwarg todumps
; instead itsdumps
takes anoption
kwarg which expects bitwise flags. This PR makes the necessary changes toself._json_dumps_args
inJsonRpcStreamWriter.__init__
depending on whetherorjson
is available. (Unnecessary but hopefully helpful: it also adds theseparators=(',', ':')
kwarg to the stdlib json case so that it matchesorjson
's behavior of omitting unnecessary spaces.)orjson.dumps
returns bytes, not a string. This PR encodes the dumped JSON if necessary (i.e. in the stdlib case) and uses bytestring % formatting to populateresponse
inJsonRpcStreamWriter.write
.It was tricky to make sure that this worked with or without
orjson
. The existing test only tests theorjson
case iforjson
is available on the system on which the tests are run, but I wanted to be sure to testJsonRpcStreamWriter
against the stdlibjson
too. As such, this PR adds atest_writer_stdlib_json
test that uses some funky patching to ensure thatJsonRpcStreamWriter.write
behaves correctly whenorjson
is unavailable. It's a bit hacky but I confirmed that it does what's intended in the debugger and it seemed better than risking a developer breaking the writer for users who can't importorjson
. (Unfortunately I don't think there's any good way to test the writer againstorjson
if it can't be imported in the dev's environment!)