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

Add Support for Relative Path Calculation #1183

Open
1 task done
gshmu opened this issue Oct 9, 2024 · 4 comments · Fixed by #1340 · May be fixed by #1392
Open
1 task done

Add Support for Relative Path Calculation #1183

gshmu opened this issue Oct 9, 2024 · 4 comments · Fixed by #1340 · May be fixed by #1392
Assignees

Comments

@gshmu
Copy link

gshmu commented Oct 9, 2024

Is your feature request related to a problem?

No response

Describe the solution you'd like

Current Behavior

The yarl library provides an intuitive API for handling URL parsing and construction. However, it lacks an operator for directly calculating the relative path between two URLs.

Proposed Feature

Introduce support for using the subtraction operator (-) to calculate the relative path between two URL objects. This would simplify the syntax for relative path calculation, making it more intuitive. The operator could calculate the difference between two URLs' paths and return the relative path.

Example Usage
from yarl import URL

url1 = URL('http://example.com/docs/')
url2 = URL('http://example.com/docs/a.json')

relative_path = url1 - url2  # Proposed feature

print(relative_path)  # Expected output: 'a.json'

Describe alternatives you've considered

This feature would simplify URL handling in situations where relative paths are frequently needed and would make yarl even more developer-friendly. It would be especially useful in web applications and API development.

Thank you for considering this feature request!

Additional context

No response

Code of Conduct

  • I agree to follow the aio-libs Code of Conduct
@gshmu
Copy link
Author

gshmu commented Oct 15, 2024

eg: https://github.com/fastapi/fastapi/pull/12421/files

@oleksbabieiev
Copy link
Contributor

This idea looks quite interesting. But I think it would be more intuitive if we "subtracted" the shorter link from the longer one.
Like this:

from yarl import URL

target = URL('http://example.com/docs/a.json')
base = URL('http://example.com/docs/')

relative_path = target - base

print(relative_path)  # Expected output: 'a.json'

So I would just swap the operands.

@oleksbabieiev
Copy link
Contributor

I haven't contributed to this repository yet, but it looks like a "good first issue" to me. I'll try to implement your idea today or tomorrow.

@bdraco
Copy link
Member

bdraco commented Oct 25, 2024

We discovered that approach in #1340 isn't ready to ship so I've opened #1391 to revert it so we can try again.

more details starting at #1388 (comment)

@oleksbabieiev oleksbabieiev linked a pull request Oct 25, 2024 that will close this issue
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants