Python `datetime` and `dataclasses`
jgbos opened this issue · 3 comments
I have this example:
import hydra_zen as hz
from dataclasses import dataclass
from datetime import datetime
@dataclass
class Config:
time: datetime
cfg = Config(time=datetime(2019,8,14))
Standard configuration and instantiate work as expected
hz.instantiate(hz.make_config(cfg=cfg))
# as expected: {'cfg': {'time': datetime.datetime(2019, 8, 14, 0, 0)}}
type(hz.instantiate(hz.make_config(cfg=cfg)))
# as expected: omegaconf.dictconfig.DictConfig
But trying to convert to a dataclass using zen_convert
:
hz.instantiate(hz.make_config(cfg=cfg, zen_convert={"dataclass": True}))
# error!
gives this error:
HydraZenUnsupportedPrimitiveError: The configured value 2019-08-14 00:00:00, for field `time`, is not supported by Hydra -- serializing or instantiating this config would ultimately result in an error.
Consider using `hydra_zen.builds(<class 'datetime.datetime'>, ...)` create a config for this particular value.
I haven't had a chance to dive into the exact cause and if it's fixable.
Not a huge deal as I can do the following to get the dataclass back:
Config(**hz.instantiate(hz.make_config(cfg=cfg)).cfg)
hydra-zen doesn't have specialized support for datetime.datetime
. Your config - even the ones that instantiate - won't be yaml-serializable by Hydra.
We can add custom support by having hydra-zen do something like builds(datetime.fromisoformat, str(datetime(2019, 8, 14))
, but there will be stuff we'd need to look into with detecting incompatible timezones I think.
Yeah, I was meaning to look into your new config system and play with this but moved on to other things.