Disagreement between PureWindowsPath.relative_to() and ntpath.relpath() with rootless drives · Issue #99029 · python/cpython (original) (raw)

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

import ntpath, 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: One path is relative and the other is absolute. 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