Docs are incorrect regarding UploadFile #9922
-
First Check
Commit to Help
Example Codefrom fastapi import UploadFile
file_name = 'test.png'
file_path = 'test.png'
with open(file_path, "rb") as f:
file=UploadFile(
filename=file_name, file=f.read(), content_type="image/png"
)
) Description
Docs need to be updated to accurately reflect changes to Starlette Operating SystemLinux Operating System DetailsNo response FastAPI Version0.100.0 Python Version3.11 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @holyoaks, I believe what is stated in the docs is absolutely correct. That is, an instance of an UploadFile indeed has a Looking into the signature of the UploadFile class one can see that a content_type is a property method. This means that this attribute content_type is calculate on the fly every time you access class UploadFile:
"""
An uploaded file included as part of the request data.
"""
def __init__(
self,
file: typing.BinaryIO,
*,
size: typing.Optional[int] = None,
filename: typing.Optional[str] = None,
headers: "typing.Optional[Headers]" = None,
) -> None:
self.filename = filename
self.file = file
self.size = size
self.headers = headers or Headers()
@property
def content_type(self) -> typing.Optional[str]:
return self.headers.get("content-type", None) |
Beta Was this translation helpful? Give feedback.
Hi @holyoaks,
I believe what is stated in the docs is absolutely correct. That is, an instance of an UploadFile indeed has a
content_type
attribute. Having an attribute does not imply that UploadFile accepts content_type in the__init__
method.Looking into the signature of the UploadFile class one can see that a content_type is a property method. This means that this attribute content_type is calculate on the fly every time you access
some_file.content_type
attribute.