Repository: encode/httpx

Issue:
API design question - `Response.url`
Currently our `Response.url` attribute exposes a `URL` instance.

This is a breaking change from the requests API where it just exposes a plain string.

It's feasible that we should instead only be exposing plain string URLs, in order to aim for drop-in replacement API compatibility w/ requests, *and* in order to keep the API surface area low.

Options here are:

* Expose `request.url` as a URL instance. (Richer information, URL class is also useful in its own right.)
* Expose `request.url` as a str. (Better API compat. Lower API surface area to maintain.)
* Expose `request.url` as a str, and `request.urlinfo` as a URL instance. (Better API compat. High API surface area.)


Discussion:
tomchristie: Great, yup! 👍

Patch:
diff --git a/httpx/models.py b/httpx/models.py
index 710c030..9cbdc25 100644
--- a/httpx/models.py
+++ b/httpx/models.py
@@ -204,7 +204,7 @@ class URL:
         return hash(str(self))
 
     def __eq__(self, other: typing.Any) -> bool:
-        return isinstance(other, URL) and str(self) == str(other)
+        return isinstance(other, (URL, str)) and str(self) == str(other)
 
     def __str__(self) -> str:
         return self.components.unsplit()


Test patch:
diff --git a/tests/models/test_url.py b/tests/models/test_url.py
index 5f5208c..70089e0 100644
--- a/tests/models/test_url.py
+++ b/tests/models/test_url.py
@@ -25,6 +25,12 @@ def test_url():
     assert new.scheme == "http"
 
 
+def test_url_eq_str():
+    url = URL("https://example.org:123/path/to/somewhere?abc=123#anchor")
+    assert url == "https://example.org:123/path/to/somewhere?abc=123#anchor"
+    assert str(url) == url
+
+
 def test_url__params():
     url = URL("https://example.org:123/path/to/somewhere", params={"a": "123"})
     assert str(url) == "https://example.org:123/path/to/somewhere?a=123"