@@ -66,6 +66,10 @@ New syntax features:
6666
6767* PEP 498: :ref: `Formatted string literals <whatsnew-fstrings >`
6868
69+ Standard library improvements:
70+
71+ * PEP 519: :ref: `Adding a file system path protocol <pep-519 >`
72+
6973Windows improvements:
7074
7175* The ``py.exe `` launcher, when used interactively, no longer prefers
@@ -92,6 +96,69 @@ Windows improvements:
9296New Features
9397============
9498
99+ .. _pep-519 :
100+
101+ PEP 519: Adding a file system path protocol
102+ ===========================================
103+
104+ File system paths have historically been represented as :class: `str `
105+ or :class: `bytes ` objects. This has led to people who write code which
106+ operate on file system paths to assume that such objects are only one
107+ of those two types (an :class: `int ` representing a file descriptor
108+ does not count as that is not a file path). Unfortunately that
109+ assumption prevents alternative object representations of file system
110+ paths like :mod: `pathlib ` from working with pre-existing code,
111+ including Python's standard library.
112+
113+ To fix this situation, a new interface represented by
114+ :class: `os.PathLike ` has been defined. By implementing the
115+ :meth: `~os.PathLike.__fspath__ ` method, an object signals that it
116+ represents a path. An object can then provide a low-level
117+ representation of a file system path as a :class: `str ` or
118+ :class: `bytes ` object. This means an object is considered
119+ :term: `path-like <path-like object> ` if it implements
120+ :class: `os.PathLike ` or is a :class: `str ` or :class: `bytes ` object
121+ which represents a file system path. Code can use :func: `os.fspath `,
122+ :func: `os.fsdecode `, or :func: `os.fsencode ` to explicitly get a
123+ :class: `str ` and/or :class: `bytes ` representation of a path-like
124+ object.
125+
126+ The built-in :func: `open ` function has been updated to accept
127+ :class: `os.PathLike ` objects as have all relevant functions in the
128+ :mod: `os ` and :mod: `os.path ` modules. The :class: `os.DirEntry ` class
129+ and relevant classes in :mod: `pathlib ` have also been updated to
130+ implement :class: `os.PathLike `. The hope is that updating the
131+ fundamental functions for operating on file system paths will lead
132+ to third-party code to implicitly support all
133+ :term: `path-like objects <path-like object> ` without any code changes
134+ or at least very minimal ones (e.g. calling :func: `os.fspath ` at the
135+ beginning of code before operating on a path-like object).
136+
137+ Here are some examples of how the new interface allows for
138+ :class: `pathlib.Path ` to be used more easily and transparently with
139+ pre-existing code::
140+
141+ >>> import pathlib
142+ >>> with open(pathlib.Path("README")) as f:
143+ ... contents = f.read()
144+ ...
145+ >>> import os.path
146+ >>> os.path.splitext(pathlib.Path("some_file.txt"))
147+ ('some_file', '.txt')
148+ >>> os.path.join("/a/b", pathlib.Path("c"))
149+ '/a/b/c'
150+ >>> import os
151+ >>> os.fspath(pathlib.Path("some_file.txt"))
152+ 'some_file.txt'
153+
154+ (Implemented by Brett Cannon, Ethan Furman, Dusty Phillips, and Jelle Zijlstra.)
155+
156+ .. seealso ::
157+
158+ :pep: `519 ` - Adding a file system path protocol
159+ PEP written by Brett Cannon and Koos Zevenhoven.
160+
161+
95162.. _whatsnew-fstrings :
96163
97164PEP 498: Formatted string literals
0 commit comments