1818from attrs import define
1919from pytask import PNode
2020from pytask import PPathNode
21+ from pytask import PProvisionalNode
2122from pytask import PTask
2223from pytask import PythonNode
2324from pytask import Traceback
@@ -255,7 +256,7 @@ def _handle_function_products(
255256 raise ValueError (msg )
256257
257258 def _save_and_carry_over_product (
258- path : tuple [Any , ...], node : PNode
259+ path : tuple [Any , ...], node : PNode | PProvisionalNode
259260 ) -> CarryOverPath | PythonNode | None :
260261 argument = path [0 ]
261262
@@ -275,25 +276,38 @@ def _save_and_carry_over_product(
275276 for p in path [1 :]:
276277 value = value [p ]
277278
278- # If the node is a PythonNode, we need to carry it over to the main process.
279- if isinstance (node , PythonNode ):
280- node .save (value = value )
281- return node
279+ return _save_return_product (node , value , remote = remote )
282280
283- # If the path is local and we are remote, we need to carry over the value to
284- # the main process as a PythonNode and save it later.
285- if isinstance (node , PPathNode ) and is_local_path (node .path ) and remote :
286- return PythonNode (value = value )
281+ return cast (
282+ "PyTree[CarryOverPath | PythonNode | None]" ,
283+ tree_map_with_path (
284+ _save_and_carry_over_product ,
285+ cast ("Any" , task .produces ),
286+ ),
287+ )
287288
288- # If no condition applies, we save the value and do not carry it over. Like a
289- # remote path to S3.
290- node .save (value )
289+
290+ def _save_return_product (
291+ node : PNode | PProvisionalNode ,
292+ value : Any , # noqa: ANN401
293+ * ,
294+ remote : bool ,
295+ ) -> CarryOverPath | PythonNode | None :
296+ """Save a concrete return product or defer a provisional one to collection."""
297+ if isinstance (node , PProvisionalNode ):
291298 return None
292299
293- return tree_map_with_path (
294- _save_and_carry_over_product ,
295- cast ("Any" , task .produces ),
296- )
300+ # Python nodes must be carried back to the main process.
301+ if isinstance (node , PythonNode ):
302+ node .save (value = value )
303+ return node
304+
305+ # Local paths on remote workers are saved by the main process.
306+ if isinstance (node , PPathNode ) and is_local_path (node .path ) and remote :
307+ return PythonNode (value = value )
308+
309+ node .save (value )
310+ return None
297311
298312
299313def _write_local_files_to_remote (
0 commit comments