cpython icon indicating copy to clipboard operation
cpython copied to clipboard

gh-119993 ignore `NotADirectoryError` in `Path.unlink()` if `missing_ok` is `True`

Open MusicalNinjaDad opened this issue 1 year ago • 1 comments

Add a short note on when Path.unlink() raises NotADirectoryError

This can occur "in the wild", for example, after calling shutil.move(src, dst) where dst is a non-existent directory. Debugging can cost a lot of time as the cause can be non-intuitive, potentially not nearby in the code and the docs don't mention the exception as being raised in any circumstances. (This just happened to me after encountering pypa/cibuildwheel#1850)

Fixes: #119993

  • Issue: gh-119993

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

MusicalNinjaDad avatar Jun 04 '24 15:06 MusicalNinjaDad

@barneygale - this is how unlink would/could work if it were to ignore NotADirectoryError. Semantically that makes success with missing_ok = True mean "The file is not there afterwards" and with missing_ok = False to mean "The file was deleted". ~~I think this makes sense, but I have no strong feelings that it must be like this.~~

Windows works differently from Linux/MacOS and raises FileNotFoundError if an intermediate path refers to a file. Without this change the same end-user code will potentially raise on one OS and not on another for the same situation.

MusicalNinjaDad avatar Jul 04 '24 12:07 MusicalNinjaDad

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

And if you don't make the requested changes, you will be put in the comfy chair!

bedevere-app[bot] avatar Aug 10 '24 18:08 bedevere-app[bot]

@barneygale - I was unexpectedly offline for the last 6 months due to illness. I've just pushed the changes you requested and will now start to merge into the lasted main

~~Looking back through my code from the summer: Did I understand your request correctly to integrate the test into test_pathlib_abc.py? I ended up completely mocking the functionality in DummyPath making that test a no-op. The test is also still present in test_pathlib.py - so no value from inheritance if I understand correctly...~~

I see the ABC no longer includes unlink() as of #127736 - so leaving previous comments as resolved and taking over the new version of test_pathlib_abc.py

MusicalNinjaDad avatar Jan 24 '25 08:01 MusicalNinjaDad

I have made the requested changes; please review again

MusicalNinjaDad avatar Jan 24 '25 10:01 MusicalNinjaDad

Thanks for making the requested changes!

@barneygale: please review the changes made to this pull request.

bedevere-app[bot] avatar Jan 24 '25 10:01 bedevere-app[bot]

@barneygale - is there anything I can do to support with this? Should I rebase and reintegrate with all the work you've done on the ABCs?

MusicalNinjaDad avatar Jun 05 '25 17:06 MusicalNinjaDad