Skip to content

Serializing Date parameters does not support allow_None #578

@MarcSkovMadsen

Description

@MarcSkovMadsen

Param: 1.12.0

I'm trying to help a user that wants to implement the Pydantic getting started example using Param. https://discourse.holoviz.org/t/creating-an-optional-datetime-parameter-using-param/3147.

He asks how to implement signup_ts: Optional[datetime] = None. I think its not possible with the existing parameters as I get the below error.

$ python 'script.py'
Traceback (most recent call last):
  File "script.py", line 12, in <module>
    deserialized_data = User.param.deserialize_parameters(json_data)
  File "C:\repos\private\panel-ai\.venv\lib\site-packages\param\parameterized.py", line 2100, in deserialize_parameters
    return serializer.deserialize_parameters(self_or_cls, serialization, subset=subset)
  File "C:\repos\private\panel-ai\.venv\lib\site-packages\param\serializer.py", line 113, in deserialize_parameters
    deserialized = pobj.param[name].deserialize(value)
  File "C:\repos\private\panel-ai\.venv\lib\site-packages\param\__init__.py", line 1928, in deserialize
    return dt.datetime.strptime(value, "%Y-%m-%dT%H:%M:%S.%f")
TypeError: strptime() argument 1 must be str, not None
import param
import json

class User(param.Parameterized):
    signup_ts = param.Date(allow_None=True, doc="""The sign up time of the user""")


external_data = {
    'signup_ts': None,
}
json_data = json.dumps(external_data)
deserialized_data = User.param.deserialize_parameters(json_data)
User(**deserialized_data)

Additional Context

If you look at the Pydantic getting started example you will realize that its deserializing is much more flexible/ forgiving as it will be able to converts string to ints for integer parameters, not care so much about the date time format for datetime parameters etc.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions