Skip to content

Commit 7773362

Browse files
authored
Pathlib backport signatures and adjust stat_result return type (#476)
* Revert "upath.implementatios.local: fix type error on <3.12" This reverts commit d68e7ac. * upath: stat() follow_symlinks and fix return type * tests: add backport tests * tests: adjust assumptions * tests: correct backport tests * upath: implement backport changes * upath: switch to StatResultType as return type * upath: more typing and signature fixes * fix lint * tests: fix backport tests for windows * upath.implementations.local: fix that st_birthtime depends on os an may be missing
1 parent 94fba8f commit 7773362

File tree

11 files changed

+412
-22
lines changed

11 files changed

+412
-22
lines changed

typesafety/test_upath_interface.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@
273273
main: |
274274
from upath import UPath
275275
276-
reveal_type(UPath("abc").stat()) # N: Revealed type is "upath._stat.UPathStatResult"
276+
reveal_type(UPath("abc").stat()) # N: Revealed type is "upath.types.StatResultType"
277277
278278
- case: upath_chmod
279279
disable_cache: false
@@ -378,7 +378,7 @@
378378
main: |
379379
from upath import UPath
380380
381-
reveal_type(UPath("abc").lstat()) # N: Revealed type is "upath._stat.UPathStatResult"
381+
reveal_type(UPath("abc").lstat()) # N: Revealed type is "upath.types.StatResultType"
382382
383383
- case: upath_mkdir
384384
disable_cache: false

typesafety/test_upath_signatures.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@
615615
import os
616616
617617
p = UPath("")
618-
reveal_type(p.stat()) # N: Revealed type is "upath._stat.UPathStatResult"
618+
reveal_type(p.stat()) # N: Revealed type is "upath.types.StatResultType"
619619
620620
- case: upath_method_lstat
621621
disable_cache: false
@@ -624,7 +624,7 @@
624624
import os
625625
626626
p = UPath("")
627-
reveal_type(p.lstat()) # N: Revealed type is "upath._stat.UPathStatResult"
627+
reveal_type(p.lstat()) # N: Revealed type is "upath.types.StatResultType"
628628
629629
- case: upath_method_chmod
630630
disable_cache: false
@@ -1086,8 +1086,8 @@
10861086
reveal_type(p.group()) # N: Revealed type is "builtins.str"
10871087
10881088
# Stat methods
1089-
reveal_type(p.stat()) # NR: Revealed type is ".*(UPathStatResult|stat_result).*"
1090-
reveal_type(p.lstat()) # NR: Revealed type is ".*(UPathStatResult|stat_result).*"
1089+
reveal_type(p.stat()) # NR: Revealed type is ".*(StatResultType|stat_result).*"
1090+
reveal_type(p.lstat()) # NR: Revealed type is ".*(StatResultType|stat_result).*"
10911091
10921092
# None methods
10931093
reveal_type(p.symlink_to("target")) # N: Revealed type is "None"

upath/core.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
from upath.types import PathInfo
4545
from upath.types import ReadablePath
4646
from upath.types import ReadablePathLike
47+
from upath.types import StatResultType
4748
from upath.types import SupportsPathLike
4849
from upath.types import UPathParser
4950
from upath.types import WritablePath
@@ -1375,7 +1376,7 @@ def stat(
13751376
self,
13761377
*,
13771378
follow_symlinks: bool = True,
1378-
) -> UPathStatResult:
1379+
) -> StatResultType:
13791380
"""
13801381
Return the result of the stat() system call on this path, like
13811382
os.stat() does.
@@ -1399,7 +1400,7 @@ def stat(
13991400
)
14001401
return UPathStatResult.from_info(self.fs.info(self.path))
14011402

1402-
def lstat(self) -> UPathStatResult:
1403+
def lstat(self) -> StatResultType:
14031404
return self.stat(follow_symlinks=False)
14041405

14051406
def chmod(self, mode: int, *, follow_symlinks: bool = True) -> None:
@@ -1413,18 +1414,39 @@ def exists(self, *, follow_symlinks: bool = True) -> bool:
14131414
----
14141415
For fsspec filesystems follow_symlinks is currently ignored.
14151416
"""
1417+
if not follow_symlinks:
1418+
warnings.warn(
1419+
f"{type(self).__name__}.exists() follow_symlinks=False"
1420+
" is currently ignored.",
1421+
UserWarning,
1422+
stacklevel=2,
1423+
)
14161424
return self.fs.exists(self.path)
14171425

