Skip to content
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

Support using the subtraction operator to get the relative path between URLs #1340

Merged
merged 27 commits into from
Oct 22, 2024

Conversation

oleksbabieiev
Copy link
Contributor

What do these changes do?

Support for using the subtraction operator to calculate the difference between the paths of two URLs. A new URL with the relative path is returned to the user.

Usage:

from yarl import URL

target = URL("http://example.com/path/index.html")
base = URL("http://example.com/path/")

rel = target - base

print(rel)  # output: "index.html"

Are there changes in behavior for the user?

Now the user can easily calculate the relative path between two URLs!

Related issue number

Resolves #1183

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes

@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Oct 20, 2024
@oleksbabieiev oleksbabieiev marked this pull request as ready for review October 20, 2024 15:27
@bdraco
Copy link
Member

bdraco commented Oct 20, 2024

Looks like the tests aren't passing in the CI

@bdraco bdraco marked this pull request as draft October 20, 2024 21:15
yarl/_url.py Fixed Show fixed Hide fixed
yarl/_url.py Outdated Show resolved Hide resolved
@bdraco
Copy link
Member

bdraco commented Oct 21, 2024

Sorry about the conflicts. I was doing a bit of housekeeping to better organize internals. I think I've worked them out correctly.

@oleksbabieiev
Copy link
Contributor Author

oleksbabieiev commented Oct 21, 2024

I have also noticed that when using os.path.relpath() with absolute and relative paths (e.g. dir and /), the function first gets the current working directory (essentially doing os.getcwd()). Otherwise, it is obviously impossible to calculate the path between the given directories. This behavior is undesirable, so I have banned it.

yarl/_path.py Outdated Show resolved Hide resolved
yarl/_path.py Outdated Show resolved Hide resolved
tests/test_url.py Outdated Show resolved Hide resolved
@bdraco
Copy link
Member

bdraco commented Oct 22, 2024

The windows wheel build is failing the tests

You probably want to use PosixPurePath instead so windows doesn’t try to use windows path separators

Copy link

codspeed-hq bot commented Oct 22, 2024

CodSpeed Performance Report

Merging #1340 will not alter performance

Comparing oleksbabieiev:relpath (d683e16) with master (f029a3c)

Summary

✅ 82 untouched benchmarks

Copy link

codecov bot commented Oct 22, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.02%. Comparing base (f029a3c) to head (d683e16).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1340      +/-   ##
==========================================
+ Coverage   95.97%   96.02%   +0.04%     
==========================================
  Files          31       31              
  Lines        5645     5707      +62     
  Branches      349      355       +6     
==========================================
+ Hits         5418     5480      +62     
  Misses        201      201              
  Partials       26       26              
Flag Coverage Δ
CI-GHA 96.02% <100.00%> (+0.04%) ⬆️
MyPy 45.73% <73.01%> (+0.31%) ⬆️
OS-Linux 99.55% <100.00%> (+<0.01%) ⬆️
OS-Windows 99.61% <100.00%> (+<0.01%) ⬆️
OS-macOS 99.29% <100.00%> (+<0.01%) ⬆️
Py-3.10.11 99.27% <100.00%> (+<0.01%) ⬆️
Py-3.10.15 99.50% <100.00%> (+<0.01%) ⬆️
Py-3.11.10 99.50% <100.00%> (+<0.01%) ⬆️
Py-3.11.9 99.27% <100.00%> (+<0.01%) ⬆️
Py-3.12.7 99.50% <100.00%> (+<0.01%) ⬆️
Py-3.13.0 99.50% <100.00%> (+<0.01%) ⬆️
Py-3.9.13 99.23% <100.00%> (+<0.01%) ⬆️
Py-3.9.20 99.46% <100.00%> (+<0.01%) ⬆️
Py-pypy7.3.16 99.52% <100.00%> (+<0.01%) ⬆️
Py-pypy7.3.17 99.54% <100.00%> (+<0.01%) ⬆️
VM-macos-latest 99.29% <100.00%> (+<0.01%) ⬆️
VM-ubuntu-latest 99.55% <100.00%> (+<0.01%) ⬆️
VM-windows-latest 99.61% <100.00%> (+<0.01%) ⬆️
pytest 99.55% <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 Sentry.
📢 Have feedback on the report? Share it here.

@oleksbabieiev oleksbabieiev marked this pull request as ready for review October 22, 2024 19:23
yarl/_path.py Outdated Show resolved Hide resolved
Co-authored-by: J. Nick Koston <[email protected]>
yarl/_url.py Show resolved Hide resolved
Copy link
Member

@bdraco bdraco left a comment

Choose a reason for hiding this comment

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

@bdraco bdraco merged commit ff455c4 into aio-libs:master Oct 22, 2024
44 of 46 checks passed
CHANGES/1340.feature.rst Show resolved Hide resolved
docs/api.rst Show resolved Hide resolved
tests/test_url.py Show resolved Hide resolved
tests/test_url.py Show resolved Hide resolved
yarl/_path.py Show resolved Hide resolved
docs/api.rst Show resolved Hide resolved
@bdraco
Copy link
Member

bdraco commented Oct 22, 2024

@oleksbabieiev Would you please address the additional comments in a followup PR?

@oleksbabieiev
Copy link
Contributor Author

@bdraco yes, but I won't be able to do it until tomorrow. Sorry for the inconvenience

@bdraco
Copy link
Member

bdraco commented Oct 22, 2024

no worries. thank you!

opened #1377 so I don't loose track

@bdraco
Copy link
Member

bdraco commented Oct 25, 2024

I looks like I jumped the gun and merged this too soon. It needs a little more work before its ready. I opened #1391 to revert it and the work I did on top of it to try to get it into a good state. Sorry I didn't realize it sooner.

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.

Add Support for Relative Path Calculation
3 participants