]> git.ipfire.org Git - thirdparty/starlette.git/commitdiff
Add `UploadFile.__repr__` (#2360)
authorSamuel Colvin <s@muelcolvin.com>
Tue, 5 Dec 2023 07:34:41 +0000 (10:34 +0300)
committerGitHub <noreply@github.com>
Tue, 5 Dec 2023 07:34:41 +0000 (00:34 -0700)
starlette/datastructures.py
tests/test_datastructures.py

index dc57c2e9f8f0abcf119c1be2cd81e38c446dfa2b..a0c3ba14018e2ce87ca223e843e5f024d8089f4b 100644 (file)
@@ -482,6 +482,14 @@ class UploadFile:
         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]]):
     """
index 79d27cc049a9f5185c297a479863c0d12d49ae76..c1268327cadb9eb2a16a4511dbc4aa7602c2d2a1 100644 (file)
@@ -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