-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
bpo-43525: Highlight pathlib operator behavior with anchored paths #24900
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
Conversation
Add a warning describing the behavior of Path objects when combining a Path object with an anchored-path string, using the `/` operator.
>>> p / '/bin' | ||
PurePosixPath('/bin') | ||
>>> p / '/bin' / '/python' | ||
PurePosixPath('/python') |
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.
Is this ok? I didn't want to use python3
to not make the example susceptible to bit-rot 😆
I couldn't think of another executable. Maybe a different path would be a better example?
Maybe I'm overthinking it! 😆 🤣
@@ -217,6 +217,16 @@ The slash operator helps create child paths, similarly to :func:`os.path.join`:: | |||
>>> '/usr' / q | |||
PurePosixPath('/usr/bin') | |||
|
|||
Note that a string containing a leading '`/`' character will be considered | |||
an absolute path, making the last to be taken as the anchor for the new |
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.
"last"? "last one"? The phrasing is copied from pathlib.PurePath
:
When several absolute paths are given, the last is taken as an anchor (mimicking os.path.join()’s behaviour):
This behaviour is already explained a few paragraphs before. |
The way I ran into this caveat was that I replaced naive string concatenations on an old codebase, and changed stuff like this: filepath = ROOT_DIR + "/some_file.txt" Into: root = Path(...)
filepath = root / "/some_file.txt" I missed the leading slash on a few updates, and got the confusing result of I went to read the docs on the Perhaps my suggestion is too verbose? I think it would be good enough without the example. Feel free to close if you don't think the change is worth it at all. |
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.
Thanks for the PR! I agree that this is too verbose, given that it's been explained a couple paragraphs above.
I'm still sympathetic to the fact that this can be surprising. I opened a different PR that just adds an extra example over here: #100737
Add a warning describing the behavior of Path objects when combining a
Path object with an anchored-path string, using the
/
operator.https://bugs.python.org/issue43525