Skip to content

Commit 4d7be31

Browse files
committed
Merge remote-tracking branch 'origin/main' into subclassing
2 parents eae14c2 + f8a9ff5 commit 4d7be31

File tree

3 files changed

+106
-20
lines changed

3 files changed

+106
-20
lines changed

upath/core.py

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,6 @@ class UPath(pathlib.Path):
9797
"home",
9898
"expanduser",
9999
"group",
100-
"is_mount",
101-
"is_symlink",
102-
"is_socket",
103-
"is_fifo",
104-
"is_block_device",
105-
"is_char_device",
106100
"lchmod",
107101
"lstat",
108102
"owner",
@@ -112,8 +106,26 @@ class UPath(pathlib.Path):
112106
def __new__(cls, *args, **kwargs) -> Union["UPath", pathlib.Path]:
113107
if issubclass(cls, UPath):
114108
args_list = list(args)
115-
url = args_list.pop(0)
116-
url = stringify_path(url)
109+
first = args_list.pop(0)
110+
if isinstance(first, pathlib.PurePath):
111+
# Create a (modified) copy, if first arg is a Path object
112+
other = first
113+
parts = args_list
114+
drv, root, parts = other._parse_args(parts)
115+
drv, root, parts = other._flavour.join_parsed_parts(
116+
other._drv, other._root, other._parts, drv, root, parts
117+
)
118+
119+
new_kwargs = getattr(other, "_kwargs", {}).copy()
120+
new_kwargs.pop("_url", None)
121+
new_kwargs.update(kwargs)
122+
123+
return other.__class__(
124+
other._format_parsed_parts(drv, root, parts),
125+
**new_kwargs,
126+
)
127+
128+
url = stringify_path(first)
117129
parsed_url = urllib.parse.urlparse(url)
118130
for key in ["scheme", "netloc"]:
119131
val = kwargs.get(key)
@@ -263,6 +275,27 @@ def is_file(self):
263275
return True
264276
return False
265277

278+
def is_mount(self):
279+
return False
280+
281+
def is_symlink(self):
282+
info = self._accessor.info(self)
283+
if "islink" in info:
284+
return info["islink"]
285+
return False
286+
287+
def is_socket(self):
288+
return False
289+
290+
def is_fifo(self):
291+
return False
292+
293+
def is_block_device(self):
294+
return False
295+
296+
def is_char_device(self):
297+
return False
298+
266299
def chmod(self, mod):
267300
raise NotImplementedError
268301

upath/tests/cases.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,28 +62,22 @@ def test_is_file(self):
6262
assert not self.path.is_file()
6363

6464
def test_is_mount(self):
65-
with pytest.raises(NotImplementedError):
66-
self.path.is_mount()
65+
assert self.path.is_mount() is False
6766

6867
def test_is_symlink(self):
69-
with pytest.raises(NotImplementedError):
70-
self.path.is_symlink()
68+
assert self.path.is_symlink() is False
7169

7270
def test_is_socket(self):
73-
with pytest.raises(NotImplementedError):
74-
self.path.is_socket()
71+
assert self.path.is_socket() is False
7572

7673
def test_is_fifo(self):
77-
with pytest.raises(NotImplementedError):
78-
self.path.is_fifo()
74+
assert self.path.is_fifo() is False
7975

8076
def test_is_block_device(self):
81-
with pytest.raises(NotImplementedError):
82-
self.path.is_block_device()
77+
assert self.path.is_block_device() is False
8378

8479
def test_is_char_device(self):
85-
with pytest.raises(NotImplementedError):
86-
self.path.is_char_device()
80+
assert self.path.is_char_device() is False
8781

8882
def test_iterdir(self, local_testdir):
8983
pl_path = Path(local_testdir)
@@ -259,3 +253,14 @@ def test_child_path(self):
259253
assert path_a._drv == path_b._drv
260254
assert path_a._parts == path_b._parts
261255
assert path_a._url == path_b._url
256+
257+
def test_copy_path(self):
258+
path = self.path
259+
copy_path = UPath(path)
260+
261+
assert type(path) == type(copy_path)
262+
assert str(path) == str(copy_path)
263+
assert path._drv == copy_path._drv
264+
assert path._root == copy_path._root
265+
assert path._parts == copy_path._parts
266+
assert path.fs.storage_options == copy_path.fs.storage_options

upath/tests/test_core.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,51 @@ def test_pickling_child_path():
185185
assert path._root == recovered_path._root
186186
assert path._parts == recovered_path._parts
187187
assert path.fs.storage_options == recovered_path.fs.storage_options
188+
189+
190+
def test_copy_path():
191+
path = UPath("gcs://bucket/folder", anon=True)
192+
copy_path = UPath(path)
193+
194+
print(type(path), type(copy_path))
195+
196+
assert type(path) == type(copy_path)
197+
assert str(path) == str(copy_path)
198+
assert path._drv == copy_path._drv
199+
assert path._root == copy_path._root
200+
assert path._parts == copy_path._parts
201+
assert path.fs.storage_options == copy_path.fs.storage_options
202+
203+
204+
def test_copy_path_posix():
205+
path = UPath("/tmp/folder")
206+
copy_path = UPath(path)
207+
208+
assert type(path) == type(copy_path) == type(pathlib.Path(""))
209+
assert str(path) == str(copy_path)
210+
assert path._drv == copy_path._drv
211+
assert path._root == copy_path._root
212+
assert path._parts == copy_path._parts
213+
214+
215+
def test_copy_path_append():
216+
path = UPath("/tmp/folder")
217+
copy_path = UPath(path, "folder2")
218+
219+
assert type(path) == type(copy_path) == type(pathlib.Path(""))
220+
assert str(path / "folder2") == str(copy_path)
221+
222+
path = UPath("/tmp/folder")
223+
copy_path = UPath(path, "folder2/folder3")
224+
225+
assert str(path / "folder2" / "folder3") == str(copy_path)
226+
227+
228+
def test_copy_path_append_kwargs():
229+
path = UPath("gcs://bucket/folder", anon=True)
230+
copy_path = UPath(path, anon=False)
231+
232+
assert type(path) == type(copy_path)
233+
assert str(path) == str(copy_path)
234+
assert not copy_path._kwargs["anon"]
235+
assert path._kwargs["anon"]

0 commit comments

Comments
 (0)