-
-
Notifications
You must be signed in to change notification settings - Fork 932
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
remove file parameter from UploadFile #1406
Conversation
Hrm. So the parameters in the constructor here are meaningful/useful only from a perspective of "UploadFile() should be a nice stand-alone testable interface". Interestingly after reviewing this, if I was refactoring this, I'd probably do this the other way around... class UploadFile:
def __init__(
self,
file: typing.Optional[typing.BinaryIO],
*,
filename: str = "",
content_type: str = "",
headers: typing.Optional[Headers] = None,
) -> None:
... In this style the form parsing would be responsible for opening a temporary file. I kinda prefer this style because it makes sense the actual file I/O and eg. temporary file naming styles policy etc to be outside of this class, and for this to actually be a small wrapper onto "I'm a (binary-only) file-like object, that also has HTTP headers attached." There's some finer details to talk around about that, but on the whole I think that way around makes more sense. Keep the I/O out of the datastructure itself. |
I see your point Tom. I think that would certainly be an improvement since there'd only be 1 execution path instead of 2 and we'd actually use that 1 execution path in Starlette. I'm happy to make a PR for that instead if we settle on it. Some practical questions: how will this play with #1405? Does |
Depends - one option here would be to drop the The nice thing then is that the
|
Yup I like that! |
A couple possible issues with this design Tom:
|
The filename (as specified by the client performing the upload) comes from the Content-Disposition. I don't think(?) we need that in the parser at all. The path that we use for the file is just whatever the temporary file is given. (right?) Correct. It's possible that browser clients might include it (since it allows servers to determine up front if they want to accept or reject the upload), but I've no indication if they do or not - it'd need looking into. Even if |
We use the filename to determine if it's a file or form field: starlette/starlette/formparsers.py Lines 231 to 235 in fcc4c70
Fair enough, we can keep that as a separate discussion. |
Closing in favor of #1413 |
As per discussion in #1405 and #1312, this parameter is not used in Starlette and is not documented as part of the public API. Having this parameters makes it harder to implement features like the one suggested in #1405.
This PR would remove this parameter.
This may break users tests, but should not break their production code since Starlette doesn't currently use that feature.
For a user to experience a breakage at runtime in their production code, they would have to have been using an undocumented constructor parameter to UploadFile outside of the context of the rest of Starlette, which I'm guessing is no one.