-
Notifications
You must be signed in to change notification settings - Fork 192
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
Dependencies: remove simplejson
#5391
Conversation
Note, you can see the behaviour of simplejson at: https://github.com/simplejson/simplejson/blob/02221b19672b1b35188080435c7360cd2d6af6fb/simplejson/encoder.py#L109 I checked (in Python 3.8) that the built-in
|
@@ -9,13 +9,13 @@ | |||
########################################################################### | |||
# pylint: disable=invalid-name | |||
"""Utilities for removing legacy workflows.""" | |||
import json |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you checked the migration tests still work? (given these tests do not run in PRs any more)
Lower in this file, it passes a 'wb' handle to json.dump
, which I assume will fail?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, but I think even the tests wouldn't catch this because I think that path of the code is uncovered. Will have a look later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should catch it, I added a test specifically for it in #5330
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a test specifically for it in
Fantastic, can confirm that it failed, well spotted! I have fixed it and migration tests now run fine. Also removed some remaining uses of aiida.common.json
in the tests. Should be good to go now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cheers 👍 will have a look in a bit
Also pinging @csadorf since this would drop a dependency |
Sorry to drop in only now. Just wanted to mention that, historically, IIRC this was actually introduced not for py2-py3 but because it was faster. Note that the docs mention |
One of my colleagues ran some benchmark tests a while back to test exactly this. If anything |
I think we should test against the version that most people would install by simply installing with |
I'm pretty sure that they are installed/compiled by default if the environment has the compilers. However, this will only work for CPython of course. I think optional in this case means that |
b3d1f39
to
d6e83ed
Compare
This library was used as a drop-in replacement for the `json` module of the standard library to provide consistency of JSON (de)serializing between Python 2 and Python 3. Since we have long since dropped support for Python 2, we can now simply use the standard library. The module `aiida.common.json` provided an interface to `simplejson` and was used throughout `aiida-core` instead of `json`. This module is now deprecated and `aiida-core` just uses `json` directly. There are two significant changes that needed to be taken into account: * `simplejson` provided automatic serialization for `decimal.Decimal` but `json` does not. The support for serializing these types to the database is maintained by performing the serialization manually in the `aiida.orm.implementation.utils.clean_value` method. All instances of `decimal.Decimal` are serialized as `numbers.Real`, e.g. floats, so the behavior should remain the same. * The `aiida.common.json` wrapper functions `dump` and `load` accepted file objects in text and binary mode. However, the `json` analogues only support text files. The wrapper functions therefore had to be adapted to decode and encode, respectively, the contents of the file handle before passing it to the `json` function. * The code that calls `json.dump` passing a handle from a temporary file generated with `tempfile.NamedTemporaryFile` had to be updated to wrap the handle in `codecs.getwriter('utf-8')` since the default mode for `NamedTemporaryFile` is `w+b` and `json.dump` requires a text file handle. Finally, the `aiida.common.json` module is deprecated and will emit a deprecation warning when it is imported.
d6e83ed
to
714f624
Compare
There are wheels available (i.e. pre-compiled) for most architectures: https://pypi.org/project/simplejson/#files, this is what pip will install |
actually, there is another difference: In [1]: import collections, simplejson, json
In [2]: TupleClass = collections.namedtuple("TupleClass", ("a", "b"))
In [3]: value = TupleClass(1, 2)
In [4]: json.dumps(value)
Out[4]: '[1, 2]'
In [5]: simplejson.dumps(value)
Out[5]: '{"a": 1, "b": 2}' Don't think this would break anything we do though, and the json behaviour seems perhaps better 🤷 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The more dependencies dropped, the better 😄
Fixes #5374
This library was used as a drop-in replacement for the
json
module ofthe standard library to provide consistency of JSON (de)serializing
between Python 2 and Python 3. Since we have long since dropped support
for Python 2, we can now simply use the standard library.
The module
aiida.common.json
provided an interface tosimplejson
andwas used throughout
aiida-core
instead ofjson
. This module is nowdeprecated and
aiida-core
just usesjson
directly.There are two significant changes that needed to be taken into account:
simplejson
provided automatic serialization fordecimal.Decimal
but
json
does not. The support for serializing these types to thedatabase is maintained by performing the serialization manually in
the
aiida.orm.implementation.utils.clean_value
method. All instancesof
decimal.Decimal
are serialized asnumbers.Real
, e.g. floats,so the behavior should remain the same.
The
aiida.common.json
wrapper functionsdump
andload
acceptedfile objects in text and binary mode. However, the
json
analoguesonly support text files. The wrapper functions therefore had to be
adapted to decode and encode, respectively, the contents of the file
handle before passing it to the
json
function.The code that calls
json.dump
passing a handle from a temporary filegenerated with
tempfile.NamedTemporaryFile
had to be updated towrap the handle in
codecs.getwriter('utf-8')
since the default modefor
NamedTemporaryFile
isw+b
andjson.dump
requires a textfile handle.
Finally, the
aiida.common.json
module is deprecated and will emit adeprecation warning when it is imported.