-
- Notifications
You must be signed in to change notification settings - Fork 33.6k
Closed
Labels
OS-windowstopic-pathlibtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
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 pathsThis 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
Labels
OS-windowstopic-pathlibtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error