Skip to content

Commit e70c0d1

Browse files
committed
Simpify code and fix test.
1 parent 472018f commit e70c0d1

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

src/_pytask/nodes.py

+19-16
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from _pytask.typing import NoDefault
2121
from attrs import define
2222
from attrs import field
23+
from upath import UPathStatResult
2324

2425

2526
if 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)

tests/test_execute.py

-1
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,6 @@ def func(path): path.touch()
918918
assert tmp_path.joinpath("out.txt").exists()
919919

920920

921-
@pytest.mark.skipif(sys.version_info >= (3, 12), reason="Not supported in Python 3.12.")
922921
def test_with_http_path(runner, tmp_path):
923922
source = """
924923
from upath import UPath

0 commit comments

Comments
 (0)