Skip to content

Commit

Permalink
Merge pull request #176 from pyiron/copy_and_update
Browse files Browse the repository at this point in the history
Copy and update __dict__
  • Loading branch information
liamhuber authored Jan 30, 2024
2 parents 6685d7a + a3f4a75 commit e4b1595
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 7 deletions.
4 changes: 1 addition & 3 deletions pyiron_workflow/channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -729,11 +729,9 @@ def __round__(self):

# Because we override __getattr__ we need to get and set state for serialization
def __getstate__(self):
return self.__dict__
return dict(self.__dict__)

def __setstate__(self, state):
# Update instead of overriding in case some other attributes were added on the
# main process while a remote process was working away
self.__dict__.update(**state)


Expand Down
4 changes: 4 additions & 0 deletions pyiron_workflow/composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ def process_run_result(self, run_output):
return DotDict(self.outputs.to_value_dict())

def _parse_remotely_executed_self(self, other_self):
# Un-parent existing nodes before ditching them
for node in self:
node._parent = None
other_self.running = False # It's done now
self.__setstate__(other_self.__getstate__())

def disconnect_run(self) -> list[tuple[Channel, Channel]]:
Expand Down
4 changes: 2 additions & 2 deletions pyiron_workflow/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ def __getitem__(self, item):
) from e

def __getstate__(self):
return self.__dict__
return dict(self.__dict__)

def __setstate__(self, state):
self.__dict__ = state
self.__dict__.update(**state)

def register(self, package_identifier: str, domain: Optional[str] = None) -> None:
"""
Expand Down
2 changes: 1 addition & 1 deletion pyiron_workflow/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def to_dict(self):

def __getstate__(self):
# Compatibility with python <3.11
return self.__dict__
return dict(self.__dict__)

def __setstate__(self, state):
# Because we override getattr, we need to use __dict__ assignment directly in
Expand Down
2 changes: 1 addition & 1 deletion pyiron_workflow/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,7 @@ def replace_with(self, other: Node | type[Node]):
warnings.warn(f"Could not replace_node {self.label}, as it has no parent.")

def __getstate__(self):
state = self.__dict__
state = dict(self.__dict__)
state["_parent"] = None
# I am not at all confident that removing the parent here is the _right_
# solution.
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/test_macro.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ def test_with_executor(self):

returned_nodes = result.result(timeout=120) # Wait for the process to finish
sleep(1)
self.assertFalse(
macro.running,
msg="Macro should be done running"
)
self.assertIsNot(
original_one,
returned_nodes.one,
Expand Down

0 comments on commit e4b1595

Please sign in to comment.