Skip to content

with_path: preserve bare-relative URL semantics#1771

Open
HrachShah wants to merge 2 commits into
aio-libs:masterfrom
HrachShah:fix/with-path-preserve-relative
Open

with_path: preserve bare-relative URL semantics#1771
HrachShah wants to merge 2 commits into
aio-libs:masterfrom
HrachShah:fix/with-path-preserve-relative

Conversation

@HrachShah

Copy link
Copy Markdown

What do these changes do?

URL.with_path silently promoted a bare-relative URL to an absolute-path
reference when the new path did not begin with /. URL("foo/bar").with_path("abs")
returned URL("/abs"), which is a different reference (an absolute-path
reference per RFC 3986 §4.2) and resolves differently against any base URL.

The fix narrows the implicit /-prepending to the cases where it was
actually wanted:

  • URL with a netloc (http://example.com/foohttp://example.com/abs): unchanged.
  • Absolute-path URL (/foo/bar/abs): unchanged.
  • Bare-relative URL (foo/barabs): stays bare-relative.

Passing an explicit "/abs" to a bare-relative URL still produces the
slash-prefixed result ("/abs") — only the implicit-promotion case is
fixed. 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_path
now 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

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • Tests cover the edge cases I described above
  • All existing tests still pass (922 passed)

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.
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided There is a change note present in this PR label Jun 26, 2026
@codspeed-hq

codspeed-hq Bot commented Jun 26, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 97 untouched benchmarks


Comparing HrachShah:fix/with-path-preserve-relative (fde0e62) with master (315a7aa)1

Open in CodSpeed

Footnotes

  1. No successful run was found on master (b0d27e4) during the generation of this report, so 315a7aa was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@codecov

codecov Bot commented Jun 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.86%. Comparing base (315a7aa) to head (fde0e62).
⚠️ Report is 1 commits behind head on master.

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           
Flag Coverage Δ
CI-GHA 99.79% <100.00%> (+<0.01%) ⬆️
OS-Linux 99.79% <100.00%> (+<0.01%) ⬆️
OS-Windows 98.54% <100.00%> (+<0.01%) ⬆️
OS-macOS 98.72% <100.00%> (+<0.01%) ⬆️
Py-3.10 99.69% <100.00%> (+<0.01%) ⬆️
Py-3.11 99.69% <100.00%> (+<0.01%) ⬆️
Py-3.12 99.69% <100.00%> (+<0.01%) ⬆️
Py-3.13 99.74% <100.00%> (+<0.01%) ⬆️
Py-3.14 99.74% <100.00%> (+<0.01%) ⬆️
Py-3.14t 99.74% <100.00%> (+<0.01%) ⬆️
Py-pypy-3.11 99.37% <100.00%> (+<0.01%) ⬆️
VM-macos-latest 98.72% <100.00%> (+<0.01%) ⬆️
VM-ubuntu-latest 99.79% <100.00%> (+<0.01%) ⬆️
VM-windows-latest 98.54% <100.00%> (+<0.01%) ⬆️
pytest 99.79% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@coveralls

Copy link
Copy Markdown

Coverage Report for CI Build 28219831121

Coverage increased (+0.004%) to 98.744%

Details

  • Coverage increased (+0.004%) from the base build.
  • Patch coverage: 22 of 22 lines across 2 files are fully covered (100%).
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 6231
Covered Lines: 6152
Line Coverage: 98.73%
Relevant Branches: 61
Covered Branches: 61
Branch Coverage: 100.0%
Branches in Coverage %: Yes
Coverage Strength: 0.99 hits per line

💛 - Coveralls

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

Labels

bot:chronographer:provided There is a change note present in this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

with_path silently promotes a bare-relative URL to an absolute-path reference

2 participants