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

Rework of the URL subtraction feature #1392

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

oleksbabieiev
Copy link
Contributor

@oleksbabieiev oleksbabieiev commented Oct 25, 2024

What do these changes do?

Rework of the URL subtraction feature (added in #1340; removed in #1391)

Are there changes in behavior for the user?

Being able to calculate the relative path between two URLs using the subtraction syntax:

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"

Related issue number

Resolves #1183

Checklist

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

Known issues

@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Oct 25, 2024
tests/test_url_benchmarks.py Dismissed Show dismissed Hide dismissed
Copy link

codspeed-hq bot commented Oct 25, 2024

CodSpeed Performance Report

Merging #1392 will not alter performance

Comparing oleksbabieiev:subtraction (2341ece) with master (8ce8d67)

Summary

✅ 83 untouched benchmarks

🆕 2 new benchmarks

Benchmarks breakdown

Benchmark master oleksbabieiev:subtraction Change
🆕 test_url_subtract N/A 13.6 ms N/A
🆕 test_url_subtract_long_urls N/A 157.6 ms N/A

Copy link

codecov bot commented Oct 25, 2024

Codecov Report

Attention: Patch coverage is 99.01961% with 1 line in your changes missing coverage. Please review.

Project coverage is 96.10%. Comparing base (8ce8d67) to head (2341ece).

Files with missing lines Patch % Lines
tests/test_url.py 98.30% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1392      +/-   ##
==========================================
+ Coverage   96.05%   96.10%   +0.05%     
==========================================
  Files          31       31              
  Lines        5751     5852     +101     
  Branches      342      350       +8     
==========================================
+ Hits         5524     5624     +100     
- Misses        201      202       +1     
  Partials       26       26              
Flag Coverage Δ
CI-GHA 96.10% <99.01%> (+0.05%) ⬆️
MyPy 49.47% <82.35%> (+0.60%) ⬆️
OS-Linux 99.56% <100.00%> (+<0.01%) ⬆️
OS-Windows 99.62% <100.00%> (+<0.01%) ⬆️
OS-macOS 99.30% <100.00%> (+<0.01%) ⬆️
Py-3.10.11 99.28% <100.00%> (+<0.01%) ⬆️
Py-3.10.15 99.51% <100.00%> (+<0.01%) ⬆️
Py-3.11.10 99.51% <100.00%> (+<0.01%) ⬆️
Py-3.11.9 99.28% <100.00%> (+<0.01%) ⬆️
Py-3.12.7 99.51% <100.00%> (+<0.01%) ⬆️
Py-3.13.0 99.51% <100.00%> (+<0.01%) ⬆️
Py-3.9.13 99.24% <100.00%> (+<0.01%) ⬆️
Py-3.9.20 99.47% <100.00%> (+<0.01%) ⬆️
Py-pypy7.3.16 99.53% <100.00%> (+<0.01%) ⬆️
Py-pypy7.3.17 99.55% <100.00%> (+<0.01%) ⬆️
VM-macos-latest 99.30% <100.00%> (+<0.01%) ⬆️
VM-ubuntu-latest 99.56% <100.00%> (+<0.01%) ⬆️
VM-windows-latest 99.62% <100.00%> (+<0.01%) ⬆️
pytest 99.56% <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.

@bdraco
Copy link
Member

bdraco commented Oct 28, 2024

I'm going to do a 1.17.0 release so we can start preparing aiohttp 3.11.x and I want do to some more downstream cleanups. We can do a 1.18.0 for this once its ready

yarl/_url.py Outdated Show resolved Hide resolved
@bdraco
Copy link
Member

bdraco commented Oct 30, 2024

Some conflicts happened. I think I've resolved them correctly


>>> target = URL('http://example.com/path/index.html')
>>> base = URL('http://example.com/')
>>> target - base
Copy link
Member

Choose a reason for hiding this comment

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

Could you also document what happens when you do base - target?

assert result_url == expected_url


@pytest.mark.xfail(reason="Empty segments are not preserved")
Copy link
Member

Choose a reason for hiding this comment

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

What does need to be done to fix this? It'd be useful to include this information into the reason so that it's obvious when it can be removed.

Copy link
Contributor

Choose a reason for hiding this comment

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

this is bugs to address - c.f. #1388 (comment)

("path/to", "spam/", "../path/to"),
("path/../to", "path/", "../to"),
("path/..", ".", "path/.."),
("path/../replace/me", "path/../replace", "replace/me"),
Copy link
Contributor

Choose a reason for hiding this comment

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

How is this different to the one below?
("path/../replace/me", "path/../replace/", "me"),

("http://example.com", "http://example.com/", "."),
("//example.com", "//example.com", "."),
("/path/to", "/spam/", "../path/to"),
("path/to", "spam/", "../path/to"),
Copy link
Contributor

Choose a reason for hiding this comment

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

no path

("/", "/to", ".."),
("/", "/path/to", "../.."),

normal

("/path", "/path/to", ".."),

trailing / - empy segment at the end

("/path", "/path/", ".."),
("/path", "/path/to/", "../.."),

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member

@asvetlov asvetlov left a comment

Choose a reason for hiding this comment

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

While I support the intention I propose more conservative approach.
Let's provide a method, name it, instead of __sub__ operator overloading.
Add tests, doc, etc.

Publish new yarl release, wait for feedback.
Tune and polish.
After some time we can return to adding __sub__.

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
5 participants