Open
Description
Feature or enhancement
Proposal:
I propose that we add a pathlib.PurePath.segments
attribute that stores the original arguments given to the PurePath
initializer. If we did this, then:
- Users would be able to implement their own
joinpath()
/__truediv__()
-like methods without resorting toPurePath.parts
(requires normalization - slow!) or the privatePurePath._raw_paths
attribute. - In the pathlib ABCs, we can make
JoinablePath.segments
abstract, and use it from a default implementation ofJoinablePath.__str__()
.- N.B.
JoinablePath.__str__()
is currently abstract, which is weird given it's non-abstract inobject
.
- N.B.
Usage would look like:
>>> p = PurePath('/usr', 'bin/python3')
>>> p.segments
('/usr', 'bin/python3')
When a PurePath
object is given as an argument, its segments should be merged:
>>> p = PurePath('/usr', PurePath('bin', 'python3'))
>>> p.segments
('/usr', 'bin', 'python3')
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
https://discuss.python.org/t/protocol-for-virtual-filesystem-paths/82753/8
Linked PRs
Metadata
Metadata
Assignees
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
pythonGH-131916: Add `pathlib.PurePath.segments`
pathlib.PurePath.segments
#131917__str__()
should be abstract barneygale/pathlib-abc#32encukou commentedon Apr 29, 2025
Hi! Sorry for the delay.
Thinking about it more, I'm not comfortable with exposing non-normalized
segments
.It would allow access to superficial details that pathlib currently hides, with unexpected consequences. In particular, I think people would (ab)use it to distinguish between paths with and without a trailing slash.
With that, using
parts
for unknown path types seems safer. For performance you'd need to use type-specific internals.Similarly, I think a default
__str__
should look at normalized data.