Skip to content

Commit 4a6b287

Browse files
committed
fixes
1 parent 705d810 commit 4a6b287

File tree

2 files changed

+44
-19
lines changed

2 files changed

+44
-19
lines changed

upath/core.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -123,28 +123,28 @@ 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()
126+
if issubclass(cls, UPath):
127+
args_list = list(args)
128+
first = args_list.pop(0)
129+
if isinstance(first, pathlib.PurePath):
130+
# Create a (modified) copy, if first arg is a Path object
131+
other = first
132+
parts = args_list
133+
drv, root, parts = other._parse_args(parts)
134+
drv, root, parts = other._flavour.join_parsed_parts(
135+
other._drv, other._root, other._parts, drv, root, parts
136+
)
137+
138+
new_kwargs = getattr(other, "_kwargs", {}).copy()
136139
new_kwargs.pop("_url", None)
140+
new_kwargs.update(kwargs)
137141

138-
return cls.__new__(
139-
cls,
140-
*new_args,
141-
**new_kwargs,
142-
)
142+
return other.__class__(
143+
other._format_parsed_parts(drv, root, parts),
144+
**new_kwargs,
145+
)
143146

144-
if issubclass(cls, UPath):
145-
args_list = list(args)
146-
url = args_list.pop(0)
147-
url = stringify_path(url)
147+
url = stringify_path(first)
148148
parsed_url = urllib.parse.urlparse(url)
149149
for key in ["scheme", "netloc"]:
150150
val = kwargs.get(key)

upath/tests/test_core.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ def test_copy_path():
182182
path = UPath("gcs://bucket/folder", anon=True)
183183
copy_path = UPath(path)
184184

185+
print(type(path), type(copy_path))
186+
185187
assert type(path) == type(copy_path)
186188
assert str(path) == str(copy_path)
187189
assert path._drv == copy_path._drv
@@ -199,3 +201,26 @@ def test_copy_path_posix():
199201
assert path._drv == copy_path._drv
200202
assert path._root == copy_path._root
201203
assert path._parts == copy_path._parts
204+
205+
206+
def test_copy_path_append():
207+
path = UPath("/tmp/folder")
208+
copy_path = UPath(path, "folder2")
209+
210+
assert type(path) == type(copy_path) == type(pathlib.Path(""))
211+
assert f"{path}/folder2" == str(copy_path)
212+
213+
path = UPath("/tmp/folder")
214+
copy_path = UPath(path, "folder2/folder3")
215+
216+
assert f"{path}/folder2/folder3" == str(copy_path)
217+
218+
219+
def test_copy_path_append_kwargs():
220+
path = UPath("gcs://bucket/folder", anon=True)
221+
copy_path = UPath(path, anon=False)
222+
223+
assert type(path) == type(copy_path)
224+
assert str(path) == str(copy_path)
225+
assert not copy_path._kwargs["anon"]
226+
assert path._kwargs["anon"]

0 commit comments

Comments
 (0)