GH-139174: Add pathlib.Path.info.stat()#139279
GH-139174: Add pathlib.Path.info.stat()#139279barneygale wants to merge 6 commits intopython:mainfrom
pathlib.Path.info.stat()#139279Conversation
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.
encukou
left a comment
There was a problem hiding this comment.
Looks good!
I appreciate that stat() isn't the best API, but it is useful for interop with existing systems.
| 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. |
There was a problem hiding this comment.
Hmm, would it be better to call stat() on access in this case?
The platform difference looks ugly at the API level.
There was a problem hiding this comment.
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()?
There was a problem hiding this comment.
When we implement #140127, we'll be able to recommend Path.metadata(cached=False).stat()
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
Add
stat()method topathlib.Path.infothat returns a (possibly cached)os.stat_resultobject. We don't add it topathlib.types.PathInfobecause it's too specific to local filesystem paths. This requires a bit of reworking of the docs to explain!Rename
pathlib._Infotopathlib.Infoand document it, including the newstat()method. Ensure it can't be instantiated by users. Move the existing docs forpathlib.typesto a new page.pathlib.Path.info#139174📚 Documentation preview 📚: https://cpython-previews--139279.org.readthedocs.build/