This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: File-altering aspects of pathlib should return new pathlib objects
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Walter Szeliga, brett.cannon, pitrou, serhiy.storchaka
Priority: normal Keywords:

Created on 2017-02-03 00:04 by Walter Szeliga, last changed 2022-04-11 14:58 by admin.

Messages (4)
msg286835 - (view) Author: Walter Szeliga (Walter Szeliga) Date: 2017-02-03 00:04
Methods on pathlib.PosixPath() and other objects that alter files on the file system should return new object instances with new information. For example, if there exists a file on the system called 'bar', then

bar_var = pathlib.PosixPath('bar')
bar_var.rename('foo')

will rename the file 'bar' to 'foo' on the system and leave 'bar' as a still-valid object that no longer points to a system file. Changing the return type of .rename() to return a new pathlib.PosixPath() object with path 'foo' could help reduce confusion about the behavior. For example:

bar_var = pathlib.PosixPath('bar')
foo_var = bar_var.rename('foo')

and foo_var would then be a pathlib.PosixPath() object pointing to 'foo'.
msg287164 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2017-02-06 18:44
The idea in general seems reasonable. Anyone else have an opinion?
msg287218 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-02-07 09:32
Path.rename() is simple wrapper around os.rename() and returns the result of os.rename(). If os.rename() became returning something useful, this change will made impossible to return it from Path.rename().

Why not create destination Path object explicitly?

    bar_var = pathlib.PosixPath('bar')
    foo_var = pathlib.PosixPath('foo')
    bar_var.rename(foo_var)

Note, that in 3.6 you can also write

    os.rename(bar_var, foo_var)
msg287252 - (view) Author: Walter Szeliga (Walter Szeliga) Date: 2017-02-07 19:04
If Path.rename() were made to be slightly more than a wrapper around os.rename(), then any future changes to the return value of os.rename() could be taken into consideration in the return value of Path.rename(). I don't see how anything would become impossible then.

Creating the destination Path object explicitly is my solution right now. Since it had become such a pattern in my code, I figured it could be delegated up to the level of the Path object and make for cleaner looking code.
History
Date User Action Args
2022-04-11 14:58:42adminsetgithub: 73611
2017-02-07 19:04:49Walter Szeligasetmessages: + msg287252
2017-02-07 09:32:32serhiy.storchakasetnosy: + serhiy.storchaka, pitrou

messages: + msg287218
versions: + Python 3.7, - Python 3.6
2017-02-06 18:44:21brett.cannonsetnosy: + brett.cannon
messages: + msg287164
2017-02-03 00:04:13Walter Szeligacreate