Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise TypeError on invalid query params. #2523

Merged
merged 5 commits into from
Dec 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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'."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need the r if you aren't escaping the periods — this is doing a re.search so technically the . will be a wildcard but 🤷‍♀️ it's not likely to cause you any problems. You could use re.escape if you really wanted to be correct.

):
httpx.QueryParams({"a": b"bytes"})


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