I propose we add a new Path.status attribute that stores an os.DirEntry object in paths yielded from Path.iterdir(), or a pathlib-specific type with a similar interface in other paths.
This would:
Allow users to access to the cached os.DirEntry after calling Path.iterdir(), which is useful for efficiently determining files' types and often doesn't involve a system call.
Allow users to switch on the type of any path without repeatedly making system calls, or having to resort to S_ISREG(st.st_mode) and other holy incantations.
In the pathlib ABCs, allows us to entirely banish PathBase.stat() and the stat_result interface, which is too low-level and local filesystem-specific
I put this feedback on the PR, but it's probably better placed here: while I like the general idea, I don't think this specific API is the right way to do it.
dir_entry potentially being None based on how the instance was created is inconvenient
the docs having to excuse dir_entry existing on PurePath objects is awkward
I think we can eliminate both of those bits of awkwardness:
define a new alternative construction method on os.DirEntry objects that allows one to be created from arbitrary os.PathLike objects
make the slot on PurePath private rather than public (presumably as _dir_entry)
define PathBase.dir_entry as a read-only property that returns the cached entry if it is already set, otherwise it uses the new constructor API to create a cached DirEntry instance for itself
If it's impractical to add os.DirEntry.from_path, then a pathlib._DirEntry class that just emulated the os.DirEntry API based on the real underlying Path object would also be fine
Activity
pythonGH-125413: Add `pathlib.Path.dir_entry` attribute
pythonGH-125413: Add `pathlib.Path.dir_entry` attribute
ncoghlan commented on Oct 23, 2024
I put this feedback on the PR, but it's probably better placed here: while I like the general idea, I don't think this specific API is the right way to do it.
I think we can eliminate both of those bits of awkwardness:
If it's impractical to add os.DirEntry.from_path, then a pathlib._DirEntry class that just emulated the os.DirEntry API based on the real underlying Path object would also be fine
pythonGH-125413: `pathlib.Path.copy()`: get common metadata keys only…
pythonGH-125413: Add `pathlib.Path.scandir()` method
GH-125413: Add `pathlib.Path.scandir()` method (#126060)
pythonGH-125413: pathlib ABCs: use `scandir()` to speed up `glob()`
barneygale commented on Nov 1, 2024
To tie up the above loose ends, we went with a Path.scandir() method in the end.
pythonGH-125413: pathlib ABCs: use `scandir()` to speed up `walk()`
pythonGH-125413: pathlib: use `scandir()` to speed up `copy()`
pythonGH-125413: pathlib: use `scandir()` to speed up `copy()`
37 remaining items
GH-125413: Add `pathlib.Path.info` attribute (#127730)
pythonGH-125413: Move `pathlib.Path.copy()` implementation alongside …
GH-125413: Move `pathlib.Path.copy()` implementation alongside `Path.…
pythonGH-125413: Add private metadata methods to `pathlib.Path.info`
Merge branch 'main' into pythongh-125413-merge-copyreader
Merge branch 'main' into pythongh-125413-merge-copyreader
Merge branch 'main' into pythongh-125413-merge-copyreader
GH-125413: Add private metadata methods to `pathlib.Path.info` (#129897)
pythonGH-125413: pathlib ABCs: use caching `path.info.exists()` when …
pythonGH-125413: Fix stale metadata from `pathlib.Path.copy()` and `m…
pythonGH-125413: Fix stale metadata from `pathlib.Path.copy()` and `m…
GH-125413: pathlib ABCs: use caching `path.info.exists()` when globbi…
GH-125413: Fix stale metadata from `pathlib.Path.copy()` and `move()` (…
GH-125413: Add private `pathlib.Path` method to write metadata (#130238)