Skip to content

Commit

Permalink
Added with_stem support (#290)
Browse files Browse the repository at this point in the history
* Added with_stem support

* Update readme (+small fix to make_support_table)

* Added test

* Update history.md

* Fallback to trivial CPython implementation if `with_stem` is not available

* Add a clarification comment
  • Loading branch information
Gilthans committed Jul 24, 2023
1 parent b7d394f commit 4a32c68
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## UNRELEASED
- Add "CloudPath" as return type on `__init__` for mypy issues. ([Issue #179](https://github.com/drivendataorg/cloudpathlib/issues/179), [PR #342](https://github.com/drivendataorg/cloudpathlib/pull/342))
- Add `with_stem` to all path types when python version supports it (>=3.9). ([Issue #287](https://github.com/drivendataorg/cloudpathlib/issues/287), [PR #290](https://github.com/drivendataorg/cloudpathlib/pull/290), thanks to [@Gilthans](https://github.com/Gilthans))

## v0.15.1 (2023-07-12)

Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ Most methods and properties from `pathlib.Path` are supported except for the one
| `touch` ||||
| `unlink` ||||
| `with_name` ||||
| `with_stem` ||||
| `with_suffix` ||||
| `write_bytes` ||||
| `write_text` ||||
Expand All @@ -184,7 +185,7 @@ Most methods and properties from `pathlib.Path` are supported except for the one
| `readlink` ||||
| `root` ||||
| `symlink_to` ||||
| `with_stem` | | | |
| `clear_cache` | | | |
| `cloud_prefix` ||||
| `copy` ||||
| `copytree` ||||
Expand All @@ -194,6 +195,7 @@ Most methods and properties from `pathlib.Path` are supported except for the one
| `is_valid_cloudpath` ||||
| `rmtree` ||||
| `upload_from` ||||
| `validate` ||||
| `blob` ||||
| `bucket` ||||
| `container` ||||
Expand Down
7 changes: 7 additions & 0 deletions cloudpathlib/cloudpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,13 @@ def suffix(self) -> str:
def suffixes(self) -> List[str]:
return self._dispatch_to_path("suffixes")

def with_stem(self, stem: str) -> Self:
try:
return self._dispatch_to_path("with_stem", stem)
except AttributeError:
# with_stem was only added in python 3.9, so we fallback for compatibility
return self.with_name(stem + self.suffix)

def with_name(self, name: str) -> Self:
return self._dispatch_to_path("with_name", name)

Expand Down
2 changes: 1 addition & 1 deletion docs/make_support_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def print_table():
for _cls, methods in lib_methods.items():
all_methods = all_methods.union(methods)

df = pd.DataFrame(index=all_methods)
df = pd.DataFrame(index=list(all_methods))
df.index.name = "Methods + properties"

for _cls, methods in lib_methods.items():
Expand Down
7 changes: 7 additions & 0 deletions tests/test_cloudpath_manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ def test_with_suffix(rig):
)


def test_with_stem(rig):
assert (
str(rig.create_cloud_path("a/b/c/old.file").with_stem("new"))
== f"{rig.cloud_prefix}{rig.drive}/{rig.test_dir}/a/b/c/new.file"
)


def test_no_op_actions(rig):
path = rig.create_cloud_path("a/b/c/d.file")
assert path == path.absolute()
Expand Down

0 comments on commit 4a32c68

Please sign in to comment.