Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

User subclasses of pathlib.PurePath are os.PathLike #106037

Copy link
Copy link
Closed as not planned
Closed as not planned
Copy link
@barneygale

Description

@barneygale
Issue body actions

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:

import pathlib

class TarPath(pathlib.PurePath):
    ...

readme = TarPath('README.md', tarfile=...)
with open(readme) as f:
   ...

The with open(readme) as f: line should throw a TypeError, 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12only security fixesonly security fixes3.13bugs and security fixesbugs and security fixestopic-pathlibtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.