Skip to content
Merged
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
24 changes: 23 additions & 1 deletion upath/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import inspect
import sys
import warnings
from abc import ABCMeta
Expand Down Expand Up @@ -119,6 +120,16 @@ def _raise_unsupported(cls_name: str, method: str) -> NoReturn:
raise UnsupportedOperation(f"{cls_name}.{method}() is unsupported")


def _accepts_keyword(func: Any, name: str) -> bool:
try:
params = inspect.signature(func).parameters
except (TypeError, ValueError):
return False
return name in params or any(
p.kind is inspect.Parameter.VAR_KEYWORD for p in params.values()
)


class _IncompatibleProtocolError(TypeError, ValueError):
"""switch to TypeError for incompatible protocols in a backward compatible way.

Expand Down Expand Up @@ -1908,7 +1919,7 @@ def rmdir(self, recursive: bool = True) -> None: # fixme: non-standard
raise OSError(f"Not recursive and directory not empty: {self}")
self.fs.rm(self.path, recursive=recursive)

def rename(
def rename( # noqa: C901
self,
target: WritablePathLike,
*, # note: non-standard compared to pathlib
Expand Down Expand Up @@ -2003,6 +2014,17 @@ def rename(
stacklevel=2,
)
kwargs["maxdepth"] = maxdepth
if "recursive" not in kwargs and _accepts_keyword(self.fs.mv, "recursive"):
# without recursive=True some filesystems (i.e. memory, s3)
# fail to move directories or silently move nothing at all.
# Filesystems whose mv() does not accept it (i.e. sftp) do a
# native rename which already handles directories.
try:
is_dir = self.fs.info(source_abs.path)["type"] == "directory"
except FileNotFoundError:
is_dir = False
if is_dir:
kwargs["recursive"] = True
self.fs.mv(
source_abs.path,
target_abs.path,
Expand Down
11 changes: 11 additions & 0 deletions upath/tests/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,17 @@ def test_rename(self):
assert not p_moved.exists()
assert p_revert.exists()

def test_rename_directory(self):
p_source = self.path.joinpath("folder1")
p_target = self.path.joinpath("folder1_renamed")
children = sorted(p.name for p in p_source.iterdir())

p_moved = p_source.rename(p_target)
assert p_moved == p_target
assert not p_source.exists()
assert p_moved.is_dir()
assert sorted(p.name for p in p_moved.iterdir()) == children

@pytest.fixture
def supports_cwd(self):
# intentionally called on the instance to support ProxyUPath().cwd()
Expand Down
12 changes: 12 additions & 0 deletions upath/tests/implementations/test_azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import fsspec
import pytest
from packaging.version import Version

from upath import UPath
from upath.implementations.cloud import AzurePath
Expand Down Expand Up @@ -40,6 +41,17 @@ def test_protocol(self):
protocols = ["abfs", "abfss", "adl", "az"]
assert protocol in protocols

@overrides_base
@pytest.mark.xfail(
Version(fsspec.__version__) >= Version("2026.6.0"),
reason="fsspec>=2026.6.0 AbstractFileSystem.mv(recursive=True) raises"
" FileNotFoundError when copying the pseudo-directory key",
raises=FileNotFoundError,
strict=False,
)
def test_rename_directory(self):
super().test_rename_directory()

@extends_base
def test_rmdir(self):
new_dir = self.path / "new_dir_rmdir"
Expand Down
12 changes: 12 additions & 0 deletions upath/tests/implementations/test_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import fsspec
import pytest
from packaging.version import Version

from upath import UPath
from upath.implementations.cloud import S3Path
Expand Down Expand Up @@ -44,6 +45,17 @@ def path(self, s3_fixture):
def test_is_correct_class(self):
assert isinstance(self.path, S3Path)

@overrides_base
@pytest.mark.xfail(
Version(fsspec.__version__) >= Version("2026.6.0"),
reason="fsspec>=2026.6.0 AbstractFileSystem.mv(recursive=True) raises"
" FileNotFoundError when copying the pseudo-directory key",
raises=FileNotFoundError,
strict=False,
)
def test_rename_directory(self):
super().test_rename_directory()

@extends_base
def test_rmdir(self):
dirname = "rmdir_test"
Expand Down
Loading