55import hashlib
66import inspect
77import pickle
8+ from contextlib import suppress
89from os import stat_result
910from pathlib import Path # noqa: TCH003
1011from typing import TYPE_CHECKING
@@ -75,12 +76,10 @@ def signature(self) -> str:
7576
7677 def state (self ) -> str | None :
7778 """Return the state of the node."""
78- try :
79+ with suppress ( OSError ) :
7980 source = inspect .getsource (self .function )
80- except OSError :
81- return None
82- else :
8381 return hashlib .sha256 (source .encode ()).hexdigest ()
82+ return None
8483
8584 def execute (self , ** kwargs : Any ) -> Any :
8685 """Execute the task."""
@@ -137,10 +136,7 @@ def signature(self) -> str:
137136
138137 def state (self ) -> str | None :
139138 """Return the state of the node."""
140- if self .path .exists ():
141- modification_time = self .path .stat ().st_mtime
142- return hash_path (self .path , modification_time )
143- return None
139+ return _get_state (self .path )
144140
145141 def execute (self , ** kwargs : Any ) -> Any :
146142 """Execute the task."""
@@ -180,9 +176,7 @@ def state(self) -> str | None:
180176 The state is given by the modification timestamp.
181177
182178 """
183- if self .path .exists ():
184- return _get_state (self .path )
185- return None
179+ return _get_state (self .path )
186180
187181 def load (self , is_product : bool = False ) -> Path : # noqa: ARG002
188182 """Load the value."""
@@ -316,9 +310,7 @@ def from_path(cls, path: Path) -> PickleNode:
316310 return cls (name = path .as_posix (), path = path )
317311
318312 def state (self ) -> str | None :
319- if self .path .exists ():
320- return _get_state (self .path )
321- return None
313+ return _get_state (self .path )
322314
323315 def load (self , is_product : bool = False ) -> Any :
324316 if is_product :
@@ -331,15 +323,19 @@ def save(self, value: Any) -> None:
331323 pickle .dump (value , f )
332324
333325
334- def _get_state (path : Path ) -> str :
326+ def _get_state (path : Path ) -> str | None :
335327 """Get state of a path.
336328
337329 A simple function to handle local and remote files.
338330
339331 """
340- stat = path .stat ()
332+ try :
333+ stat = path .stat ()
334+ except FileNotFoundError :
335+ return None
336+
341337 if isinstance (stat , stat_result ):
342- modification_time = path . stat () .st_mtime
338+ modification_time = stat .st_mtime
343339 return hash_path (path , modification_time )
344340 if isinstance (stat , UPathStatResult ):
345341 return stat .as_info ().get ("ETag" , "0" )
0 commit comments