Skip to content

Commit

Permalink
Add UploadFile.__repr__ (#2360)
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin authored Dec 5, 2023
1 parent 3df3df1 commit d29d993
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
8 changes: 8 additions & 0 deletions starlette/datastructures.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,14 @@ async def close(self) -> None:
else:
await run_in_threadpool(self.file.close)

def __repr__(self) -> str:
return (
f"{self.__class__.__name__}("
f"filename={self.filename!r}, "
f"size={self.size!r}, "
f"headers={self.headers!r})"
)


class FormData(ImmutableMultiDict[str, typing.Union[UploadFile, str]]):
"""
Expand Down
17 changes: 17 additions & 0 deletions tests/test_datastructures.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,23 @@ def test_formdata():
assert FormData({"a": "123", "b": "789"}) != {"a": "123", "b": "789"}


@pytest.mark.anyio
async def test_upload_file_repr():
stream = io.BytesIO(b"data")
file = UploadFile(filename="file", file=stream, size=4)
assert repr(file) == "UploadFile(filename='file', size=4, headers=Headers({}))"


@pytest.mark.anyio
async def test_upload_file_repr_headers():
stream = io.BytesIO(b"data")
file = UploadFile(filename="file", file=stream, headers=Headers({"foo": "bar"}))
assert (
repr(file)
== "UploadFile(filename='file', size=None, headers=Headers({'foo': 'bar'}))"
)


def test_multidict():
q = MultiDict([("a", "123"), ("a", "456"), ("b", "789")])
assert "a" in q
Expand Down

0 comments on commit d29d993

Please sign in to comment.