Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added with_stem support #290

Merged
merged 6 commits into from
Jul 24, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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