Make pathlib extensible

:sparkles: July 2023 progress report :sparkles:

Thank you to all those who have been providing feedback on naming, hierarchies, etc. It’s so useful to bounce ideas off such talented and experienced devs!

As I mentioned in a previous post, I’ve put up a PR that adds a private _PathBase class:

That PR has been slimmed down: it originally added tarfile.TarPath too, but the expected behaviour of paths involving symlinks wasn’t clear, and so I’m going to work on TarPath in a PyPI package first.

When that PR lands, the remaining work is:

  • Add a public PurePath.pathmod class attribute (PR: GH-106533)
  • Figure out what to do with _PathBase.__hash__(), __eq__(), __lt__(), etc (any opinions?)
  • Make pathlib.PathBase public!

For the first time, I feel confident that this project will succeed. There are no architectural problems remaining in pathlib that would prevent it, nor any major decisions to be made (touch wood). It will be immediately useful upon release, and I think it could grow into one of Python’s best-loved features as third-party APIs begin to accept os.PathLike | pathlib.PathBase for path arguments. Eventually users should be able to do things like:

shutil.copytree(FTPPath(...), TarPath(...))
pandas.read_csv(S3Path(...))
image.save(TarPath(...))  # PIL

We’re doing for path objects what PEP 3116 and the io module did for file objects :slight_smile:

That’s it for now. Thanks again to everyone who has helped with this!

8 Likes