diff --git a/upath/core.py b/upath/core.py index 047a9574..992a2f23 100644 --- a/upath/core.py +++ b/upath/core.py @@ -2,6 +2,7 @@ from __future__ import annotations +import inspect import sys import warnings from abc import ABCMeta @@ -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. @@ -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 @@ -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, diff --git a/upath/tests/cases.py b/upath/tests/cases.py index 9abfc3a4..47407bdc 100644 --- a/upath/tests/cases.py +++ b/upath/tests/cases.py @@ -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() diff --git a/upath/tests/implementations/test_azure.py b/upath/tests/implementations/test_azure.py index ee8ed625..700a5d0b 100644 --- a/upath/tests/implementations/test_azure.py +++ b/upath/tests/implementations/test_azure.py @@ -2,6 +2,7 @@ import fsspec import pytest +from packaging.version import Version from upath import UPath from upath.implementations.cloud import AzurePath @@ -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" diff --git a/upath/tests/implementations/test_s3.py b/upath/tests/implementations/test_s3.py index 226324e0..e5f070fc 100644 --- a/upath/tests/implementations/test_s3.py +++ b/upath/tests/implementations/test_s3.py @@ -5,6 +5,7 @@ import fsspec import pytest +from packaging.version import Version from upath import UPath from upath.implementations.cloud import S3Path @@ -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"