From 220837e80abe5829a7fc8587de5323b74856b524 Mon Sep 17 00:00:00 2001 From: Andreas Poehlmann Date: Sun, 8 Nov 2026 19:33:58 +0100 Subject: [PATCH 1/2] upath.core: fix rename() of directories UPath.rename() called fs.mv() without recursive=True unless the deprecated keyword argument was passed, so renaming a directory raised on memory:// and silently moved nothing on s3:// while returning the target path. Directory sources are now moved recursively. With fsspec>=2026.6.0 AbstractFileSystem.mv(recursive=True) fails on object stores because copying the pseudo-directory key raises, so the s3 test is marked xfail for those versions. --- upath/core.py | 11 ++++++++++- upath/tests/cases.py | 11 +++++++++++ upath/tests/implementations/test_s3.py | 12 ++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/upath/core.py b/upath/core.py index 90ebac60..a3ce53f1 100644 --- a/upath/core.py +++ b/upath/core.py @@ -1900,7 +1900,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 @@ -1995,6 +1995,15 @@ def rename( stacklevel=2, ) kwargs["maxdepth"] = maxdepth + if "recursive" not in kwargs: + # without recursive=True some filesystems (i.e. memory, s3) + # fail to move directories or silently move nothing at all + 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 22587fa9..eefd09c0 100644 --- a/upath/tests/cases.py +++ b/upath/tests/cases.py @@ -857,6 +857,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_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" From 7409dea64bc3d353ef9220674a90c8b8960d8efd Mon Sep 17 00:00:00 2001 From: Andreas Poehlmann Date: Tue, 10 Nov 2026 21:59:02 +0100 Subject: [PATCH 2/2] upath.core: only pass recursive to mv() implementations accepting it SFTPFileSystem.mv(old, new) does a native rename that already handles directories and raises TypeError on the injected recursive=True. Also mark the azure rename directory test xfail for fsspec>=2026.6.0 for the same pseudo-directory reason as on s3. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- upath/core.py | 17 +++++++++++++++-- upath/tests/implementations/test_azure.py | 12 ++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/upath/core.py b/upath/core.py index c18275b8..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. @@ -2003,9 +2014,11 @@ def rename( # noqa: C901 stacklevel=2, ) kwargs["maxdepth"] = maxdepth - if "recursive" not in kwargs: + 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 + # 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: 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"