1418-
def is_dir(self) -> bool:
1426+
def is_dir(self, *, follow_symlinks: bool = True) -> bool:
14191427
"""
14201428
Whether this path is a directory.
14211429
"""
1430+
if not follow_symlinks:
1431+
warnings.warn(
1432+
f"{type(self).__name__}.is_dir() follow_symlinks=False"
1433+
" is currently ignored.",
1434+
UserWarning,
1435+
stacklevel=2,
1436+
)
14221437
return self.fs.isdir(self.path)
14231438

1424-
def is_file(self) -> bool:
1439+
def is_file(self, *, follow_symlinks: bool = True) -> bool:
14251440
"""
14261441
Whether this path is a regular file.
14271442
"""
1443+
if not follow_symlinks:
1444+
warnings.warn(
1445+
f"{type(self).__name__}.is_file() follow_symlinks=False"
1446+
" is currently ignored.",
1447+
UserWarning,
1448+
stacklevel=2,
1449+
)
14281450
return self.fs.isfile(self.path)
14291451

14301452
def is_mount(self) -> bool:
@@ -1597,10 +1619,10 @@ def rglob(
15971619
seen.add(name)
15981620
yield self.joinpath(name)
15991621

1600-
def owner(self) -> str:
1622+
def owner(self, *, follow_symlinks: bool = True) -> str:
16011623
_raise_unsupported(type(self).__name__, "owner")
16021624

1603-
def group(self) -> str:
1625+
def group(self, *, follow_symlinks: bool = True) -> str:
16041626
_raise_unsupported(type(self).__name__, "group")
16051627

16061628
def absolute(self) -> Self:
@@ -2012,6 +2034,12 @@ def match(
20122034
path_pattern = str(path_pattern)
20132035
if not path_pattern:
20142036
raise ValueError("pattern cannot be empty")
2037+
if case_sensitive is not None:
2038+
warnings.warn(
2039+
f"{type(self).__name__}.match(): case_sensitive is currently ignored.",
2040+
UserWarning,
2041+
stacklevel=2,
2042+
)
20152043
return self.full_match(path_pattern.replace("**", "*"))
20162044

20172045
@classmethod

upath/extensions.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818

1919
from upath._chain import Chain
2020
from upath._chain import ChainSegment
21-
from upath._stat import UPathStatResult
2221
from upath.core import UnsupportedOperation
2322
from upath.core import UPath
2423
from upath.types import UNSET_DEFAULT
2524
from upath.types import JoinablePathLike
2625
from upath.types import PathInfo
2726
from upath.types import ReadablePath
2827
from upath.types import ReadablePathLike
28+
from upath.types import StatResultType
2929
from upath.types import SupportsPathLike
3030
from upath.types import UPathParser
3131
from upath.types import WritablePathLike
@@ -202,10 +202,10 @@ def stat(
202202
self,
203203
*,
204204
follow_symlinks=True,
205-
) -> UPathStatResult:
205+
) -> StatResultType:
206206
return self.__wrapped__.stat(follow_symlinks=follow_symlinks)
207207

208-
def lstat(self) -> UPathStatResult:
208+
def lstat(self) -> StatResultType:
209209
return self.__wrapped__.stat(follow_symlinks=False)
210210

211211
def chmod(self, mode: int, *, follow_symlinks: bool = True) -> None:

upath/implementations/http.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from upath._stat import UPathStatResult
1414
from upath.core import UPath
1515
from upath.types import JoinablePathLike
16+
from upath.types import StatResultType
1617

1718
if TYPE_CHECKING:
1819
from typing import Literal
@@ -98,7 +99,7 @@ def is_dir(self, *, follow_symlinks: bool = True) -> bool:
9899
else:
99100
return True
100101

101-
def stat(self, follow_symlinks: bool = True) -> UPathStatResult:
102+
def stat(self, follow_symlinks: bool = True) -> StatResultType:
102103
if not follow_symlinks:
103104
warnings.warn(
104105
f"{type(self).__name__}.stat(follow_symlinks=False):"

0 commit comments

Comments
 (0)