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

Stricter data typechecking #3128

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

tomchristie
Copy link
Member

Closes #3127

Before...

>>> import httpx
>>> data = {"key": {"this": "ain't json, buddy"}}
>>> r = httpx.post("https://www.example.com", data=data)
>>> r.request.content
b'key=%7B%27this%27%3A+%22ain%27t+json%2C+buddy%22%7D'

After...

>>> import httpx
>>> data = {"key": {"this": "ain't json, buddy"}}
>>> r = httpx.post("https://www.example.com", data=data)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/tom.christie/GitHub/encode/httpx/httpx/_api.py", line 331, in post
    return request(
  File "/Users/tom.christie/GitHub/encode/httpx/httpx/_api.py", line 118, in request
    return client.request(
  File "/Users/tom.christie/GitHub/encode/httpx/httpx/_client.py", line 825, in request
    request = self.build_request(
  File "/Users/tom.christie/GitHub/encode/httpx/httpx/_client.py", line 359, in build_request
    return Request(
  File "/Users/tom.christie/GitHub/encode/httpx/httpx/_models.py", line 342, in __init__
    headers, stream = encode_request(
  File "/Users/tom.christie/GitHub/encode/httpx/httpx/_content.py", line 235, in encode_request
    return encode_urlencoded_data(data)
  File "/Users/tom.christie/GitHub/encode/httpx/httpx/_content.py", line 167, in encode_urlencoded_data
    plain_data.append(_coerce_type(key, value))
  File "/Users/tom.christie/GitHub/encode/httpx/httpx/_content.py", line 153, in _coerce_type
    raise TypeError(
TypeError: Request data values must be str, int, float, bool, or None. Got type 'dict' for key 'key'.

I've not changed any type signatures here, and aimed to keep the PR as focused as possible.

Ideally we should be following up with more comprehensive documentation on the behaviour of content=..., data=..., files=..., json=....

@tomchristie tomchristie added the user-experience Ensuring that users have a good experience using the library label Mar 1, 2024
@tomchristie tomchristie requested review from adriangb and a team March 1, 2024 15:53
@tomchristie
Copy link
Member Author

Oh... thanks for the fast turnaround @zanieb. 😊

Copy link
Contributor

@T-256 T-256 left a comment

Choose a reason for hiding this comment

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

Is it breaking change? how about rebase to version-1.0 branch?
Should it also apply for QueryParams?

@@ -133,15 +135,36 @@ def encode_content(
raise TypeError(f"Unexpected type for 'content', {type(content)!r}")


def _coerce_type(key: str, value: PrimitiveData) -> Tuple[str, str]:
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
def _coerce_type(key: str, value: PrimitiveData) -> Tuple[str, str]:
def _coerce_type(key: str, value: PrimitiveData) -> tuple[str, str]:

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.

Stricter type checking on request data.
4 participants