This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: pathlib: Highlight pathlib operator behavior with anchored paths
Type: Stage: patch review
Components: Documentation Versions: Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: diegoe, docs@python
Priority: normal Keywords: patch

Created on 2021-03-17 04:04 by diegoe, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 24900 open diegoe, 2021-03-17 04:22
Messages (1)
msg388904 - (view) Author: (diegoe) * Date: 2021-03-17 04:04
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 `PurePath` class:

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:

```
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).
History
Date User Action Args
2022-04-11 14:59:42adminsetgithub: 87691
2021-03-17 04:24:30diegoesettitle: pathlib: Highlight operator behavior with anchored paths -> pathlib: Highlight pathlib operator behavior with anchored paths
2021-03-17 04:22:36diegoesetkeywords: + patch
stage: patch review
pull_requests: + pull_request23664
2021-03-17 04:04:55diegoecreate