bug: strings not accepted in files param #1897
-
import requests
import httpx
# requests works
resp = requests.post("https://httpbin.org/post", files=[("name", "content")])
assert resp.status_code == 200
assert resp.json()["files"] == {"name": "content"}
# httpx fails, despite the signature of `files` saying it accepts a 2 tuple of (name: str, data: Union[str, bytes, file])
httpx.post("https://httpbin.org/post", files=[("name", "content")]) # raises a TypeError
assert resp.status_code == 200
assert resp.json()["files"] == {"name": "content"} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
So... FileContent = Union[IO[bytes], bytes]
FileTypes = Union[
# file (or text)
FileContent,
# (filename, file (or text))
Tuple[Optional[str], FileContent],
# (filename, file (or text), content_type)
Tuple[Optional[str], FileContent, Optional[str]],
]
RequestFiles = Union[Mapping[str, FileTypes], Sequence[Tuple[str, FileTypes]]] Those comments look like they need updating tho'. They're not in line with what We switched from accepting text-or-bytes to bytes-only here. (We need to be able to know upfront how many bytes the content will be, and files opened in text mode don't allow us to do that, but text strings could be allowable, perhaps.) |
Beta Was this translation helpful? Give feedback.
So...
content
there needs to be bytes - the annotation is derived from here... https://github.com/encode/httpx/blob/master/httpx/_types.py#L84-L93Those comments look like they need updating tho'. They're not in line with what
FileContent
actually accepts.We switched from accepting text-or-bytes to bytes-only here.
We could feasibly reconsider t…