Skip to content

Commit

Permalink
Raise a proper type error on invalid URL type (#1259)
Browse files Browse the repository at this point in the history
* Added test for expected URL class behavior

* Updated URL class, tests pass

* Updated to include type in error message
  • Loading branch information
wozniakty authored Sep 4, 2020
1 parent 8fa8765 commit 42c6686
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
6 changes: 5 additions & 1 deletion httpx/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,12 @@ def __init__(self, url: URLTypes = "", params: QueryParamTypes = None) -> None:
# 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:
Expand Down
8 changes: 8 additions & 0 deletions tests/models/test_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 42c6686

Please sign in to comment.