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

Conversation

tomchristie
Copy link
Member

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'>.

@tomchristie tomchristie added the user-experience Ensuring that users have a good experience using the library label Dec 30, 2022
@@ -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.

🤨

tomchristie and others added 3 commits December 30, 2022 20:07
@tomchristie tomchristie merged commit 4cbf13e into master Dec 30, 2022
@tomchristie tomchristie deleted the raise-type-error-on-invalid-query-params branch December 30, 2022 20:56
@@ -89,7 +89,7 @@ def test_empty_query_params():

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.

@tomchristie
Copy link
Member Author

Ooofff. Okay, more care needed in the future.

@tomchristie
Copy link
Member 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
@tomchristie tomchristie mentioned this pull request Jan 4, 2023
tomchristie 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

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