Skip to content

Adjust path_to_url et al. to produce the same results on Python 3.14+ #13423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

hroncok
Copy link
Contributor

@hroncok hroncok commented Jun 12, 2025

See python/cpython#125974 and #13138 (comment)

This makes the tests pass on Fedora with Python 3.14.0b2 (except test_get_index_content_directory_append_index, which segfaults python/cpython#135448)

return urllib.request.pathname2url(urllib.request.url2pathname(part))
ret = urllib.request.pathname2url(urllib.request.url2pathname(part))
if sys.version_info >= (3, 14):
ret = ret.removeprefix("//")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not proud of this :(

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It wasn't obvious to me how else to do this either 🙁

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there something in cpython we can reference here so people reading this later knows what’s going on?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can try digging a reference to the change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, if we could find something that explained why cpython changed what they did, and why the new result is "correct" and why pip needs something different from the "correct" answer, that would be best (as it would give us a much better basis for informed decisions if this code ever needs to change again).

Unfortunately, I get the impression that there's no real "correct" answer here, and the cpython change was "because it's more consistent with (something or other that pip maybe doesn't even care about)". If so, then documenting what precisely pip is using to base its idea of what "the url for a pathname" is, would be better than nothing.

Worst case scenario would be that there's simply no standard for how to convert a pathname to a URL in general, and it's all just a mess of guesswork and hacks 🙁

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to this comment it is this is a result of CPython's attempt to follow RFC 3986, Section 5.2.2: python/cpython#125974 (comment)

I beleive python/cpython#125989 and python/cpython#123273 are the related PRs.

Previous discussions on the "standards" around path to url: #13062 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't get to this today. If you want, feel free to adjust this PR with some comments.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My main point here is that as a pip maintainer, the removeprefix('//') line looks like complete magic - there's no clear reason why it's needed, why it's 3.14+ only, or why urllib.request isn't doing the right thing for us in the first place. And I'm not particularly comfortable with a comment that essentially just says that we're compensating for a change in CPython, because that begs the question why are we having to effectively revert the CPython change, rather than raising this as a bug on the stdlib function. I'd like to see something that explains why it isn't a CPython bug, and why in that case we need to alter the behaviour.

Having said all of this, I don't think it's fair to expect @hroncok to do all that research and analysis, so if we have to go with a comment that says "CPython changed behaviour in a way that broke our code, so we're patching the result back to what we expect" then I'll live with it.

@barneygale as the author of #13062 (comment) can you shed any light on this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if having a custom wrapper around just urllib.request.pathname2url wouldn't be clearer. Call it e.g. path2url_fragment and describe what it should return and that it cannot start with //.

@notatallshaw
Copy link
Member

With no other feedback, are you planning to remove draft status from this and add a small news item?

I tested this locally and it appears to be good, but because it's slightly changing the behavior of brittle path functions I think there should be a small bug fix note.

@hroncok
Copy link
Contributor Author

hroncok commented Jun 22, 2025

Can do.

@hroncok
Copy link
Contributor Author

hroncok commented Jun 30, 2025

@hroncok hroncok marked this pull request as ready for review June 30, 2025 09:19
@hroncok
Copy link
Contributor Author

hroncok commented Jun 30, 2025

Anyway. I added a news entry and a link to the upstream discussion to a comment. Marked as non-draft. That's about as much as I have the capacity to do.

@ichard26 ichard26 added this to the 25.2 milestone Jul 5, 2025
@ichard26
Copy link
Member

ichard26 commented Jul 5, 2025

With my RM hat on, barring any major issues (I still need to review this), I plan on including this in the upcoming 25.2 release regardless of whether CPython replies or not. I'd like to include it in the release so downstream can use/package pip for the Python 3.14 betas.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants