Skip to content
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

Allow (de)serializing CalendarRange and DateRange Parameters #625

Merged
merged 7 commits into from
May 17, 2022

Conversation

maximlt
Copy link
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

codecov-commenter commented May 11, 2022

Codecov Report

Merging #625 (ab01157) into master (b38d3b4) will increase coverage by 0.08%.
The diff coverage is n/a.

@@            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     
Impacted Files Coverage Δ
numbergen/__init__.py 80.67% <0.00%> (+0.34%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b38d3b4...ab01157. Read the comment docs.

Copy link
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
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
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
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