Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32.4k
Description
Feature or enhancement
Add afollow_symlinks argument topathlib.Path.is_dir()
, defaulting toTrue
Pitch
Pathlib'swalk()
andglob()
implementations are built uponos.scandir()
, which yieldsos.DirEntry
objects. The interface foros.DirEntry
is a rough subset ofpathlib.Path
, including thename
attribute andis_dir()
method.
Pathlib only ever callsos.scandir()
via a privatepathlib.Path._scandir()
method, currently defined as follows:
def_scandir(self):returnos.scandir(self)
In future I'd like to add apathlib.AbstractPath
class with abstractstat()
,iterdir()
andopen()
methods.
The default implementation ofAbstractPath._scandir()
would useiterdir()
:
def_scandir(self):returncontextlib.nullcontext(list(self.iterdir()))
Note how it returnsAbstractPath
objects, and notDirEntry
objects. This exploits the similarities of thePath
andDirEntry
APIs.
... but there's a problem!
Theos.DirEntry.is_dir()
method accepts a keyword-onlyfollow_symlinks
argument. Our globbing implementation requires us to set this argument. But thePath.is_dir()
method does not accept this argument!
If we add a keyword-onlyfollow_symlinks argument toPath.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 futureAbstractPath
class.
Previous discussion
General discussion ofAbstractPath
:https://discuss.python.org/t/make-pathlib-extensible/3428
We added afollow_symlinks argument toPath.exists()
in#89769 /#29655.