Skip to content

Conversation

barneygale
Copy link
Contributor

@barneygale barneygale commented Sep 24, 2025

Add stat() method to pathlib.Path.info that returns a (possibly cached) os.stat_result object. We don't add it to pathlib.types.PathInfo because it's too specific to local filesystem paths. This requires a bit of reworking of the docs to explain!

Rename pathlib._Info to pathlib.Info and document it, including the new stat() method. Ensure it can't be instantiated by users. Move the existing docs for pathlib.types to a new page.


📚 Documentation preview 📚: https://cpython-previews--139279.org.readthedocs.build/

Add `stat()` method to `pathlib.Path.info` that returns a (possibly cached)
`os.stat_result` object. We don't add it to `pathlib.types.PathInfo`
because it's too specific to local filesystem paths. This requires a bit of
reworking of the docs to explain!

Rename `pathlib._Info` to `pathlib.Info` and document it, including the new
`stat()` method. Ensure it can't be instantiated by users. Move the
existing docs for `pathlib.types` to a new page.
@barneygale barneygale marked this pull request as ready for review September 24, 2025 03:03
@barneygale barneygale requested a review from encukou September 24, 2025 03:03
Copy link
Member

@encukou encukou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

I appreciate that stat() isn't the best API, but it is useful for interop with existing systems.

@barneygale barneygale requested a review from encukou October 10, 2025 17:38
Comment on lines +1788 to +1791
On Windows, if the path was generated by scanning a directory, then the
``st_ino``, ``st_dev`` and ``st_nlink`` attributes of the
:class:`~os.stat_result` are always set to zero. Call :meth:`Path.stat`
to get these attributes.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, would it be better to call stat() on access in this case?
The platform difference looks ugly at the API level.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

os.DirEntry.stat() has the same shortcoming. Would it be any better if we recommended os.stat() instead of, or in addition to, Path.stat()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we implement #140127, we'll be able to recommend Path.metadata(cached=False).stat()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even there it would be great if Path.metadata(cached=True).stat() returned a complete stat object.

IMO, os.DirEntry is an implementation detail that pathlib should hide.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, will do. This will require merging the _StatResultInfo and _DirEntryInfo classes together I think. I've opened a new PR to do that: #140155

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants