-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
Description
Feature or enhancement
Add a follow_symlinks argument to pathlib.Path.is_dir(), defaulting to True
Pitch
Pathlib's walk() and glob() implementations are built upon os.scandir(), which yields os.DirEntry objects. The interface for os.DirEntry is a rough subset of pathlib.Path, including the name attribute and is_dir() method.
Pathlib only ever calls os.scandir() via a private pathlib.Path._scandir() method, currently defined as follows:
def _scandir(self):
return os.scandir(self)In future I'd like to add a pathlib.AbstractPath class with abstract stat(), iterdir() and open() methods.
The default implementation of AbstractPath._scandir() would use iterdir():
def _scandir(self):
return contextlib.nullcontext(list(self.iterdir()))Note how it returns AbstractPath objects, and not DirEntry objects. This exploits the similarities of the Path and DirEntry APIs.
... but there's a problem!
The os.DirEntry.is_dir() method accepts a keyword-only follow_symlinks argument. Our globbing implementation requires us to set this argument. But the Path.is_dir() method does not accept this argument!
If we add a keyword-only follow_symlinks argument to Path.is_dir(), we make it compatible with os.DirEntry.is_dir(), which in turn allows us to build a glob() implementation upon user-defined stat() and iterdir() methods in a future AbstractPath class.
Previous discussion
General discussion of AbstractPath: https://discuss.python.org/t/make-pathlib-extensible/3428
We added a follow_symlinks argument to Path.exists() in #89769 / #29655.