Skip to content

Commit 003b04f

Browse files
authored
upath: update to pathlib-abc==0.5.0 (#402)
1 parent ed559a0 commit 003b04f

File tree

6 files changed

+49
-27
lines changed

6 files changed

+49
-27
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ maintainers = [
1616
requires-python = ">=3.9"
1717
dependencies = [
1818
"fsspec >=2024.5.0",
19-
"pathlib-abc ==0.4.3",
19+
"pathlib-abc ==0.5.0",
2020
]
2121
classifiers = [
2222
"Programming Language :: Python :: 3",

upath/core.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,9 @@ def with_segments(self, *pathsegments: JoinablePathLike) -> Self:
415415
)
416416

417417
def __str__(self) -> str:
418+
return self.__vfspath__()
419+
420+
def __vfspath__(self) -> str:
418421
return self._chain_parser.chain(self._chain.to_list())[0]
419422

420423
def __repr__(self) -> str:
@@ -484,12 +487,8 @@ def iterdir(self) -> Iterator[Self]:
484487
_, _, name = name.removesuffix(sep).rpartition(self.parser.sep)
485488
yield base.with_segments(str(base), name)
486489

487-
def __open_rb__(self, buffering: int = -1) -> BinaryIO:
488-
block_size = _buffering2blocksize("wb", buffering)
489-
kw = {}
490-
if block_size is not None:
491-
kw["block_size"] = block_size
492-
return self.fs.open(self.path, mode="rb", **kw)
490+
def __open_reader__(self) -> BinaryIO:
491+
return self.fs.open(self.path, mode="rb")
493492

494493
def readlink(self) -> Self:
495494
raise NotImplementedError
@@ -523,12 +522,8 @@ def mkdir(
523522
if not self.is_dir():
524523
raise FileExistsError(str(self))
525524

526-
def __open_wb__(self, buffering: int = -1) -> BinaryIO:
527-
block_size = _buffering2blocksize("wb", buffering)
528-
kw = {}
529-
if block_size is not None:
530-
kw["block_size"] = block_size
531-
return self.fs.open(self.path, mode="wb", **kw)
525+
def __open_writer__(self, mode: Literal["a", "w", "x"]) -> BinaryIO:
526+
return self.fs.open(self.path, mode=f"{mode}b")
532527

533528
# --- upath overrides ---------------------------------------------
534529

upath/extensions.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ def iterdir(self) -> Iterator[Self]:
123123
for pth in self.__wrapped__.iterdir():
124124
yield self._from_upath(pth)
125125

126-
def __open_rb__(self, buffering: int = -1) -> BinaryIO:
127-
return self.__wrapped__.__open_rb__(buffering)
126+
def __open_reader__(self) -> BinaryIO:
127+
return self.__wrapped__.__open_reader__()
128128

129129
def readlink(self) -> Self:
130130
return self._from_upath(self.__wrapped__.readlink())
@@ -144,8 +144,8 @@ def mkdir(
144144
) -> None:
145145
self.__wrapped__.mkdir(mode=mode, parents=parents, exist_ok=exist_ok)
146146

147-
def __open_wb__(self, buffering: int = -1) -> BinaryIO:
148-
return self.__wrapped__.__open_wb__(buffering)
147+
def __open_writer__(self, mode: Literal["a", "w", "x"]) -> BinaryIO:
148+
return self.__wrapped__.__open_writer__(mode)
149149

150150
@overload
151151
def open(

upath/types/__init__.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@
1717
from typing import overload
1818
from typing import runtime_checkable
1919

20-
from pathlib_abc import magic_open
21-
2220
from upath.types._abc import JoinablePath
2321
from upath.types._abc import PathInfo
2422
from upath.types._abc import PathParser
2523
from upath.types._abc import ReadablePath
2624
from upath.types._abc import WritablePath
25+
from upath.types._abc import vfsopen
2726

2827
if TYPE_CHECKING:
2928
if sys.version_info > (3, 11):
@@ -110,7 +109,7 @@ def open(
110109
errors: str | None = None,
111110
newline: str | None = None,
112111
) -> IO[Any]:
113-
return magic_open(self, mode, buffering, encoding, errors, newline)
112+
return vfsopen(self, mode, buffering, encoding, errors, newline)
114113

115114

116115
if sys.version_info >= (3, 14):
@@ -125,11 +124,10 @@ class CompatJoinablePath(Protocol):
125124
# not available in Python 3.9.* pathlib:
126125
# - `parser`
127126
# - `with_segments`
127+
# - `__vfspath__`
128128
# - `full_match`
129129
__slots__ = ()
130130

131-
def __str__(self) -> str: ...
132-
133131
@property
134132
def anchor(self) -> str: ...
135133
@property
@@ -161,8 +159,8 @@ def parents(self) -> Sequence[Self]: ...
161159
@runtime_checkable
162160
class CompatReadablePath(CompatJoinablePath, Protocol):
163161
# not available in Python 3.9.* pathlib:
164-
# - `__open_rb__`
165162
# - `info`
163+
# - `__open_reader__`
166164
# - `copy`
167165
# - `copy_into`
168166
# - `walk`
@@ -187,7 +185,7 @@ def readlink(self) -> Self: ...
187185
@runtime_checkable
188186
class CompatWritablePath(CompatJoinablePath, Protocol):
189187
# not available in Python 3.9.* pathlib:
190-
# - `__open_wb__`
188+
# - `__open_writer__`
191189
# - `_copy_from`
192190
__slots__ = ()
193191

upath/types/_abc.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55
from pathlib_abc import PathParser
66
from pathlib_abc import ReadablePath
77
from pathlib_abc import WritablePath
8+
from pathlib_abc import vfsopen
9+
from pathlib_abc import vfspath
810

911
__all__ = [
1012
"JoinablePath",
1113
"ReadablePath",
1214
"WritablePath",
1315
"PathInfo",
1416
"PathParser",
17+
"vfsopen",
18+
"vfspath",
1519
]

upath/types/_abc.pyi

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ from typing import Any
77
from typing import BinaryIO
88
from typing import Callable
99
from typing import Iterator
10+
from typing import Literal
1011
from typing import Protocol
1112
from typing import Sequence
13+
from typing import TextIO
1214
from typing import TypeVar
1315
from typing import runtime_checkable
1416

@@ -26,7 +28,7 @@ class JoinablePath(ABC):
2628
@abstractmethod
2729
def with_segments(self, *pathsegments: str | Self) -> Self: ...
2830
@abstractmethod
29-
def __str__(self) -> str: ...
31+
def __vfspath__(self) -> str: ...
3032
@property
3133
def anchor(self) -> str: ...
3234
@property
@@ -61,7 +63,7 @@ class ReadablePath(JoinablePath):
6163
@abstractmethod
6264
def info(self) -> PathInfo: ...
6365
@abstractmethod
64-
def __open_rb__(self, buffering: int = ...) -> BinaryIO: ...
66+
def __open_reader__(self) -> BinaryIO: ...
6567
def read_bytes(self) -> bytes: ...
6668
def read_text(
6769
self,
@@ -93,7 +95,7 @@ class WritablePath(JoinablePath):
9395
@abstractmethod
9496
def mkdir(self) -> None: ...
9597
@abstractmethod
96-
def __open_wb__(self, buffering: int = ...) -> BinaryIO: ...
98+
def __open_writer__(self, mode: Literal["a", "w", "x"]) -> BinaryIO: ...
9799
def write_bytes(self, data: bytes) -> int: ...
98100
def write_text(
99101
self,
@@ -119,3 +121,26 @@ class PathInfo(Protocol):
119121
def is_dir(self, *, follow_symlinks: bool = True) -> bool: ...
120122
def is_file(self, *, follow_symlinks: bool = True) -> bool: ...
121123
def is_symlink(self) -> bool: ...
124+
125+
class SupportsOpenReader(Protocol):
126+
def __open_reader__(self) -> BinaryIO: ...
127+
128+
class SupportsOpenWriter(Protocol):
129+
def __open_writer__(self, mode: Literal["a", "w", "x"]) -> BinaryIO: ...
130+
131+
class SupportsOpenUpdater(Protocol):
132+
def __open_updater__(self, mode: Literal["r+", "w+", "+r", "+w"]) -> BinaryIO: ...
133+
134+
def vfsopen(
135+
obj: SupportsOpenReader | SupportsOpenWriter | SupportsOpenUpdater,
136+
mode="r",
137+
buffering: int = -1,
138+
encoding: str | None = None,
139+
errors: str | None = None,
140+
newline: str | None = None,
141+
) -> BinaryIO | TextIO: ...
142+
143+
class SupportsVFSPath(Protocol):
144+
def __vfspath__(self) -> str: ...
145+
146+
def vfspath(obj: SupportsVFSPath) -> str: ...

0 commit comments

Comments
 (0)