From: Tom Christie Date: Fri, 30 Dec 2022 20:56:48 +0000 (+0000) Subject: Raise `TypeError` on invalid query params. (#2523) X-Git-Tag: 0.23.2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4cbf13ece2e584b45b935df0a0c670e1863c4569;p=thirdparty%2Fhttpx.git Raise `TypeError` on invalid query params. (#2523) * Raise TypeError on invalid query params * Fix TypeError * Update tests/models/test_queryparams.py Co-authored-by: Michael Adkins * Linting * Fix exception check Co-authored-by: Michael Adkins --- diff --git a/httpx/_utils.py b/httpx/_utils.py index 1f64deed..1e1570ee 100644 --- a/httpx/_utils.py +++ b/httpx/_utils.py @@ -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: diff --git a/tests/models/test_queryparams.py b/tests/models/test_queryparams.py index 29b2ca63..cbb487bb 100644 --- a/tests/models/test_queryparams.py +++ b/tests/models/test_queryparams.py @@ -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):