Skip to content

Disagreement between PureWindowsPath.relative_to() and ntpath.relpath() with rootless drives #99029

Description

@barneygale

On Windows, the path C: means "the current directory on the C: drive".

But pathlib's relative_to() treats it as the immediate parent of C:\. This makes sense lexographically, but it's inconsistent with everything else:

C:\Users\Barney>python>>>importntpath, pathlib>>>ntpath.relpath('C:/Windows', 'C:/Users/Barney')
'..\\..\\Windows'>>>ntpath.relpath('C:/Windows', '.')
'..\\..\\Windows'>>>ntpath.relpath('C:/Windows', 'C:')
'..\\..\\Windows'>>>pathlib.Path('C:/Windows').relative_to('C:/Users/Barney', walk_up=True)
WindowsPath('../../Windows')
>>>pathlib.Path('C:/Windows').relative_to('.', walk_up=True)
ValueError: Onepathisrelativeandtheotherisabsolute.
>>>pathlib.Path('C:/Windows').relative_to('C:')
WindowsPath('/Windows') # should be ValueError, as we're mixing absolute + relative paths

This prevents us from using relpath() from pathlib, and renders the two implementations incompatible. Booo! Also prevents us from simplifying is_relative_to() down to other == self or other in self.parents

Special cases aren't special enough to break the rules. Let's make these compatible!

Previous discussion: #84538

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions