Skip to content
Myr

Tweak query param types (#175)

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

* Tweak query param types * Handle primitive data types for queryparams * QueryParam keys as str-only, not PrimitiveData

authored by Tom Christie · committed by GitHub · parent 3382297 · Browse files · 3 files+42 added−4 removed
This commit

State

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

Diff stats

3 files+42 added−4 removed

  • tests:+20 added−0 removed
  • other:+22 added−4 removed

3 files+42 added−4 removed

modified:httpx/models.py+7 added-4 removed
32 unmodified lines
33 guess_json_utf,34 is_known_encoding,35 normalize_header_key,36 normalize_header_value,37 str_query_param38)3940PrimitiveData = typing.Union[str, int, float, bool, type(None)]4142URLTypes = typing.Union["URL", str]4344QueryParamTypes = typing.Union[45 "QueryParams",43 typing.Mapping[str, str],44 typing.List[typing.Tuple[typing.Any, typing.Any]],46 typing.Mapping[str, PrimitiveData],47 typing.List[typing.Tuple[str, PrimitiveData]],48 str,49]5051HeaderTypes = typing.Union[
218 unmodified lines
270 items = value271 else:272 items = value.items() # type: ignore273271 self._list = [(str(k), str(v)) for k, v in items]272 self._dict = {str(k): str(v) for k, v in items}274 self._list = [(str(k), str_query_param(v)) for k, v in items]275 self._dict = {str(k): str_query_param(v) for k, v in items}276277 def getlist(self, key: typing.Any) -> typing.List[str]:278 return [item_value for item_key, item_value in self._list if item_key == key]279
906 unmodified lines
modified:httpx/utils.py+15 added
18 unmodified lines
19 return value20 return value.encode(encoding or "ascii")212223def str_query_param(value: typing.Union[str, int, float, bool, type(None)]) -> str:24 """25 Coerce a primitive data type into a string value for query params.2627 Note that we prefer JSON-style 'true'/'false' for boolean values here.28 """29 if value is True:30 return "true"31 elif value is False:32 return "false"33 elif value is None:34 return ""35 return str(value)363738def is_known_encoding(encoding: str) -> bool:39 """40 Return `True` if `encoding` is a known codec.41 """
40 unmodified lines
modified:tests/models/test_queryparams.py+20 added
29 unmodified lines
30 assert QueryParams({"a": "123", "b": "456"}) != "invalid"3132 q = QueryParams([("a", "123"), ("a", "456")])33 assert QueryParams(q) == q343536def test_queryparam_types():37 q = QueryParams({"a": True})38 assert str(q) == "a=true"3940 q = QueryParams({"a": False})41 assert str(q) == "a=false"4243 q = QueryParams({"a": ""})44 assert str(q) == "a="4546 q = QueryParams({"a": None})47 assert str(q) == "a="4849 q = QueryParams({"a": 1.23})50 assert str(q) == "a=1.23"5152 q = QueryParams({"a": 123})53 assert str(q) == "a=123"