cpython/Lib/pathlib/types.py
Barney Gale 718ab66299
GH-125413: Add pathlib.Path.info attribute (#127730)
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()`.
2025-02-08 01:16:45 +00:00

30 lines
1 KiB
Python

"""
Protocols for supporting classes in pathlib.
"""
from typing import Protocol, runtime_checkable
@runtime_checkable
class _PathParser(Protocol):
"""Protocol for path parsers, which do low-level path manipulation.
Path parsers provide a subset of the os.path API, specifically those
functions needed to provide JoinablePath functionality. Each JoinablePath
subclass references its path parser via a 'parser' class attribute.
"""
sep: str
def split(self, path: str) -> tuple[str, str]: ...
def splitext(self, path: str) -> tuple[str, str]: ...
def normcase(self, path: str) -> str: ...
@runtime_checkable
class PathInfo(Protocol):
"""Protocol for path info objects, which support querying the file type.
Methods may return cached results.
"""
def exists(self, *, follow_symlinks: bool = True) -> bool: ...
def is_dir(self, *, follow_symlinks: bool = True) -> bool: ...
def is_file(self, *, follow_symlinks: bool = True) -> bool: ...
def is_symlink(self) -> bool: ...