2020from _pytask .typing import NoDefault
2121from attrs import define
2222from attrs import field
23+ from upath ._stat import UPathStatResult
2324
2425
2526if TYPE_CHECKING :
@@ -179,14 +180,7 @@ def state(self) -> str | None:
179180
180181 """
181182 if self .path .exists ():
182- stat = self .path .stat ()
183- if isinstance (stat , stat_result ):
184- modification_time = self .path .stat ().st_mtime
185- return hash_path (self .path , modification_time )
186- if isinstance (stat , dict ):
187- return stat .get ("ETag" , "0" )
188- msg = "Unknown stat object."
189- raise NotImplementedError (msg )
183+ return _get_state (self .path )
190184 return None
191185
192186 def load (self , is_product : bool = False ) -> Path : # noqa: ARG002
@@ -322,14 +316,7 @@ def from_path(cls, path: Path) -> PickleNode:
322316
323317 def state (self ) -> str | None :
324318 if self .path .exists ():
325- stat = self .path .stat ()
326- if isinstance (stat , stat_result ):
327- modification_time = self .path .stat ().st_mtime
328- return hash_path (self .path , modification_time )
329- if isinstance (stat , dict ):
330- return stat .get ("ETag" , "0" )
331- msg = "Unknown stat object."
332- raise NotImplementedError (msg )
319+ return _get_state (self .path )
333320 return None
334321
335322 def load (self , is_product : bool = False ) -> Any :
@@ -341,3 +328,19 @@ def load(self, is_product: bool = False) -> Any:
341328 def save (self , value : Any ) -> None :
342329 with self .path .open ("wb" ) as f :
343330 pickle .dump (value , f )
331+
332+
333+ def _get_state (path : Path ) -> str :
334+ """Get state of a path.
335+
336+ A simple function to handle local and remote files.
337+
338+ """
339+ stat = path .stat ()
340+ if isinstance (stat , stat_result ):
341+ modification_time = path .stat ().st_mtime
342+ return hash_path (path , modification_time )
343+ if isinstance (stat , UPathStatResult ):
344+ return stat .as_info ().get ("ETag" , "0" )
345+ msg = "Unknown stat object."
346+ raise NotImplementedError (msg )
0 commit comments