Skip to content

Add Support for Relative Path Calculation #1183

@gshmu

Description

@gshmu

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions