Skip to content

Commit

Permalink
Add proper == and != support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian committed Oct 18, 2023
1 parent 9bd715f commit 7edc0f3
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "url-py"
version = "0.3.3"
version = "0.4.0"
edition = "2021"

[lib]
Expand Down
1 change: 1 addition & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ API Reference
:members:
:undoc-members:
:imported-members:
:special-members: __iter__, __getitem__, __len__, __truediv__
5 changes: 5 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
Changelog
=========

v0.4.0
-------

* Add support for ``/`` and proper equality checking.

v0.3.3
-------

Expand Down
16 changes: 13 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use pyo3::{create_exception, exceptions::PyException, prelude::*, types::PyType};
use pyo3::{
create_exception, exceptions::PyException, prelude::*, pyclass::CompareOp, types::PyType,
};
use url::{ParseError, Url};

create_exception!(url, URLError, PyException);
Expand Down Expand Up @@ -47,16 +49,24 @@ impl UrlPy {
self.inner.to_string()
}

fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> PyObject {
match op {
CompareOp::Eq => (self.inner == other.inner).into_py(py),
CompareOp::Ne => (self.inner != other.inner).into_py(py),
_ => py.NotImplemented(),
}
}

fn __truediv__(&self, other: &str) -> PyResult<Self> {
self.join(other)
}

#[classmethod]
fn parse(_cls: &PyType, value: &str) -> PyResult<UrlPy> {
fn parse(_cls: &PyType, value: &str) -> PyResult<Self> {
from_result(Url::parse(value))
}

fn join(&self, input: &str) -> PyResult<UrlPy> {
fn join(&self, input: &str) -> PyResult<Self> {
from_result(self.inner.join(input))
}

Expand Down
8 changes: 8 additions & 0 deletions tests/test_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,11 @@ def test_invalid_relative_url_without_base():
def test_invalid_junk():
with pytest.raises(URLError):
URL.parse("https:/12949a;df;;@@@")


def test_eq():
"""
Support the / operator as many Python types have decided to.
"""
assert URL.parse("http://example.com") == URL.parse("http://example.com")
assert URL.parse("http://foo.com") != URL.parse("http://bar.com")

0 comments on commit 7edc0f3

Please sign in to comment.