Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Closed as not planned
Closed as not planned
@barneygale

Description

@barneygale

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 fixes3.13bugs and security fixestopic-pathlibtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions