Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 088a09a

Browse files
shangdahaojaraco
authored andcommitted
bpo-31163: Added return values to pathlib.Path instance's rename and replace methods. (GH-13582)
* bpo-31163: Added return values to pathlib.Path instance's rename and replace methods.
1 parent f9b5840 commit 088a09a

4 files changed

Lines changed: 30 additions & 11 deletions

File tree

Doc/library/pathlib.rst

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -946,23 +946,32 @@ call fails (for example because the path doesn't exist).
946946

947947
.. method:: Path.rename(target)
948948

949-
Rename this file or directory to the given *target*. On Unix, if
950-
*target* exists and is a file, it will be replaced silently if the user
951-
has permission. *target* can be either a string or another path object::
949+
Rename this file or directory to the given *target*, and return a new Path
950+
instance pointing to *target*. On Unix, if *target* exists and is a file,
951+
it will be replaced silently if the user has permission. *target* can be
952+
either a string or another path object::
952953

953954
>>> p = Path('foo')
954955
>>> p.open('w').write('some text')
955956
9
956957
>>> target = Path('bar')
957958
>>> p.rename(target)
959+
PosixPath('bar')
958960
>>> target.open().read()
959961
'some text'
960962

963+
.. versionchanged:: 3.8
964+
Added return value, return the new Path instance.
965+
961966

962967
.. method:: Path.replace(target)
963968

964-
Rename this file or directory to the given *target*. If *target* points
965-
to an existing file or directory, it will be unconditionally replaced.
969+
Rename this file or directory to the given *target*, and return a new Path
970+
instance pointing to *target*. If *target* points to an existing file or
971+
directory, it will be unconditionally replaced.
972+
973+
.. versionchanged:: 3.8
974+
Added return value, return the new Path instance.
966975

967976

968977
.. method:: Path.resolve(strict=False)

Lib/pathlib.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,20 +1341,24 @@ def link_to(self, target):
13411341

13421342
def rename(self, target):
13431343
"""
1344-
Rename this path to the given path.
1344+
Rename this path to the given path,
1345+
and return a new Path instance pointing to the given path.
13451346
"""
13461347
if self._closed:
13471348
self._raise_closed()
13481349
self._accessor.rename(self, target)
1350+
return self.__class__(target)
13491351

13501352
def replace(self, target):
13511353
"""
13521354
Rename this path to the given path, clobbering the existing
1353-
destination if it exists.
1355+
destination if it exists, and return a new Path instance
1356+
pointing to the given path.
13541357
"""
13551358
if self._closed:
13561359
self._raise_closed()
13571360
self._accessor.replace(self, target)
1361+
return self.__class__(target)
13581362

13591363
def symlink_to(self, target, target_is_directory=False):
13601364
"""

Lib/test/test_pathlib.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,12 +1767,14 @@ def test_rename(self):
17671767
size = p.stat().st_size
17681768
# Renaming to another path.
17691769
q = P / 'dirA' / 'fileAA'
1770-
p.rename(q)
1770+
renamed_p = p.rename(q)
1771+
self.assertEqual(renamed_p, q)
17711772
self.assertEqual(q.stat().st_size, size)
17721773
self.assertFileNotFound(p.stat)
17731774
# Renaming to a str of a relative path.
17741775
r = rel_join('fileAAA')
1775-
q.rename(r)
1776+
renamed_q = q.rename(r)
1777+
self.assertEqual(renamed_q, self.cls(r))
17761778
self.assertEqual(os.stat(r).st_size, size)
17771779
self.assertFileNotFound(q.stat)
17781780

@@ -1782,12 +1784,14 @@ def test_replace(self):
17821784
size = p.stat().st_size
17831785
# Replacing a non-existing path.
17841786
q = P / 'dirA' / 'fileAA'
1785-
p.replace(q)
1787+
replaced_p = p.replace(q)
1788+
self.assertEqual(replaced_p, q)
17861789
self.assertEqual(q.stat().st_size, size)
17871790
self.assertFileNotFound(p.stat)
17881791
# Replacing another (existing) path.
17891792
r = rel_join('dirB', 'fileB')
1790-
q.replace(r)
1793+
replaced_q = q.replace(r)
1794+
self.assertEqual(replaced_q, self.cls(r))
17911795
self.assertEqual(os.stat(r).st_size, size)
17921796
self.assertFileNotFound(q.stat)
17931797

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pathlib.Path instance's rename and replace methods now return the new Path
2+
instance.

0 commit comments

Comments
 (0)