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 2 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
4 changes: 3 additions & 1 deletion httpx/_utils.py
Expand Up @@ -67,7 +67,9 @@ 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)!r}.")


def is_known_encoding(encoding: str) -> bool:
Expand Down
5 changes: 5 additions & 0 deletions tests/models/test_queryparams.py
Expand Up @@ -87,6 +87,11 @@ def test_empty_query_params():
assert str(q) == "a="


def test_invalid_query_params():
with pytest.raises(TypeError):
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems nice to ensure that the formatted message is correct

Suggested change
with pytest.raises(TypeError):
with pytest.raises(TypeError, match=r"Expected str, int, float, bool, or None\. Got 'bytes'\."):

Copy link
Member Author

Choose a reason for hiding this comment

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

That's neat, thanks.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry I broke your line length :)

Copy link
Member Author

Choose a reason for hiding this comment

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

🤨

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


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