Skip to content

relpath: let os.path.relpath detect different drives on Windows - #382

Merged
skshetry merged 1 commit into
mainfrom
relpath-windows-drive
Nov 6, 2026
Merged

skshetry merged 1 commit into
mainfrom
relpath-windows-drive

Conversation

@skshetry

@skshetry skshetry commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator

Follow-up to #375. The os.path.commonpath() guard in relpath() raises ValueError not only for paths on different drives but also when one path has a root and the other does not. A UNC share root without a trailing separator (\\srv\sh) has no root, so relpath(r"\\srv\sh\x", r"\\srv\sh") returned the absolute path instead of x.

os.path.relpath() on Windows raises ValueError only when the drives differ (or for an empty path, which abspath rules out), so call it directly and fall back to path on ValueError. Non-Windows behaviour is unchanged.

Behaviour on Windows (checked with ntpath):

path, start before #375 (commonprefix) #375 (commonpath) this PR
D:\x, C:\a path path path
C:\a\b, C:\a b b b
c:\a\b, C:\a path (wrong) b b
\\srv1\sh\x, \\srv2\sh ValueError path path
\\srv\sh\x, \\srv\sh x path (wrong) x

Not verified on a real Windows share; relpath has no tests in this repo.

🤖 Generated with Claude Code

The os.path.commonpath() guard added in a80305b also raises ValueError
when one path has a root and the other does not, as with a UNC share
root without a trailing separator ("\\srv\sh"). relpath() then returned
the absolute path instead of a relative one. os.path.relpath() raises
ValueError on Windows only when the drives differ (or the path is
empty, which abspath rules out), so catch that instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-project-automation github-project-automation Bot moved this to Backlog in DVC Nov 6, 2026
@skshetry
skshetry merged commit 3ffa731 into main Nov 6, 2026
24 checks passed
@skshetry
skshetry deleted the relpath-windows-drive branch November 6, 2026 08:32
@github-project-automation github-project-automation Bot moved this from Backlog to Done in DVC Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant