-
-
Notifications
You must be signed in to change notification settings - Fork 35k
bpo-31163: Adding return values to pathlib's rename and replace methods #4055
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1265,19 +1265,33 @@ def lstat(self): | |
| def rename(self, target): | ||
| """ | ||
| Rename this path to the given path. | ||
|
|
||
| If target is an instance of Path returns it, otherwise creates | ||
| new instance from target (string) and returns it. | ||
| """ | ||
| if self._closed: | ||
| self._raise_closed() | ||
| self._accessor.rename(self, target) | ||
| if isinstance(target, str): | ||
| target = self.__class__(target) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be safer to call if self._closed:
self._raise_closed()
self._accessor.rename(self, target)
return self.__class__(target)
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I was considering this approach as well, just wanted to avoid having two objects representing the same thing. |
||
|
|
||
| return target | ||
|
|
||
| def replace(self, target): | ||
| """ | ||
| Rename this path to the given path, clobbering the existing | ||
| destination if it exists. | ||
|
|
||
| If target is an instance of Path returns it, otherwise creates | ||
| new instance from target (string) and returns it. | ||
| """ | ||
| if self._closed: | ||
| self._raise_closed() | ||
| self._accessor.replace(self, target) | ||
| if isinstance(target, str): | ||
| target = self.__class__(target) | ||
|
|
||
| return target | ||
|
|
||
| def symlink_to(self, target, target_is_directory=False): | ||
| """ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| pathlib.PurePath.rename and pathlib.PurePath.replace now have return values | ||
| Return value is either Path (input object) or string, based on input's type. | ||
| Patch by Janko Krstic. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The example below needs to be updated now that
p.rename(target)is returning a value (double-check this in a terminal):