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.
Linked PRs
Feature or enhancement
Add a follow_symlinks argument to
pathlib.Path.is_dir(), defaulting toTruePitch
Pathlib's
walk()andglob()implementations are built uponos.scandir(), which yieldsos.DirEntryobjects. The interface foros.DirEntryis a rough subset ofpathlib.Path, including thenameattribute andis_dir()method.Pathlib only ever calls
os.scandir()via a privatepathlib.Path._scandir()method, currently defined as follows:In future I'd like to add a
pathlib.AbstractPathclass with abstractstat(),iterdir()andopen()methods.The default implementation of
AbstractPath._scandir()would useiterdir():Note how it returns
AbstractPathobjects, and notDirEntryobjects. This exploits the similarities of thePathandDirEntryAPIs.... but there's a problem!
The
os.DirEntry.is_dir()method accepts a keyword-onlyfollow_symlinksargument. Our globbing implementation requires us to set this argument. But thePath.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 withos.DirEntry.is_dir(), which in turn allows us to build aglob()implementation upon user-definedstat()anditerdir()methods in a futureAbstractPathclass.Previous discussion
General discussion of
AbstractPath: https://discuss.python.org/t/make-pathlib-extensible/3428We added a follow_symlinks argument to
Path.exists()in #89769 / #29655.Linked PRs
pathlib.Path.is_dir()andis_file()#105794