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

Implements prototype of hydra_zen.like #219

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/hydra_zen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
to_yaml,
)
from ._launch import launch
from ._like import like
from ._version import get_versions
from .structured_configs import (
ZenField,
Expand Down Expand Up @@ -36,6 +37,7 @@
"ZenField",
"make_custom_builds_fn",
"launch",
"like",
]

__version__ = get_versions()["version"]
Expand Down
41 changes: 41 additions & 0 deletions src/hydra_zen/_like.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from typing import TypeVar, cast

T = TypeVar("T")


class _Tracker:
def __init__(self, origin, tracks=None):
self.origin = origin

self.tracker = [] if tracks is None else tracks.copy()

def __repr__(self) -> str:
base = f"Like({repr(self.origin)})"
for item in self.tracker:
if isinstance(item, tuple):
_, args, kwargs = item
contents = ""
if args:
contents += ", ".join(repr(x) for x in args)
if kwargs:
if args:
contents += ", "
contents += ", ".join((f"{k}={v}" for k, v in kwargs.items()))

base += f"({contents})"
else:
base += f".{item}"
return base

def __call__(self, *args, **kwargs):
return _Tracker(self.origin, self.tracker + [("__call__", args, kwargs)])

def __getattr__(self, name):
# IPython will make attribute calls on objects; we don't want to track these.
if name != "_ipython_canary_method_should_not_exist_" and name != "__wrapped__":
return _Tracker(self.origin, self.tracker + [name])
return self


def like(obj: T) -> T:
return cast(T, _Tracker(obj))
18 changes: 18 additions & 0 deletions src/hydra_zen/structured_configs/_implementations.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
)
from hydra_zen.typing._implementations import DataClass, HasTarget, _DataClass

from .._like import _Tracker
from ._value_conversion import ZEN_VALUE_CONVERSION

_T = TypeVar("_T")
Expand Down Expand Up @@ -396,6 +397,20 @@ def wrapper(decorated_obj: Any) -> Any:
return wrapper


def make_like(target, tracks):
from hydra._internal import utils as _hydra_internal_utils

obj = _hydra_internal_utils._locate(target)

for item in tracks:
if not isinstance(item, str):
_, args, kwargs = item
obj = obj(*args, **kwargs)
else:
obj = getattr(obj, item)
return obj


def just(obj: Importable) -> Type[Just[Importable]]:
"""Returns a config that, when instantiated by Hydra, "just" returns the un-instantiated target-object.

Expand Down Expand Up @@ -457,6 +472,9 @@ def just(obj: Importable) -> Type[Just[Importable]]:
>>> conf.reduction_fn(conf.data)
6
"""
if isinstance(obj, _Tracker):
return builds(make_like, _utils.get_obj_path(obj.origin), obj.tracker)

try:
obj_path = _utils.get_obj_path(obj)
except AttributeError:
Expand Down