Skip to content

Commit 484f7e7

Browse files
ap--jorenretel
andauthored
fix broken with_* methods (#73)
* tests: add test verifying broken with_* methods * Added with_suffix and with_name methods to upath.UPath from python 3.10 adding url as an argument to the _from_parsed_parts method. * improving formatting for flake8 Co-authored-by: Joren Retel <[email protected]>
1 parent aecb583 commit 484f7e7

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

upath/core.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,3 +417,35 @@ def __reduce__(self):
417417
(self._format_parsed_parts(self._drv, self._root, self._parts),),
418418
{"_kwargs": self._kwargs.copy()},
419419
)
420+
421+
def with_suffix(self, suffix):
422+
"""Return a new path with the file suffix changed. If the path
423+
has no suffix, add given suffix. If the given suffix is an empty
424+
string, remove the suffix from the path.
425+
"""
426+
f = self._flavour
427+
if f.sep in suffix or f.altsep and f.altsep in suffix:
428+
raise ValueError("Invalid suffix %r" % (suffix,))
429+
if suffix and not suffix.startswith('.') or suffix == '.':
430+
raise ValueError("Invalid suffix %r" % (suffix))
431+
name = self.name
432+
if not name:
433+
raise ValueError("%r has an empty name" % (self,))
434+
old_suffix = self.suffix
435+
if not old_suffix:
436+
name = name + suffix
437+
else:
438+
name = name[:-len(old_suffix)] + suffix
439+
return self._from_parsed_parts(self._drv, self._root,
440+
self._parts[:-1] + [name], url=self._url)
441+
442+
def with_name(self, name):
443+
"""Return a new path with the file name changed."""
444+
if not self.name:
445+
raise ValueError("%r has an empty name" % (self,))
446+
drv, root, parts = self._flavour.parse_parts((name,))
447+
if (not name or name[-1] in [self._flavour.sep, self._flavour.altsep]
448+
or drv or root or len(parts) != 1):
449+
raise ValueError("Invalid name %r" % (name))
450+
return self._from_parsed_parts(self._drv, self._root,
451+
self._parts[:-1] + [name], url=self._url)

upath/tests/cases.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,28 @@ def test_copy_path(self):
290290
assert path._root == copy_path._root
291291
assert path._parts == copy_path._parts
292292
assert path.fs.storage_options == copy_path.fs.storage_options
293+
294+
def test_with_name(self):
295+
path = self.path / "file.txt"
296+
path = path.with_name("file.zip")
297+
assert path.name == "file.zip"
298+
299+
def test_with_suffix(self):
300+
path = self.path / "file.txt"
301+
path = path.with_suffix(".zip")
302+
assert path.suffix == ".zip"
303+
304+
def test_with_stem(self):
305+
if sys.version_info < (3, 9):
306+
pytest.skip("with_stem only available on py3.9+")
307+
path = self.path / "file.txt"
308+
path = path.with_stem("document")
309+
assert path.stem == "document"
310+
311+
def test_repr_after_with_name(self):
312+
p = self.path.joinpath("file.txt").with_name("file.zip")
313+
assert "file.zip" in repr(p)
314+
315+
def test_repr_after_with_suffix(self):
316+
p = self.path.joinpath("file.txt").with_suffix(".zip")
317+
assert "file.zip" in repr(p)

0 commit comments

Comments
 (0)