Skip to content
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

GH-125413: Add pathlib.Path.info attribute #127730

Open
wants to merge 34 commits into
base: main
Choose a base branch
from

Conversation

barneygale
Copy link
Contributor

@barneygale barneygale commented Dec 7, 2024

Add pathlib.Path.info attribute, which stores an object implementing the pathlib.types.PathInfo protocol (also new). The object supports querying the file type and internally caching os.stat() results. Path objects generated by Path.iterdir() are initialised with status information from os.DirEntry objects, which is gleaned from scanning the parent directory.

The PathInfo protocol has four methods: exists(), is_dir(), is_file() and is_symlink().

When a path object is generated by `PathBase.iterdir()`, then its `_info`
attribute now stores a `os.DirEntry`-like object that can be used to query
the file type. This removes any need for a `_scandir()` method.

Currently the `_info` attribute is private and only guaranteed to be
populated in paths from `iterdir()`. Later on, I'm hoping to rename it to
`info` and ensure that it's populated for all kinds of paths (this probably
involves adding a `pathlib.FileInfo` class.) In the pathlib ABCs, `info`
will replace `stat()` as the lowest-level abstract file status querying
mechanism.
@barneygale
Copy link
Contributor Author

Steve - hopefully this helps address the issues you raised in #127377, but perhaps I'm only moving the problem around here.

@zooba
Copy link
Member

zooba commented Dec 9, 2024

I think this is just moving stuff around. It might end up being the right places, but let's see the Discourse discussion play out a bit first.

@barneygale barneygale changed the title GH-125413: pathlib ABCs: replace _scandir() with _info GH-125413: pathlib ABCs: replace _scandir() with _status Dec 9, 2024
@barneygale barneygale changed the title GH-125413: pathlib ABCs: replace _scandir() with _status GH-125413: Add pathlib.Path.status attribute Dec 9, 2024
@barneygale barneygale marked this pull request as draft December 9, 2024 20:58
@barneygale
Copy link
Contributor Author

I've revised the PR to be a more complete implementation of what I described on the forum, mostly as a demonstration/proof-of-concept at this stage.

@barneygale barneygale added the type-feature A feature request or enhancement label Dec 9, 2024
@barneygale barneygale marked this pull request as ready for review December 12, 2024 00:46
@barneygale barneygale requested a review from encukou December 12, 2024 00:46
@barneygale
Copy link
Contributor Author

Hi Petr, I've requested your review on this PR, but at this stage what I most need is a sense-check: does it take the right approach and avoid the pitfalls you outlined in this forum thread? Thank you.

@encukou
Copy link
Member

encukou commented Dec 13, 2024

Sorry for getting to this late; I think we moved on in the Discourse thread instead :)

@barneygale
Copy link
Contributor Author

This is up-to-date with my latest proposal on Discourse in fact!

Actually you mentioned making this a method rather than a property, which I haven't done here. I'll bring it up on the forum.

@barneygale barneygale requested a review from ncoghlan December 17, 2024 09:57
@barneygale
Copy link
Contributor Author

Right, I think this is ready for review. Alyssa, sorry if this seems like deja-vu!

@barneygale barneygale changed the title GH-125413: Add pathlib.Path.status attribute GH-125413: Add pathlib.Path.info attribute Dec 21, 2024
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.

3 participants