Skip to content

Allow (de)serializing CalendarRange and DateRange Parameters#625

Merged
philippjfr merged 7 commits into
masterfrom
serialize_range_date
May 17, 2022
Merged

Allow (de)serializing CalendarRange and DateRange Parameters#625
philippjfr merged 7 commits into
masterfrom
serialize_range_date

Conversation

@maximlt
Copy link
Copy Markdown
Member

@maximlt maximlt commented May 11, 2022

I got the following error while working on a Lumen app that has a DateRange filter widget synced to the URL:

  File "/Users/mliquet/miniconda3/envs/lumen-dev37/lib/python3.7/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type datetime is not JSON serializable

We found out that the DateRange and CalendarRange parameters inherit from the Tuple parameter (de)serialization methods, while they should override these methods as Python's json module 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.datetime64 and pd.Timestamp objects since they appeared to be supported (I learnt that pd.Timestamp objects inherits from datetime.datetime).

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 11, 2022

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.89%. Comparing base (b38d3b4) to head (ab01157).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Member

@jbednar jbednar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

@maximlt
Copy link
Copy Markdown
Member Author

maximlt commented May 12, 2022

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 DateRangeSlider widget offered by Panel whose value attribute isn't a DateRange parameter but a Tuple.

The way forward may be to subclass json.JSONEncoder and json.JSONDecoder to implement custom encoders/decoders that handle datetime values. There must be some existing implementations of that in the Python ecosystem.

@maximlt
Copy link
Copy Markdown
Member Author

maximlt commented May 12, 2022

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.

@jbednar jbednar added this to the v1.12.2 milestone May 16, 2022
@philippjfr
Copy link
Copy Markdown
Member

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.

@philippjfr philippjfr merged commit ee7e86a into master May 17, 2022
@philippjfr philippjfr deleted the serialize_range_date branch May 17, 2022 21:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants