Skip to content

Commit

Permalink
nits
Browse files Browse the repository at this point in the history
  • Loading branch information
ASEM000 committed Mar 30, 2024
1 parent c662373 commit 0f11cb0
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions sepes/_src/tree_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,23 +206,23 @@ def __init_subclass__(klass: type[T], **k):
if "__delattr__" in vars(klass):
raise TypeError(f"Reserved method `__delattr__` defined in `{klass}`.")
super().__init_subclass__(**k)
# register the class with the proper tree backend.
# the registration envolves defining two rules: how to flatten the nested
# structure of the class and how to unflatten the flattened structure.
# The flatten rule for `TreeClass` is equivalent to vars(self). and the
# unflatten rule is equivalent to `klass(**flat_tree)`. The flatten/unflatten
# rule is exactly same as the flatten rule for normal dictionaries.
# - register the class with the proper tree backend.
# - the registration envolves defining two rules: how to flatten the nested
# structure of the class and how to unflatten the flattened structure.
# The flatten rule for `TreeClass` is equivalent to vars(self). and the
# unflatten rule is equivalent to `klass(**flat_tree)`. The flatten/unflatten
# rule is exactly same as the flatten rule for normal dictionaries.
treelib = sepes._src.backend.treelib
treelib.register_treeclass(klass)

def __setattr__(self, key: str, value: Any) -> None:
# implements the controlled mutability behavior.
# In essence, setattr is allowed to set attributes during initialization
# and during functional call using .at["method"](*, **) by marking the
# instnace as mutable. Otherwise, setattr is disallowed.
# recall that during the functional call using .at["method"](*, **)
# the tree is always copied and the copy is marked as mutable, thus
# setattr is allowed to set attributes on the copy not the original.
# - implements the controlled mutability behavior.
# - In essence, setattr is allowed to set attributes during initialization
# and during functional call using `value_and_tree(method)(*, **)` by marking the
# instnace as mutable. Otherwise, setattr is disallowed.
# - recall that during the functional call using `value_and_tree(method)(*, **)`
# the tree is always copied and the copy is marked as mutable, thus
# setattr is allowed to set attributes on the copy not the original.
if id(self) not in _mutable_instance_registry:
raise AttributeError(
f"Cannot set attribute {value=} to `{key=}` "
Expand All @@ -232,13 +232,13 @@ def __setattr__(self, key: str, value: Any) -> None:
getattr(object, "__setattr__")(self, key, value)

def __delattr__(self, key: str) -> None:
# same as __setattr__ but for delattr.
# both __setattr__ and __delattr__ are used to implement the
# controlled mutability behavior during initialization and
# during functional call using .at["method"](*, **).
# recall that during the functional call using .at["method"](*, **)
# the tree is always copied and the copy is marked as mutable, thus
# setattr is allowed to set attributes on the copy not the original.
# - same as __setattr__ but for delattr.
# - both __setattr__ and __delattr__ are used to implement the
# - controlled mutability behavior during initialization and
# during functional call using `value_and_tree(method)(*, **)`.
# - recall that during the functional call using `value_and_tree(method)(*, **)`
# the tree is always copied and the copy is marked as mutable, thus
# setattr is allowed to set attributes on the copy not the original.
if id(self) not in _mutable_instance_registry:
raise AttributeError(
f"Cannot delete attribute `{key}` "
Expand Down

0 comments on commit 0f11cb0

Please sign in to comment.