Skip to content

Commit

Permalink
Raise TypeError on invalid query params. (#2523)
Browse files Browse the repository at this point in the history
* Raise TypeError on invalid query params

* Fix TypeError

* Update tests/models/test_queryparams.py

Co-authored-by: Michael Adkins <[email protected]>

* Linting

* Fix exception check

Co-authored-by: Michael Adkins <[email protected]>
  • Loading branch information
tomchristie and zanieb authored Dec 30, 2022
1 parent 10a3b68 commit 4cbf13e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
6 changes: 5 additions & 1 deletion httpx/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ def primitive_value_to_str(value: "PrimitiveData") -> str:
return "false"
elif value is None:
return ""
return str(value)
elif isinstance(value, (str, float, int)):
return str(value)
raise TypeError(
f"Expected str, int, float, bool, or None. Got {type(value).__name__!r}."
)


def is_known_encoding(encoding: str) -> bool:
Expand Down
7 changes: 7 additions & 0 deletions tests/models/test_queryparams.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ def test_empty_query_params():
assert str(q) == "a="


def test_invalid_query_params():
with pytest.raises(
TypeError, match=r"Expected str, int, float, bool, or None. Got 'bytes'."
):
httpx.QueryParams({"a": b"bytes"})


def test_queryparam_update_is_hard_deprecated():
q = httpx.QueryParams("a=123")
with pytest.raises(RuntimeError):
Expand Down

0 comments on commit 4cbf13e

Please sign in to comment.