Skip to content
Myr

Raise a proper type error on invalid URL type (#1259)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Added test for expected URL class behavior * Updated URL class, tests pass * Updated to include type in error message

authored by Tyler Wozniak · committed by GitHub · parent 8fa8765 · Browse files · 2 files+13 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+13 added−1 removed

  • tests:+8 added−0 removed
  • other:+5 added−1 removed

2 files+13 added−1 removed

modified:httpx/_models.py+5 added-1 removed
69 unmodified lines
70 if self.is_absolute_url:71 # We don't want to normalize relative URLs, since doing so72 # removes any leading `../` portion.73 self._uri_reference = self._uri_reference.normalize()74 else:74 elif isinstance(url, URL):75 self._uri_reference = url._uri_reference76 else:77 raise TypeError(78 f"Invalid type for url. Expected str or httpx.URL, got {type(url)}"79 )8081 # Add any query parameters, merging with any in the URL if needed.82 if params:83 if self._uri_reference.query:
1159 unmodified lines
modified:tests/models/test_url.py+8 added
202 unmodified lines
203204def test_url_invalid():205 with pytest.raises(httpx.InvalidURL):206 httpx.URL("https://😇/")207208209def test_url_invalid_type():210 class ExternalURLClass: # representing external URL class211 pass212213 with pytest.raises(TypeError):214 httpx.URL(ExternalURLClass()) # type: ignore