Allow (de)serializing CalendarRange and DateRange Parameters#625
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #625 +/- ##
==========================================
+ Coverage 82.81% 82.89% +0.08%
==========================================
Files 5 5
Lines 3037 3070 +33
==========================================
+ Hits 2515 2545 +30
- Misses 522 525 +3 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Actually any container Parameter that holds a datetime object won't be able to be serialized due to the limitations of the basic Python JSON encoder. As such the changes made in this PR aren't yet enough to fix the issue I observed in Lumen, which uses the The way forward may be to subclass |
|
JSON has no datetime representation so it's actually not possible to directly decode a Python datetime value encoded to a JSON string. Seems like a limitation that should be mentioned in the documentation. The Lumen/Panel issue needs to be fixed separately. |
|
This seems sufficient for the specific bug we are fixing here. In the longer run it would be nice to work out some more general solution but I suspect we'd need composite parameter types to make that feasible. |
I got the following error while working on a Lumen app that has a DateRange filter widget synced to the URL:
We found out that the
DateRangeandCalendarRangeparameters inherit from theTupleparameter (de)serialization methods, while they should override these methods as Python'sjsonmodule can't serialize datetime objects (import datetime as dt, json; json.dumps(dt.datetime.now())raises the TypeError mentioned above).This PR adds these methods to the two parameters, and test them. I've added tests, including tests that use
np.datetime64andpd.Timestampobjects since they appeared to be supported (I learnt thatpd.Timestampobjects inherits fromdatetime.datetime).