Skip to content

Commit

Permalink
Merge pull request #9 from ConnorStoneAstro/control
Browse files Browse the repository at this point in the history
allow delattr of module params
  • Loading branch information
ConnorStoneAstro authored Dec 2, 2024
2 parents 136b6cb + 3ad3e33 commit bdff502
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/caskade/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,3 +313,9 @@ def __setattr__(self, key: str, value: Any):
except AttributeError:
pass
super().__setattr__(key, value)

def __delattr__(self, key: str):
"""Intercept attribute deletion to remove links."""
if key in self.children:
self.unlink(key)
super().__delattr__(key)
2 changes: 1 addition & 1 deletion src/caskade/warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class InvalidValueWarning(CaskadeWarning):

def __init__(self, name, value, valid):
message = dedent(
f"""\
f"""
Value {value.detach().cpu().tolist()} for parameter "{name}" is outside the valid range ({valid[0].detach().cpu().tolist() if valid[0] is not None else "-inf"}, {valid[1].detach().cpu().tolist() if valid[1] is not None else "inf"}).
Likely to cause errors or unexpected behavior!"""
)
Expand Down
17 changes: 17 additions & 0 deletions tests/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,20 @@ def g():
assert "deltest2" not in Module._module_names
assert "deltest1" in Module._module_names
assert m1.name == "deltest1"


def test_module_delattr():
class TestModule(Module):
def __init__(self, name, init_param):
super().__init__(name)
self.p = init_param

initparam = Param("p")
m = TestModule("test", initparam)
newparam = Param("p")
m.p = newparam
assert m.p is initparam, "Module should not allow overwriting of parameters"
del m.p
m.p = newparam
assert m.p is not initparam, "Module should allow deletion of parameters"
assert m.p is newparam, "Module should allow setting of parameters"

0 comments on commit bdff502

Please sign in to comment.