Skip to content

Commit

Permalink
chg: ♻️ separate lazyExecNode creation from the __call__
Browse files Browse the repository at this point in the history
  • Loading branch information
bashirmindee committed Jan 26, 2024
1 parent 9acdbd6 commit 877c411
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions tawazi/node/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,17 @@ def __call__(self, *args: P.args, **kwargs: P.kwargs) -> RVXN:

# 1.1 if ExecNode is used multiple times, <<usage_count>> is appended to its ID
id_ = _lazy_xn_id(self.id, self.count_occurrences(exec_nodes))
# 1.1 Construct a new LazyExecNode corresponding to the current call
new_lxn = self._new_lxn(id_, *args, **kwargs)
exec_nodes[new_lxn.id] = new_lxn

return new_lxn._usage_exec_node # type: ignore[return-value]

def _new_lxn(
self, id_: Identifier, *args: P.args, **kwargs: P.kwargs
) -> "LazyExecNode[P, RVXN]":
"""Construct a new LazyExecNode corresponding to the current call."""
values = dataclasses.asdict(self)
# force deepcopying instead of the default behavior of asdict: recursively apply asdict to dataclasses!
# force deep copy instead of the default behavior of asdict: recursively apply asdict to dataclasses!
values["exec_function"] = deepcopy(self.exec_function)
values["id_"] = id_

Expand All @@ -364,13 +372,9 @@ def __call__(self, *args: P.args, **kwargs: P.kwargs) -> RVXN:
values["tag"] = kwargs.get(ARG_NAME_TAG) or self.tag
values["unpack_to"] = kwargs.get(ARG_NAME_UNPACK_TO) or self.unpack_to

new_lxn: LazyExecNode[P, RVXN] = LazyExecNode(**values)

new_lxn._validate_dependencies()

exec_nodes[new_lxn.id] = new_lxn

return new_lxn._usage_exec_node # type: ignore[return-value]
lxn: LazyExecNode[P, RVXN] = LazyExecNode(**values)
lxn._validate_dependencies()
return lxn

def _validate_dependencies(self) -> None:
for dep in self.dependencies:
Expand Down

0 comments on commit 877c411

Please sign in to comment.