Skip to content

Commit 3009160

Browse files
committed
upath: more typing and signature fixes
1 parent 65df206 commit 3009160

File tree

5 files changed

+82
-28
lines changed

5 files changed

+82
-28
lines changed

upath/core.py

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,20 +1522,20 @@ def glob(
15221522
self,
15231523
pattern: str,
15241524
*,
1525-
case_sensitive: bool = UNSET_DEFAULT,
1526-
recurse_symlinks: bool = UNSET_DEFAULT,
1525+
case_sensitive: bool | None = None,
1526+
recurse_symlinks: bool = False,
15271527
) -> Iterator[Self]:
15281528
"""Iterate over this subtree and yield all existing files (of any
15291529
kind, including directories) matching the given relative pattern."""
1530-
if case_sensitive is not UNSET_DEFAULT:
1530+
if case_sensitive is not None:
15311531
warnings.warn(
15321532
"UPath.glob(): case_sensitive is currently ignored.",
15331533
UserWarning,
15341534
stacklevel=2,
15351535
)
1536-
if recurse_symlinks is not UNSET_DEFAULT:
1536+
if recurse_symlinks:
15371537
warnings.warn(
1538-
"UPath.glob(): recurse_symlinks is currently ignored.",
1538+
"UPath.glob(): recurse_symlinks=True is currently ignored.",
15391539
UserWarning,
15401540
stacklevel=2,
15411541
)
@@ -1552,22 +1552,22 @@ def rglob(
15521552
self,
15531553
pattern: str,
15541554
*,
1555-
case_sensitive: bool = UNSET_DEFAULT,
1556-
recurse_symlinks: bool = UNSET_DEFAULT,
1555+
case_sensitive: bool | None = None,
1556+
recurse_symlinks: bool = False,
15571557
) -> Iterator[Self]:
15581558
"""Recursively yield all existing files (of any kind, including
15591559
directories) matching the given relative pattern, anywhere in
15601560
this subtree.
15611561
"""
1562-
if case_sensitive is not UNSET_DEFAULT:
1562+
if case_sensitive is not None:
15631563
warnings.warn(
15641564
"UPath.glob(): case_sensitive is currently ignored.",
15651565
UserWarning,
15661566
stacklevel=2,
15671567
)
1568-
if recurse_symlinks is not UNSET_DEFAULT:
1568+
if recurse_symlinks:
15691569
warnings.warn(
1570-
"UPath.glob(): recurse_symlinks is currently ignored.",
1570+
"UPath.glob(): recurse_symlinks=True is currently ignored.",
15711571
UserWarning,
15721572
stacklevel=2,
15731573
)
@@ -1982,11 +1982,37 @@ def is_relative_to(
19821982
def hardlink_to(self, target: ReadablePathLike) -> None:
19831983
_raise_unsupported(type(self).__name__, "hardlink_to")
19841984

1985-
def match(self, pattern: str) -> bool:
1986-
# fixme: hacky emulation of match. needs tests...
1987-
if not pattern:
1985+
def full_match(
1986+
self,
1987+
pattern: str | SupportsPathLike,
1988+
*,
1989+
case_sensitive: bool | None = None,
1990+
) -> bool:
1991+
"""Match this path against the provided glob-style pattern.
1992+
Return True if matching is successful, False otherwise.
1993+
"""
1994+
if case_sensitive is not None:
1995+
warnings.warn(
1996+
f"{type(self).__name__}.full_match(): case_sensitive"
1997+
" is currently ignored.",
1998+
UserWarning,
1999+
stacklevel=2,
2000+
)
2001+
return super().full_match(str(pattern))
2002+
2003+
def match(
2004+
self,
2005+
path_pattern: str | SupportsPathLike,
2006+
*,
2007+
case_sensitive: bool | None = None,
2008+
) -> bool:
2009+
"""Match this path against the provided non-recursive glob-style pattern.
2010+
Return True if matching is successful, False otherwise.
2011+
"""
2012+
path_pattern = str(path_pattern)
2013+
if not path_pattern:
19882014
raise ValueError("pattern cannot be empty")
1989-
return self.full_match(pattern.replace("**", "*"))
2015+
return self.full_match(path_pattern.replace("**", "*"))
19902016

19912017
@classmethod
19922018
def __get_pydantic_core_schema__(

upath/extensions.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from upath.types import PathInfo
2727
from upath.types import ReadablePath
2828
from upath.types import ReadablePathLike
29+
from upath.types import SupportsPathLike
2930
from upath.types import UPathParser
3031
from upath.types import WritablePathLike
3132

@@ -250,8 +251,8 @@ def glob(
250251
self,
251252
pattern: str,
252253
*,
253-
case_sensitive: bool = UNSET_DEFAULT,
254-
recurse_symlinks: bool = UNSET_DEFAULT,
254+
case_sensitive: bool | None = None,
255+
recurse_symlinks: bool = False,
255256
) -> Iterator[Self]:
256257
for p in self.__wrapped__.glob(
257258
pattern, case_sensitive=case_sensitive, recurse_symlinks=recurse_symlinks
@@ -262,8 +263,8 @@ def rglob(
262263
self,
263264
pattern: str,
264265
*,
265-
case_sensitive: bool = UNSET_DEFAULT,
266-
recurse_symlinks: bool = UNSET_DEFAULT,
266+
case_sensitive: bool | None = None,
267+
recurse_symlinks: bool = False,
267268
) -> Iterator[Self]:
268269
for p in self.__wrapped__.rglob(
269270
pattern, case_sensitive=case_sensitive, recurse_symlinks=recurse_symlinks
@@ -383,7 +384,7 @@ def is_relative_to(self, other, /, *_deprecated) -> bool: # type: ignore[overri
383384
def hardlink_to(self, target: ReadablePathLike) -> None:
384385
return self.__wrapped__.hardlink_to(target)
385386

386-
def match(self, pattern: str) -> bool:
387+
def match(self, pattern: str, *, case_sensitive: bool | None = None) -> bool:
387388
return self.__wrapped__.match(pattern)
388389

389390
@property
@@ -511,8 +512,13 @@ def parent(self) -> Self:
511512
def parents(self) -> Sequence[Self]:
512513
return tuple(self._from_upath(p) for p in self.__wrapped__.parents)
513514

514-
def full_match(self, pattern: str) -> bool:
515-
return self.__wrapped__.full_match(pattern)
515+
def full_match(
516+
self,
517+
pattern: str | SupportsPathLike,
518+
*,
519+
case_sensitive: bool | None = None,
520+
) -> bool:
521+
return self.__wrapped__.full_match(pattern, case_sensitive=case_sensitive)
516522

517523

518524
UPath.register(ProxyUPath)

upath/implementations/data.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ def write_bytes(self, data: bytes) -> int:
6161
def write_text(
6262
self,
6363
data: str,
64-
encoding: str | None = ...,
65-
errors: str | None = ...,
66-
newline: str | None = ...,
64+
encoding: str | None = None,
65+
errors: str | None = None,
66+
newline: str | None = None,
6767
) -> int:
6868
raise UnsupportedOperation("DataPath does not support writing")

upath/implementations/http.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,14 @@ def path(self) -> str:
6464
sr = urlsplit(super().path)
6565
return sr._replace(path=sr.path or "/").geturl()
6666

67-
def is_file(self) -> bool:
67+
def is_file(self, *, follow_symlinks: bool = True) -> bool:
68+
if not follow_symlinks:
69+
warnings.warn(
70+
f"{type(self).__name__}.is_file(follow_symlinks=False):"
71+
" is currently ignored.",
72+
UserWarning,
73+
stacklevel=2,
74+
)
6875
try:
6976
next(super().iterdir())
7077
except (StopIteration, NotADirectoryError):
@@ -74,7 +81,14 @@ def is_file(self) -> bool:
7481
else:
7582
return False
7683

77-
def is_dir(self) -> bool:
84+
def is_dir(self, *, follow_symlinks: bool = True) -> bool:
85+
if not follow_symlinks:
86+
warnings.warn(
87+
f"{type(self).__name__}.is_dir(follow_symlinks=False):"
88+
" is currently ignored.",
89+
UserWarning,
90+
stacklevel=2,
91+
)
7892
try:
7993
next(super().iterdir())
8094
except (StopIteration, NotADirectoryError):

upath/implementations/local.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,10 +394,18 @@ def info(self) -> PathInfo:
394394

395395
if sys.version_info < (3, 13):
396396

397-
def full_match(self, pattern: str) -> bool:
397+
def full_match(
398+
self,
399+
pattern: str | os.PathLike[str],
400+
*,
401+
case_sensitive: bool | None = None,
402+
) -> bool:
398403
# hacky workaround for missing pathlib.Path.full_match in python < 3.13
399404
# todo: revisit
400-
return self.match(pattern)
405+
return self.match(
406+
pattern, # type: ignore[arg-type]
407+
case_sensitive=case_sensitive,
408+
)
401409

402410
@classmethod
403411
def from_uri(cls, uri: str, **storage_options: Any) -> Self:

0 commit comments

Comments
 (0)