allow using Request.form() as a context manager#1903
allow using Request.form() as a context manager#1903adriangb merged 11 commits intoKludex:masterfrom
Conversation
f342f8c to
fa974b6
Compare
tiangolo
left a comment
There was a problem hiding this comment.
Amazing! And beautiful types! 🤩 👏
| self._form = FormData() | ||
| return self._form | ||
|
|
||
| def form(self) -> AwaitableOrContextManager[FormData]: |
There was a problem hiding this comment.
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.
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? |
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.