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

GH-73991: Add pathlib.Path.move() #122073

Merged
merged 27 commits into from
Aug 25, 2024
Merged

GH-73991: Add pathlib.Path.move() #122073

merged 27 commits into from
Aug 25, 2024

Conversation

barneygale
Copy link
Contributor

@barneygale barneygale commented Jul 21, 2024

Add a Path.move() method that moves a file or directory tree, and returns a new Path instance pointing to the target.

This method is similar to shutil.move(), except that it doesn't accept a copy_function argument, and it doesn't check whether the destination is an existing directory.

In pathlib's private ABCs, PathBase.move() uses the copy() and delete() methods to move files and directories.


📚 Documentation preview 📚: https://cpython-previews--122073.org.readthedocs.build/

Add a `Path.move()` method that moves a file or directory tree and returns
a new `Path` instance.

This method is similar to `shutil.move()`, except that it doesn't accept a
*copy_function* argument, and it doesn't support copying into an existing
directory.
Doc/library/pathlib.rst Outdated Show resolved Hide resolved
Lib/test/test_pathlib/test_pathlib_abc.py Show resolved Hide resolved
@barneygale barneygale marked this pull request as draft July 22, 2024 00:04
@barneygale barneygale marked this pull request as ready for review July 22, 2024 04:41
Lib/pathlib/_abc.py Outdated Show resolved Hide resolved
Lib/pathlib/_abc.py Outdated Show resolved Hide resolved
Lib/pathlib/_local.py Outdated Show resolved Hide resolved
Doc/library/pathlib.rst Outdated Show resolved Hide resolved
Lib/test/test_pathlib/test_pathlib_abc.py Show resolved Hide resolved
@barneygale
Copy link
Contributor Author

Marking this as a draft as it will need to be revised once #122368 and #122369 land.

@barneygale barneygale marked this pull request as ready for review August 11, 2024 22:59
@barneygale
Copy link
Contributor Author

Should be ready to review again :-)

This isn't quite the last PR in this series. I'll log another to add copy_into() and move_into(), and then post to discuss.python.org about perhaps eliminating some/all of the ignore_errors, on_error and ignore arguments to delete() and copy().

@barneygale
Copy link
Contributor Author

Sorry for flip-flopping. I've moved the copy() changes into their own PR (#122924) so I'll mark this as a draft once again (-:

@barneygale barneygale marked this pull request as draft August 12, 2024 00:46
@picnixz
Copy link
Contributor

picnixz commented Aug 12, 2024

I've reviewed the other PR. Do you want me to review this one now or should I wait?

@barneygale
Copy link
Contributor Author

I've reviewed the other PR. Do you want me to review this one now or should I wait?

Sorry for the slow reply - I think it would be best to wait for the other PR to land before reviewing this.

@barneygale barneygale marked this pull request as ready for review August 23, 2024 19:34
@barneygale
Copy link
Contributor Author

Hey @picnixz, this is ready to review again FYI. Thanks in advance :)

Copy link
Contributor

@picnixz picnixz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be good to also have assertions before calling move(). That way you really know how the state of the files change before/after the test (and that way, we are sure that we are in a correct test environment). I did not mark all tests that need this but any test that does self.assertTrue(target.exists()) after moving should do self.assertFalse(target.exists()) before moving (and if the target is to be overwritten, then it should check that it's content changed).

@@ -1616,6 +1616,23 @@ Copying, renaming and deleting
Added return value, return the new :class:`!Path` instance.


.. method:: Path.move(target)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just me thinking loud, but do you think we should have a boolean to not overwrite an existing target (in which case, a FileExistsError would be raised)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could add a clobber=True argument, but I think that would be a separate PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have thought of strict (strict=False by default, strict=True would raise) or replace=True. But we can address this question later. Otherwise, it's just a three liner where users would do if not os.exists(target) if they want to avoid replacing files.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Clobber" has some currency already, e.g. in mv --no-clobber. But yeah, let's discuss later.

Lib/test/test_pathlib/test_pathlib_abc.py Show resolved Hide resolved
Lib/test/test_pathlib/test_pathlib_abc.py Show resolved Hide resolved
Lib/test/test_pathlib/test_pathlib_abc.py Show resolved Hide resolved
@barneygale
Copy link
Contributor Author

I don't understand why we'd make assertions before calling move() - we already know the state of the test directory (self.base) because it's configured to our specification in setUp(). If we applied this logic consistently we'd need to adjust almost every test case in DummyPathTest and PathTest!

@picnixz
Copy link
Contributor

picnixz commented Aug 25, 2024

I don't understand why we'd make assertions before calling move() - we already know the state of the test directory (self.base) because it's configured to our specification in setUp().

I shouldn't make review at 3 AM... Yes, why didn't I think about it. So yes, it's fine (I'm marking my comments as resolved). Sorry Barney for the time loss :(

@picnixz picnixz self-requested a review August 25, 2024 14:36
@barneygale
Copy link
Contributor Author

I shouldn't make review at 3 AM... Yes, why didn't I think about it. So yes, it's fine (I'm marking my comments as resolved). Sorry Barney for the time loss :(

Oh now, don't be silly! I really appreciate all your help with this PR and others, I apologise if I came off too strongly in my previous comment. I consider these sorts of discussions a great use of my time :)

@picnixz
Copy link
Contributor

picnixz commented Aug 25, 2024

I apologise if I came off too strongly in my previous comment. I consider these sorts of discussions a great use of my time :)

No your comment was definitely legitimate! I also appreciate your review and feedback on my fnmatch issues / PRs!

@picnixz picnixz self-requested a review August 25, 2024 15:47
Copy link
Contributor

@picnixz picnixz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good for me! I'll leave the discussion on clubber opened so that you can create a separate issue from the comment if you want.

@barneygale
Copy link
Contributor Author

Amazing, thanks so much! I'll get back to your fnmatch stuff shortly!

@barneygale barneygale merged commit 625d070 into python:main Aug 25, 2024
34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants