-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
User subclasses of pathlib.PurePath
are os.PathLike
#106037
Comments
…os.PathLike` We made it possible to subclass `pathlib.PurePath` in a68e585, which landed in 3.12. However, user subclasses automatically inherit an `__fspath__()` method, which may not be appropriate. For example, a user subclass may implement a "virtual" filesystem to provide access to a `.zip` file or FTP server. But it would be highly surprising if `open(FTPPath(...))` attempted to open a *local* file. This patch makes the `os.PathLike` interface opt-in. In pathlib itself, we opt into the `os.PathLike` interface for `PurePosixPath`, `PureWindowsPath` and `Path`. As `PurePath` is not instantiable (you always get a `PurePosixPath` or `PureWindowsPath` object back), this is backwards compatible with 3.11, but not with earlier 3.12 betas.
@brettcannon as co-author of PEP 519, your input here would be greatly appreciated! Though it's not specified as such, I argue that
|
If the question is, "can I leave |
Closing this issue - |
In Python 3.12 we've made
pathlib.PurePath
subclassable - see #31691.A significant wrinkle is that subclasses inherit an
__fspath__()
method, which can lead to unexpected behaviour when these "pure" paths are passed to filesystem APIs:The
with open(readme) as f:
line should throw aTypeError
, but instead it attempts to open a file called README.md in the current working directory (!). This sort of thing makes subclasses that implement purely virtual filesystems difficult to use safely.Linked PRs
os.PathLike
foot-shotgun inpathlib.PurePath
user subclasses #106043The text was updated successfully, but these errors were encountered: