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 9c1ff2b
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions tawazi/node/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,15 @@ 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
exec_nodes[id_] = self._new_lxn(id_, *args, **kwargs)
return exec_nodes[id_]._usage_exec_node # type: ignore[no-any-return,attr-defined]

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 +370,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 9c1ff2b

Please sign in to comment.