with_path: preserve bare-relative URL semantics#1771
Open
HrachShah wants to merge 2 commits into
Open
Conversation
URL('foo/bar').with_path('abs') previously returned URL('/abs'),
silently turning a path-relative reference into an absolute-path
reference per RFC 3986 §4.2 — a different reference that resolves
differently against any base URL. The prepend-slash step now skips
when the URL has no netloc AND the existing path is also bare-relative,
so bare-relative URLs stay bare-relative. Passing an explicit '/abs'
still produces a slash-prefixed result; URLs with a netloc are
unchanged.
for more information, see https://pre-commit.ci
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1771 +/- ##
=======================================
Coverage 99.86% 99.86%
=======================================
Files 21 21
Lines 4310 4330 +20
Branches 252 252
=======================================
+ Hits 4304 4324 +20
Misses 4 4
Partials 2 2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Coverage Report for CI Build 28219831121Coverage increased (+0.004%) to 98.744%Details
Uncovered ChangesNo uncovered changes found. Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What do these changes do?
URL.with_pathsilently promoted a bare-relative URL to an absolute-pathreference when the new path did not begin with
/.URL("foo/bar").with_path("abs")returned
URL("/abs"), which is a different reference (an absolute-pathreference per RFC 3986 §4.2) and resolves differently against any base URL.
The fix narrows the implicit
/-prepending to the cases where it wasactually wanted:
http://example.com/foo→http://example.com/abs): unchanged./foo/bar→/abs): unchanged.foo/bar→abs): stays bare-relative.Passing an explicit
"/abs"to a bare-relative URL still produces theslash-prefixed result (
"/abs") — only the implicit-promotion case isfixed. Empty and dot-segment behaviour on bare-relative paths is also
preserved (no normalisation is applied, since the user explicitly chose
the path string).
Are there changes in behavior for the user?
Yes, for any caller that relied on the old silent-promotion behaviour
and then ran the result through a base-URL resolver. Most users do not
construct bare-relative URLs deliberately (aiohttp uses absolute URLs
internally), so the surface impact is small. For users who do,
with_pathnow matches the principle of least surprise: if you do not pass a
/, you do not get one back.Is it a substantial burden for the maintainers to support this?
No. One-line guard expansion plus five regression tests; no public API
addition and no change to the supported-shape inputs for any URL with
a netloc (the common case).
Related issue number
Fixes #1770
Checklist