-
-
Notifications
You must be signed in to change notification settings - Fork 930
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
allow using Request.form() as a context manager #1903
Conversation
f342f8c
to
fa974b6
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Amazing! And beautiful types! 🤩 👏
@@ -265,8 +267,11 @@ async def form(self) -> FormData: | |||
self._form = FormData() | |||
return self._form | |||
|
|||
def form(self) -> AwaitableOrContextManager[FormData]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be async?
def form(self) -> AwaitableOrContextManager[FormData]: | |
async def form(self) -> AwaitableOrContextManager[FormData]: |
I understand it would work either way just because the awaitable thing is returned, but maybe it could help to make it async, to make it explicit in the function that it returns something to be awaited. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately I don't think that will work: we want to pass an await able object to AwaitableOrContextManagerWrapper
so that it can be used like async with request.form()
and not async with await request.form()
.
I can live with it. 🤷♂️ Also, since 3.11 warns, it shouldn't be much of an issue, I guess? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@adriangb Would you mind adding some lines to the docs about this? 🙏
I'd say for us to focus on getting 1.0 out. It's not a big deal to further deprecate later on. |
I opted to just edit the usage in the docs examples. I do think we should recommend this usage over the |
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
Coming from #1888 (comment)
Evidently forgetting / not knowing to call
UploadFile.close()
is a common issue.I think FastAPI should be able to automatically call that internally, but Starlette should also do more to nudge users in the right direction. I think the right tool for that in this situation is a context manager.
This PR allows users to use
Request.form()
both directly (as before) and as a context manager.I would suggest that we then deprecate the
await request.form()
usage so that there is only 1 correct way to do things.