You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
pathlib: Highlight pathlib operator behavior with anchored paths #87691
I ran into this while helping migrate a code base that was using "naive" concatenation of strings, so this:
PROJECT_DIR = ROOT_DIR + "/project-name"
was migrated to:
PROJECT_DIR = ROOT_DIR / "/project-name"
Note that, of course, we missed the leading "/".
Although the docs _do_ describe the behavior somewhere else, I believe it's worth being redundant in the operator section.
I believe it's a reasonable mistake to warn new users against, specially since "naive" concatenation is a common "ugly" pattern that many would be migrating from. Plus, a leading "/" is easy to miss, which would only compound the confusion if you are seeing your path "omit the (left-hand) Path object" (because the anchored string took precedence).
changed the title [-]pathlib: Highlight operator behavior with anchored paths[/-][+]pathlib: Highlight pathlib operator behavior with anchored paths[/+]on Mar 17, 2021
changed the title [-]pathlib: Highlight operator behavior with anchored paths[/-][+]pathlib: Highlight pathlib operator behavior with anchored paths[/+]on Mar 17, 2021
Activity
diegoe commentedon Mar 17, 2021
In the '/' operator documentation for
pathlib, the behavior for anchored paths is not described:https://docs.python.org/3/library/pathlib.html#operators
The behavior (prefer the second/right-hand root/anchor) is only explained in the
PurePathclass:https://docs.python.org/3/library/pathlib.html#pathlib.PurePath
I ran into this while helping migrate a code base that was using "naive" concatenation of strings, so this:
was migrated to:
Note that, of course, we missed the leading "/".
Although the docs _do_ describe the behavior somewhere else, I believe it's worth being redundant in the operator section.
I believe it's a reasonable mistake to warn new users against, specially since "naive" concatenation is a common "ugly" pattern that many would be migrating from. Plus, a leading "/" is easy to miss, which would only compound the confusion if you are seeing your path "omit the (left-hand) Path object" (because the anchored string took precedence).
[-]pathlib: Highlight operator behavior with anchored paths[/-][+]pathlib: Highlight pathlib operator behavior with anchored paths[/+][-]pathlib: Highlight operator behavior with anchored paths[/-][+]pathlib: Highlight pathlib operator behavior with anchored paths[/+]pythongh-87691: provide an absolute pathlib example under operator
gh-87691: add an absolute path pathlib example in / operator docs (GH…
pythongh-87691: add an absolute path pathlib example in / operator do…
pythongh-87691: add an absolute path pathlib example in / operator do…
gh-87691: add an absolute path pathlib example in / operator docs (GH…
gh-87691: add an absolute path pathlib example in / operator docs (GH…
pythongh-87691: clarify use of anchor and segment in pathlib docs
gh-87691: clarify use of anchor in pathlib docs (#100782)
pythongh-87691: clarify use of anchor in pathlib docs (pythonGH-100782)
pythongh-87691: clarify use of anchor in pathlib docs (pythonGH-100782)
gh-87691: clarify use of anchor in pathlib docs (GH-100782)
gh-87691: clarify use of anchor in pathlib docs (GH-100782)