Replaced simplejson to json in _json.py and dbstore.py#168
Conversation
dhruvmanila
left a comment
There was a problem hiding this comment.
The problem stated in the mentioned issue was that this file is redundant as we have the builtin JSON module which contains the same functions.
What this PR should do is to delete the file (_json.py) and change its references to directly use the builtin JSON module. In this case, the only reference of this file is in infogami/infobase/dbstore.py
Please make the necessary changes.
|
@cclauss Can you close and reopen this PR to trigger the CI once again? Thanks. |
|
It seems that the problem is related to the changes made in this PR. The CI is getting stuck with these failures (https://github.com/internetarchive/infogami/pull/168/checks?check_run_id=2313629409#step:8:445) which are all passing on master: Let me take a look at what is happening. |
|
@Enigma04 Hi, can you please remove the changes done in |
|
@dhruvmanila will make the changes |
|
Alright, so the problem seems to be in the removal of |
|
@Enigma04 In the meantime, can you please move the |
|
@cclauss @dhruvmanila can I close this PR and make a new one? |
|
We should keep this pull request open while creating a second separate PR. Do you make these pull requests with If you do them with the GitHub web UI then just go to https://github.com/internetarchive/infogami/edit/master/infogami/infobase/common.py and then make the appropriate changes and click |
|
@cclauss So for the common.py, should I replace simplejson with JSON and then make a PR or should I keep it as it is? |
|
Yes. The goal of #133 is to Remove simplejson dependency so replace |
|
Perhaps cherrypick a few changes from #172 ? |
|
Hi, @Enigma04 Can you please rebase on the latest master? Thanks |
|
Closed and reopened to rebase. |
|
Ok, so I found where the problem was. We were getting a infogami/infogami/infobase/dbstore.py Lines 634 to 636 in b6c8555 The object ( infogami/infogami/infobase/dbstore.py Lines 620 to 630 in b6c8555 This was handled by the @cclauss I fixed it locally and it works. I think this PR can be closed as I will open another one to solve it. |
|
My strong preference (if it is not too complicated) would be that you help @Enigma04 to understand how to fix this PR so it passes the tests and can be approved and merged. Rohit has worked hard on this PR and it would be great to support him in taking it over the goal line. We bumped into the datetime in json issue once before so please keep a look out for it in future simplejson --> json efforts. |
|
Yeah, no problem. It's a simple fix. These Python objects can be serialized by the JSON module:
So, a simple fix would be to make a function which takes an arbitrary object, say infogami/infogami/infobase/_json.py Lines 28 to 29 in b6c8555 Else, we will raise a |
|
Hey @dhruvmanila @cclauss sorry I had exams so I couldn't get work done. This issue hasn't been fixed yet right? |
|
Hey @Enigma04 it's all good. Hope your exams went well! No, this issue hasn't been fixed. You can make the necessary changes as mentioned in my previous comments and it will be good to go :) |
|
Hey @dhruvmanila so do I have to wrap this line in a function that will take an |
|
No, a separate function which will take in some object and check if it is Can you please read the documentation as mentioned in my previous link? That will clear it up. |
|
@dhruvmanila can you tell me if what I've written is correct? Feel free to tell me any changes that should be made :). Once again I apologize for the delay, I am currently having my theory exams. |
There was a problem hiding this comment.
| return TypeError | |
| raise TypeError() |
| thing_id = d and d[0].thing_id or None | ||
| return thing_id and self.get_metadata_from_id(thing_id).key | ||
|
|
||
| def check_datetime(self, obj): |
There was a problem hiding this comment.
I don't think this should be a method inside the object. It's just a helper function not specific to this object. So, please make this function global.
A bit of documentation could go a long way :)
"""Helper function to serialize ``datetime.datetime`` object to JSON compatible.
It can be extended to serialize other types of objects as per the requirement."""
We're helping the module in encoding a Python object to a JSON object.
| def check_datetime(self, obj): | |
| def encode_datetime(self, obj): |
| else: | ||
| raise TypeError() | ||
|
|
There was a problem hiding this comment.
An appropriate error message will help us in the future :)
| else: | |
| raise TypeError() | |
| raise TypeError( | |
| f'Object of type "{obj.__class__.__name__}" is not JSON serializable' | |
| ) |
| self.db.insert( | ||
| 'data', False, thing_id=id, revision=1, data=simplejson.dumps(data) | ||
| ) | ||
| self.db.insert('data', False, thing_id=id, revision=1, data= json.dumps(data, default=self.check_datetime(t))) |
There was a problem hiding this comment.
You only have to pass the reference to the function, so we do not need to call it. The module will call it only if some object is not serializable. Our function is a fallback for the JSON module.
| self.db.insert('data', False, thing_id=id, revision=1, data= json.dumps(data, default=self.check_datetime(t))) | |
| self.db.insert('data', False, thing_id=id, revision=1, data= json.dumps(data, default=self.encode_datetime)) |
|
The failing test says that black |
Fixes #80
Subtask of #133
@cclauss I have replaced two more files for you to merge.
Also side note: The infogami/infobase/client.py already has the JSON package imported and replaced so you can check that file off the list.