Replace deprecated os.path.commonprefix with os.path.commonpath#13847
Replace deprecated os.path.commonprefix with os.path.commonpath#13847
os.path.commonprefix with os.path.commonpath#13847Conversation
ichard26
left a comment
There was a problem hiding this comment.
Looks fine, although I have one question about the change in auth.py.
| key=lambda candidate: len( | ||
| commonpath( | ||
| [ | ||
| parsed_url.path, | ||
| candidate.path, | ||
| ] | ||
| ) | ||
| ), |
There was a problem hiding this comment.
This doesn't seem exactly behaviour equivalent to the old logic? Although I'm not sure what the old logic is supposed to do either. Pick the least deep common prefixing path?
There was a problem hiding this comment.
It's mostly the same, and the case it's different doesn't matter.
Each of these lambdas is returning an integer to show how close each of the candidate URLs are to the input one, then using the integer to sort the list and pick the first one as the closest match.
-
Before,
commonprefixmatched by character. So given something like "/path/path2/" and "/path/path3/", it'd give "/path/path". Thenrfind("/")gives us the index of that last "/", or 5. -
After,
commonpathmatches by full path parts. With the same inputs, we get "/path".lenof that is also 5.
There is one slight difference, but it doesn't matter, and it's for the case when there isn't really any match.
-
If we have two paths like "/path/path2/" and "/path4/path3/", before
commonprefixwould give "/path", and `rfind("/") would give the index of the only "/", or 0. -
Afterwards,
commonpathgives us "/", whoselenis 1.
That's okay, because in both cases, the shortest value we get for a meaningful common match is 2. For example, with "/p/path2/" and "/p/path3/":
-
Before:
commonprefix(["/p/path2/", "/p/path3/"])-> "/p/path" ->rfind("/")-> 2 -
After:
commonpath(["/p/path2/", "/p/path3/"])-> "/p" ->len-> 2
There was a problem hiding this comment.
Ah, I see. I did misread the original code, so that didn't help. Thanks!
Follow on from #13777.
https://docs.python.org/3.15/whatsnew/3.15.html#pending-removal-in-future-versions
There's a few uses in vendored pygments and pyproject_hooks, which are fixed and pending release: