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 14 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
Copy link

codspeed-hq bot commented Oct 25, 2024

CodSpeed Performance Report

Merging #1392 will not alter performance

Comparing oleksbabieiev:subtraction (ad61c24) with master (c1f6ef6)

Summary

✅ 99 untouched benchmarks
🆕 2 new benchmarks

Benchmarks breakdown

Benchmark master oleksbabieiev:subtraction Change
🆕 test_relative_to N/A 778.9 µs N/A
🆕 test_relative_to_long_urls N/A 4.4 ms N/A

Copy link

codecov bot commented Oct 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.20%. Comparing base (c1f6ef6) to head (ad61c24).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1392      +/-   ##
==========================================
+ Coverage   96.14%   96.20%   +0.05%     
==========================================
  Files          31       31              
  Lines        5970     6053      +83     
  Branches      364      370       +6     
==========================================
+ Hits         5740     5823      +83     
  Misses        204      204              
  Partials       26       26              
Flag Coverage Δ
CI-GHA 96.20% <100.00%> (+0.05%) ⬆️
MyPy 50.28% <90.00%> (+0.56%) ⬆️
OS-Linux 99.57% <100.00%> (+<0.01%) ⬆️
OS-Windows 99.63% <100.00%> (+<0.01%) ⬆️
OS-macOS 99.32% <100.00%> (+<0.01%) ⬆️
Py-3.10.11 99.30% <100.00%> (+<0.01%) ⬆️
Py-3.10.15 99.52% <100.00%> (+<0.01%) ⬆️
Py-3.11.11 99.52% <100.00%> (+<0.01%) ⬆️
Py-3.11.9 99.30% <100.00%> (+<0.01%) ⬆️
Py-3.12.8 99.52% <100.00%> (+<0.01%) ⬆️
Py-3.13.1 99.52% <100.00%> (+<0.01%) ⬆️
Py-3.9.13 99.26% <100.00%> (+<0.01%) ⬆️
Py-3.9.20 99.48% <100.00%> (+<0.01%) ⬆️
Py-pypy7.3.16 99.54% <100.00%> (+<0.01%) ⬆️
Py-pypy7.3.17 99.56% <100.00%> (+<0.01%) ⬆️
VM-macos-latest 99.32% <100.00%> (+<0.01%) ⬆️
VM-ubuntu-latest 99.57% <100.00%> (+<0.01%) ⬆️
VM-windows-latest 99.63% <100.00%> (+<0.01%) ⬆️
pytest 99.57% <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

docs/api.rst Outdated Show resolved Hide resolved
tests/test_url.py Outdated Show resolved Hide resolved
("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"),

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If the last segment of the path does not have a / at the end, it is simply ignored when calculating the relative path

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In fact, we can say that if the path ends with a /, then its last segment is treated as a directory, otherwise - as a file. At least that's how it works for now 🤷‍♂️

Copy link
Contributor

Choose a reason for hiding this comment

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

pathlib PosixPath can not be used to relativize URL pathes as URL pathes can have empty segments, and PosixPath strips trailing / anyway.

Copy link
Contributor Author

@oleksbabieiev oleksbabieiev Dec 27, 2024

Choose a reason for hiding this comment

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

Yeah, I've already noticed that...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll think about what to replace it with

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think I've managed to solve it

("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__.

@oleksbabieiev
Copy link
Contributor Author

oleksbabieiev commented Dec 27, 2024

I sincerely apologize for such a long break. I haven't forgotten about this pull request, I've just been really busy lately. Now I need some time to remember everything and get back to work...

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