Skip to content

Raise TypeError on invalid query params.#2523

Merged
lovelydinosaur merged 5 commits intomasterfrom
raise-type-error-on-invalid-query-params
Dec 30, 2022
Merged

Raise TypeError on invalid query params.#2523
lovelydinosaur merged 5 commits intomasterfrom
raise-type-error-on-invalid-query-params

Conversation

@lovelydinosaur
Copy link
Contributor

Prompted by #2515. (Doesn't presume any particular resolution to that case.)

Closes #746 - we don't support bytes (or other unexpected types) as QueryParam values. Let's raise a clear error, rather than coercing an unexpected str value.

Before this change...

>>> httpx.URL("http://www.example.com/", params={"query": b"abc"})
URL('http://www.example.com/?query=b%27abc%27')

After this change...

>>> httpx.URL("http://www.example.com/", params={"query": b"abc"})
TypeError: Expected str, int, float, bool, or None. Got <class 'bytes'>.

@lovelydinosaur lovelydinosaur added the user-experience Ensuring that users have a good experience using the library label Dec 30, 2022


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
Contributor 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
Contributor Author

Choose a reason for hiding this comment

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

🤨

lovelydinosaur and others added 3 commits December 30, 2022 20:07
@lovelydinosaur lovelydinosaur merged commit 4cbf13e into master Dec 30, 2022
@lovelydinosaur lovelydinosaur deleted the raise-type-error-on-invalid-query-params branch December 30, 2022 20:56
def test_invalid_query_params():
with pytest.raises(
TypeError, match=r"Expected str, int, float, bool, or None\. Got 'bytes'\."
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.

@zanieb
Copy link
Contributor

zanieb commented Jan 3, 2023

@tomchristie this broke Prefect since we were passing UUID objects — that's fine but I think we should make sure this is listed on the changelog for 0.23.2 for visibility.

@lovelydinosaur
Copy link
Contributor Author

Ooofff. Okay, more care needed in the future.

@lovelydinosaur
Copy link
Contributor Author

That's entirely on me - rushing things that didn't need to be rushed.

Do we want to roll this back in a 0.23.3, and leave it for the upcoming 0.24.0 update? (Or do we leave it as is?)

@zanieb
Copy link
Contributor

zanieb commented Jan 3, 2023

It'd probably be safest to make this change in 0.24.0 instead since it changes the supported interface (I'll try to keep an eye out for this in future reviews), but I don't have strong feelings about rolling it back.

zanieb added a commit that referenced this pull request Jan 3, 2023
@lovelydinosaur lovelydinosaur mentioned this pull request Jan 4, 2023
lovelydinosaur pushed a commit that referenced this pull request Jan 4, 2023
@curtiscook
Copy link

curtiscook commented Jan 4, 2023

@tomchristie this broke Prefect since we were passing UUID objects — that's fine but I think we should make sure this is listed on the changelog for 0.23.2 for visibility.

FYI - This also breaks datetime -> str conversion -- would suggest to keep datetime supported and adhering to RFC

samclearman pushed a commit to titanmsp/httpx that referenced this pull request Apr 26, 2025
* Raise TypeError on invalid query params

* Fix TypeError

* Update tests/models/test_queryparams.py

Co-authored-by: Michael Adkins <contact@zanie.dev>

* Linting

* Fix exception check

Co-authored-by: Michael Adkins <contact@zanie.dev>
samclearman pushed a commit to titanmsp/httpx that referenced this pull request Apr 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

user-experience Ensuring that users have a good experience using the library

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support passing bytes keys/values in params=...

3 participants