Update.de_json(cls, json_string) alters the original dict passed as json_string
-
What version of pyTelegramBotAPI are you using? pyTelegramBotAPI v3.8.1
-
What OS are you using? macOS Big Sur 11.4
-
What version of python are you using? Python 3.8.9
If json_string passed to Update.de_json(cls, json_string) is a dictionary, then, instead of making a deep copy of this dictionary for its internal purposes, de_json alters the original dictionary.
I find this behaviour undesirable and currently forced to work it around on my side by duplicating the dictionary and throwing away the one that was used as a parameter to de_json.
I believe that this is a bug that was introduced together with this commit: https://github.com/eternnoir/pyTelegramBotAPI/commit/0370a9f277552f2416e23b9894e08f78e02ba298
P. S. From looking at the code it also seems that some other de_json functions (like ChatMemberUpdated.de_json) have this problem too (Update.de_json is not the only one that has this problem).
Yep, seems you are right. I miss that this new behaviour is not fully compliant with previous one. We need to think on it.
Could you please provide somel cases when this behaviour is undesirable? How do you reuse the dictionary that was passed to, for example, Update.de_json?
I'm using https://rasa.com which internally uses PyTelegramBotAPI to integrate with Telegram. Rasa takes a dictionary from request.json property (Sanic server) and passes it to Update.de_json.
Later, in my code, I need to use the original dictionary from Sanic's request.json property as well but it is already corrupted by telebot by that time so I am forced to do an extra call to ujson.loads(request.body) and therefore spend cpu time to parse json again.
It is not the end of the world, of course (ujson is fast and the input json is not too big), but would be nice if that could be avoided.
Got it, thank you. I'll take care of it.
Thank you!
BTW. Will something like global flag apihelper.NEED_DICT_COPY will be the solution?
It seems that your case is not very common, so it seems to be good to keep current behaviour as default one with the ability to enable copying...
I think that side effects like this is not a very good practice in general - people don't usually expect objects passed as parameters to functions to be altered by those functions, unless changing those objects is the actual purpose of the function (which is not the case with de_json).
That's reasonable...
instead of making a deep copy of this dictionary
I took a better look on the code:
and seems that there is no need in deep copy. It seems that simple copy should be enough for the purpose of preserving original dict from changes.
Every de_json updates only the first-level dictionary. If he needs to go deeper - it calls de_json for internal dict which... will make its own copy, thus, saving it from changes.
Isn't it?
instead of making a deep copy of this dictionary
I took a better look on the code:
and seems that there is no need in deep copy. It seems that simple copy should be enough for the purpose of preserving original dict from changes.
Every de_json updates only the first-level dictionary. If he needs to go deeper - it calls de_json for internal dict which... will make its own copy, thus, saving it from changes.
Isn't it?
I totally agree with you. Shallow copy is the way to go here.
https://github.com/eternnoir/pyTelegramBotAPI/pull/1222