Skip to content

Commit 20dfa94

Browse files
committed
adds copy-constructor to UPath
1 parent df97e1e commit 20dfa94

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

upath/core.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,24 @@ class UPath(pathlib.Path, PureUPath, metaclass=UPathMeta):
123123
_default_accessor = _FSSpecAccessor
124124

125125
def __new__(cls, *args, **kwargs):
126+
if len(args) == 1 and isinstance(args[0], cls):
127+
other = args[0]
128+
new_args = (
129+
other._format_parsed_parts(
130+
other._drv, other._root, other._parts
131+
),
132+
)
133+
new_kwargs = {}
134+
if hasattr(other, "_kwargs"):
135+
new_kwargs = other._kwargs.copy()
136+
new_kwargs.pop("_url", None)
137+
138+
return cls.__new__(
139+
cls,
140+
*new_args,
141+
**new_kwargs,
142+
)
143+
126144
if issubclass(cls, UPath):
127145
args_list = list(args)
128146
url = args_list.pop(0)

upath/tests/test_core.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,15 @@ def test_pickling_child_path():
176176
assert path._root == recovered_path._root
177177
assert path._parts == recovered_path._parts
178178
assert path.fs.storage_options == recovered_path.fs.storage_options
179+
180+
181+
def test_copy_path():
182+
path = UPath("gcs://bucket/folder", anon=True)
183+
copy_path = UPath(path)
184+
185+
assert type(path) == type(copy_path)
186+
assert str(path) == str(copy_path)
187+
assert path._drv == copy_path._drv
188+
assert path._root == copy_path._root
189+
assert path._parts == copy_path._parts
190+
assert path.fs.storage_options == copy_path.fs.storage_options

0 commit comments

Comments
 (0)