-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Error caching datetime - Object is not JSON serializable #3231
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
Comments
I worked around the issue by adding jsonpickle as a dependency and adding this fixture to the test suite:
This approach works, and I now the cache can accept any jsonpickle-serializable object, including datetime:
Obviously, this approach is brittle, as it relies on implementation details of the cache module. I imagine pytest could adopt this technique natively, though that might be undesirable, as it adds a dependency on jsonpickle and also exposes the cache to possible security vulnerabilities (if untrusted content can reach the cache) plus I can see that the project previously explicitly set out to avoid broader encoding, though arguably jsonpickle is less invasive than execnet. What I would recommend instead is the
I'm not familiar with the order of operations, so not sure if this hook point is the right one. It seems desirable that the hook should happen as early as possible and not earlier, such that for the configured project, the hook is activated before values are loaded or stored. |
for sanity, security, and controll of those affected, i'd like to leave the structuring/restructuring of the processing data vs the serialized format up to the users of the cache, its simply too easy to slip in a security mistake or compatibility break on the framework level thats bad for everyone, for your example i would propose to store the data as a timestamp additional i believe it may be sensible to allow users to pass in own serializers in some way it would be interesting/beneficial to talk about the layering of such an abstraction (the current approach has various shortcomings that might be nicely addressable) |
Duplicate of the discussion in #2899 |
Agreed it's a dupe. |
Today, I wished to cache a datetime (the last time I queried http://ifconfig.co because they throttle to .016Hz). When I tried to store the datetime in the cache, I ran into this error:
Obviously, the current behavior expects that only JSON-serializable types are allowed in the cache, which is rather limiting. It would be nice if the cache module allowed for hooks to encode/decode values to allowable JSON.
The text was updated successfully, but these errors were encountered: