Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion upath/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1186,7 +1186,8 @@ def parts(self) -> Sequence[str]:
def with_name(self, name: str) -> Self:
"""Return a new path with the file name changed."""
split = self.parser.split
if self.parser.sep in name: # `split(name)[0]`
# `self.parser.sep in name` is `split(name)[0]`
if self.parser.sep in name or name in ("", "."):
raise ValueError(f"Invalid name {name!r}")
_path = self.__vfspath__()
_path = _path.removesuffix(split(_path)[1]) + name
Expand Down
6 changes: 6 additions & 0 deletions upath/tests/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ def test_with_name(self):
path = path.with_name("file.zip")
assert path.name == "file.zip"

@pytest.mark.parametrize("name", ["", "."])
def test_with_name_invalid(self, name):
path = self.path / "file.txt"
with pytest.raises(ValueError, match="Invalid name"):
path.with_name(name)

def test_with_suffix(self):
path = self.path / "file.txt"
path = path.with_suffix(".zip")
Expand Down
7 changes: 7 additions & 0 deletions upath/tests/implementations/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ def test_with_name(self):
with pytest.raises(UnsupportedOperation):
self.path.with_name("newname")

@overrides_base
@pytest.mark.parametrize("name", ["", "."])
def test_with_name_invalid(self, name):
# DataPath does not support name changes
with pytest.raises(UnsupportedOperation):
self.path.with_name(name)

@overrides_base
def test_with_suffix(self):
# DataPath does not support suffix changes
Expand Down
Loading