Skip to content

pathlib __truediv__ returns the "self" type #553

@dckc

Description

@dckc

The pathlib implementation seems to go to some trouble to preserve subtypes in children. This works fine:

import pathlib


class MyPath(pathlib.PosixPath):
    def parity(self):
        return len(str(self)) % 2

root = MyPath('/')
print(root.parity())
child = root / 'there'
print(child.parity())
$ python3 t.py 
1
0

But this declaration isn't parameterized:

    def __truediv__(self, key: Union[str, PurePath]) -> PurePath: ...

so the program above doesn't typecheck:

$ mypy t.py 
t.py:11: error: "PurePath" has no attribute "parity"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions