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

Skip to content

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

Closed
wants to merge 1 commit into from

Conversation

diegoe
Copy link
Contributor

@diegoe diegoe commented Mar 17, 2021

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

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')
Copy link
Contributor Author

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
Copy link
Contributor Author

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):

@barneygale
Copy link
Contributor

This behaviour is already explained a few paragraphs before.

@diegoe
Copy link
Contributor Author

diegoe commented Apr 13, 2021

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 filepath "ignoring" root when composing the path with the / operator.

I went to read the docs on the / operator and didn't see the absolute path caveat mentioned, so I thought it might be worth repeating.

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.

Copy link
Contributor

@hauntsaninja hauntsaninja left a 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants