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

Better support for Handler level objects #1178

Merged
merged 4 commits into from
Jan 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/neptune/new/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
Iterable,
Iterator,
List,
NewType,
Optional,
Union,
)
Expand Down Expand Up @@ -69,6 +70,8 @@
if TYPE_CHECKING:
from neptune.new.metadata_containers import MetadataContainer

NeptuneObject = NewType("NeptuneObject", MetadataContainer)


def validate_path_not_protected(target_path: str, handler: "Handler"):
path_protection_exception = handler._PROTECTED_PATHS.get(target_path)
Expand All @@ -94,7 +97,7 @@ class Handler:
SYSTEM_STAGE_ATTRIBUTE_PATH: NeptuneCannotChangeStageManually,
}

def __init__(self, container: "MetadataContainer", path: str):
def __init__(self, container: "NeptuneObject", path: str):
super().__init__()
self._container = container
self._path = path
Expand All @@ -113,6 +116,24 @@ def __getitem__(self, path: str) -> "Handler":
def __setitem__(self, key: str, value) -> None:
self[key].assign(value)

def __getattr__(self, item: str):
run_level_methods = {"exists", "get_structure", "get_run_url", "print_structure", "stop", "sync", "wait"}

if item in run_level_methods:
raise AttributeError(
"You're invoking an object-level method on a handler for a namespace" "inside the object.",
f"""
For example: You're trying run[{self._path}].{item}()
but you probably want run.{item}().

To obtain the root object of the namespace handler, you can do:
root_run = run[{self._path}].get_root_object()
root_run.{item}()
""",
)

return object.__getattribute__(self, item)

def _get_attribute(self):
"""Returns Attribute defined in `self._path` or throws MissingFieldException"""
attr = self._container.get_attribute(self._path)
Expand All @@ -125,6 +146,9 @@ def container(self) -> "MetadataContainer":
"""Returns the container that the attribute is attached to"""
return self._container

def get_root_object(self) -> "NeptuneObject":
return self._container

@check_protected_paths
def assign(self, value, wait: bool = False) -> None:
"""Assigns the provided value to the field.
Expand Down Expand Up @@ -300,6 +324,7 @@ def log(
self._container.set_attribute(self._path, attr)
attr.log(value, step=step, timestamp=timestamp, wait=wait, **kwargs)

@check_protected_paths
def append(
self,
value: Union[dict, Any],
Expand Down Expand Up @@ -347,6 +372,7 @@ def append(
value = ExtendUtils.validate_and_transform_to_extend_format(value)
self.extend(value, step, timestamp, wait, **kwargs)

@check_protected_paths
def extend(
self,
values: ExtendDictT,
Expand Down