Skip to content

Commit

Permalink
Merge pull request #607 from mit-ll-responsible-ai/native-timedelta-s…
Browse files Browse the repository at this point in the history
…upport

auto support for timedelta
  • Loading branch information
rsokl authored Dec 2, 2023
2 parents b441313 + 5f2ed18 commit 9bc8b22
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/source/api_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ hydra-zen provides specialized auto-config support for values of the following t
- :py:class:`complex`
- :py:class:`collections.Counter`
- :py:class:`collections.deque`
- :py:class:`datetime.timedelta`
- :py:func:`functools.partial` (note: not compatible with pickling)
- :py:class:`pathlib.Path` (*support provided for OmegaConf < 2.2.1*)
- :py:class:`pathlib.PosixPath`
Expand Down
3 changes: 2 additions & 1 deletion docs/source/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Here is a stripped-down example.
For more details and examples, see :pull:`553`.
Improvements
New Features
------------
- :class:`~hydra_zen.BuildsFn` was introduced to permit customizable auto-config and type-refinement support in config-creation functions. See :pull:`553`.
- :func:`~hydra_zen.builds` and :func:`~hydra_zen.make_custom_builds_fn` now accept a `zen_exclude` field for excluding parameters from auto-population, either by name, position-index, or by pattern. See :pull:`558`.
Expand All @@ -72,6 +72,7 @@ Improvements
- :func:`hydra_zen.zen` now has first class support for running code in an isolated :py:class:`contextvars.Context`. This enables users to safely leverage state via :py:class:`contextvars.ContextVar` in their task functions. See :pull:`583`.
- Adds formal support for Python 3.12. See :pull:`555`
- Several new methods were added to :class:`~hydra_zen.ZenStore`, including the abilities to copy, update, and merge stores. As well as remap the groups of a store's entries and delete individual entries. See :pull:`569`
- Auto-config support added for :py:class:`datetime.timedelta`.
Documentation
-------------
Expand Down
2 changes: 2 additions & 0 deletions src/hydra_zen/_compatibility.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright (c) 2023 Massachusetts Institute of Technology
# SPDX-License-Identifier: MIT
from collections import Counter, deque
from datetime import timedelta
from enum import Enum
from functools import partial
from pathlib import Path, PosixPath, WindowsPath
Expand Down Expand Up @@ -68,6 +69,7 @@ def _get_version(ver_str: str) -> Version:
deque,
Counter,
range,
timedelta,
}
)

Expand Down
23 changes: 23 additions & 0 deletions src/hydra_zen/structured_configs/_implementations.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
is_dataclass,
make_dataclass,
)
from datetime import timedelta
from enum import Enum
from functools import partial
from itertools import chain
Expand Down Expand Up @@ -3705,6 +3706,22 @@ def __post_init__(
self._args_ = (start, stop, step)


@dataclass(unsafe_hash=True)
class ConfigTimeDelta:
CBuildsFn: InitVar[Type[BuildsFn[Any]]]
days: float = 0.0
seconds: float = 0.0
microseconds: float = 0.0
milliseconds: float = 0.0
minutes: float = 0.0
hours: float = 0.0
weeks: float = 0.0
_target_: str = field(default=BuildsFn._get_obj_path(timedelta), init=False)

def __post_init__(self, CBuildsFn: Type[BuildsFn[Any]]) -> None:
del CBuildsFn


ZEN_VALUE_CONVERSION[set] = partial(
ConfigFromTuple, _target_=BuildsFn._get_obj_path(set)
)
Expand All @@ -3729,6 +3746,12 @@ def __post_init__(
value.step,
CBuildsFn=CBuildsFn,
)
ZEN_VALUE_CONVERSION[timedelta] = lambda value, CBuildsFn: ConfigTimeDelta(
days=value.days,
seconds=value.seconds,
microseconds=value.microseconds,
CBuildsFn=CBuildsFn,
)
ZEN_VALUE_CONVERSION[Counter] = partial(
ConfigFromDict, _target_=BuildsFn._get_obj_path(Counter)
)
Expand Down
3 changes: 3 additions & 0 deletions src/hydra_zen/typing/_implementations.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import sys
import types
from dataclasses import MISSING
from datetime import timedelta
from enum import Enum
from pathlib import Path, PosixPath, WindowsPath
from typing import (
Expand Down Expand Up @@ -211,6 +212,8 @@ class HydraPartialBuilds(Builds[T], HydraPartialMixin[T], Protocol[T]):
Partial[Any],
range,
Set[Any],
timedelta,
types.SimpleNamespace,
]

_SupportedPrimitive: TypeAlias = Union[
Expand Down
2 changes: 2 additions & 0 deletions tests/annotations/declarations.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from collections import Counter, deque
from dataclasses import dataclass
from datetime import timedelta
from enum import Enum
from functools import partial
from pathlib import Path
Expand Down Expand Up @@ -170,6 +171,7 @@ class A:
reveal_type(just(partial(f, 1)), expected_text="Just[partial[int]]")
reveal_type(just(set([1, 2, 3])), expected_text="Builds[type[set[int]]]")
reveal_type(just(range(10)), expected_text="Builds[type[range]]")
reveal_type(just(timedelta(10)), expected_text="Builds[type[timedelta]]")

partiald_f = instantiate(just(partial(f, 1)))
reveal_type(partiald_f, expected_text="partial[int]")
Expand Down
2 changes: 2 additions & 0 deletions tests/test_value_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import string
from collections import Counter, deque
from dataclasses import dataclass, field
from datetime import timedelta
from enum import Enum
from functools import partial
from pathlib import Path
Expand Down Expand Up @@ -68,6 +69,7 @@ class Shake(Enum):
deque,
range,
Counter,
timedelta,
),
)
@settings(
Expand Down

0 comments on commit 9bc8b22

Please sign in to comment.