Skip to content

with_path: preserve bare-relative status for netloc-less URLs (#1770)#1781

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

with_path: preserve bare-relative status for netloc-less URLs (#1770)#1781
HrachShah wants to merge 2 commits into
aio-libs:masterfrom
HrachShah:fix/with-path-preserve-bare-relative

Conversation

@HrachShah

Copy link
Copy Markdown

What do these changes do?

Preserve the bare-relative status of a URL across with_path when the new path does not start with /. The leading / is now only added for URLs that have a netloc, so URL("foo/bar").with_path("abs") returns URL("abs") instead of silently promoting the result to an absolute-path reference.

Are there changes in behavior for the user?

Yes. URLs without scheme and without authority stay bare-relative across with_path instead of being silently promoted. Callers that want the old behavior can pass an explicit /abs form, which still gets the leading slash.

Is it a substantial burden for the maintainers to support this?

No. The new branch only triggers when the caller passes a path without a leading / to a URL with no netloc, which is a narrow and previously-buggy path.

Related issue number

Fixes #1770

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt
  • Add a new news fragment into the CHANGES/ folder
Agent run details (optional, for reviewers)

Tests: python3 -m pytest tests/test_url.py -k "with_path" -p no:cacheprovider --no-cov -> 19 passed
Wider suite: python3 -m pytest tests/ yarl/ -p no:cacheprovider --no-cov -> 1457 passed, 101 skipped, 4 xfailed
Spellcheck: make doc-spelling -> my fragment passes; the 8 remaining misspellings are in pre-existing files unrelated to this change.

Drafted with Zo Bot; reviewed by HrachShah.

…bs#1770)

URL('foo/bar').with_path('abs') used to silently return URL('/abs'),
turning a bare-relative reference into an absolute-path reference.
The prepended slash changed how the URL resolved against any base,
so the bug surfaced only after the user joined the result back to a
parent.

Only prepend the leading slash when the URL has a netloc. URL
strings without a scheme and without an authority stay
bare-relative across with_path; the explicit '/abs' form still
gets the leading slash as before.

Add three regression tests in tests/test_url.py: one for the basic
shape of the bug, one for the explicit slash form, and one for the
round-trip with .join()
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided There is a change note present in this PR label Jul 1, 2026
@codspeed-hq

codspeed-hq Bot commented Jul 1, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 97 untouched benchmarks


Comparing HrachShah:fix/with-path-preserve-bare-relative (f189ef4) with master (94274c2)

Open in CodSpeed

@codecov

codecov Bot commented Jul 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.86%. Comparing base (94274c2) to head (f189ef4).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1781   +/-   ##
=======================================
  Coverage   99.86%   99.86%           
=======================================
  Files          21       21           
  Lines        4310     4327   +17     
  Branches      252      252           
=======================================
+ Hits         4304     4321   +17     
  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.

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

1 participant