]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Raise a proper type error on invalid URL type (#1259)
authorTyler Wozniak <wozniak.ty@live.com>
Fri, 4 Sep 2020 21:14:59 +0000 (14:14 -0700)
committerGitHub <noreply@github.com>
Fri, 4 Sep 2020 21:14:59 +0000 (23:14 +0200)
* Added test for expected URL class behavior

* Updated URL class, tests pass

* Updated to include type in error message

httpx/_models.py
tests/models/test_url.py

index 17ba955890ecb395ed84e34604fdc350a3cd7e07..5b6a9b65710d8e3692fdbf2eceedd07987357dee 100644 (file)
@@ -71,8 +71,12 @@ class URL:
                 # We don't want to normalize relative URLs, since doing so
                 # removes any leading `../` portion.
                 self._uri_reference = self._uri_reference.normalize()
-        else:
+        elif isinstance(url, URL):
             self._uri_reference = url._uri_reference
+        else:
+            raise TypeError(
+                f"Invalid type for url.  Expected str or httpx.URL, got {type(url)}"
+            )
 
         # Add any query parameters, merging with any in the URL if needed.
         if params:
index fa75e556a65f1421099aadf9bf9cc08dad60d769..8d34a75a790f873469cde22d367cb0156ec3065c 100644 (file)
@@ -204,3 +204,11 @@ def test_url_copywith_for_userinfo():
 def test_url_invalid():
     with pytest.raises(httpx.InvalidURL):
         httpx.URL("https://😇/")
+
+
+def test_url_invalid_type():
+    class ExternalURLClass:  # representing external URL class
+        pass
+
+    with pytest.raises(TypeError):
+        httpx.URL(ExternalURLClass())  # type: ignore