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
15 changes: 13 additions & 2 deletions upath/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1880,7 +1880,7 @@ def unlink(self, missing_ok: bool = False) -> None:
return
self.fs.rm(self.path, recursive=False)

def rmdir(self, recursive: bool = True) -> None: # fixme: non-standard
def rmdir(self, recursive: bool = UNSET_DEFAULT) -> None: # fixme: non-standard
"""
Remove this directory.

Expand All @@ -1896,7 +1896,18 @@ def rmdir(self, recursive: bool = True) -> None: # fixme: non-standard
"""
if not self.is_dir():
raise NotADirectoryError(str(self))
if not recursive and next(self.iterdir()): # type: ignore[arg-type]

has_contents = bool(next(self.iterdir()))

if recursive is UNSET_DEFAULT:
if has_contents:
warnings.warn(
"This function will change behavior in a future version.",
FutureWarning,
)
recursive = True

if not recursive and has_contents:
raise OSError(f"Not recursive and directory not empty: {self}")
self.fs.rm(self.path, recursive=recursive)

Expand Down
11 changes: 7 additions & 4 deletions upath/implementations/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,13 @@ def open(
)

def rmdir(self, recursive: bool = UNSET_DEFAULT) -> None:
if recursive is UNSET_DEFAULT or not recursive:
return super().rmdir()
else:
shutil.rmtree(self)
if recursive is UNSET_DEFAULT:
warnings.warn(
"This function will change behavior in a future version.", FutureWarning
)
recursive = True

return shutil.rmtree(self) if recursive else super().rmdir()

# we need to override pathlib.Path._copy_from to support it as a
# WritablePath._copy_from target with support for on_name_collision
Expand Down