Skip to content

allow using Request.form() as a context manager#1903

Merged
adriangb merged 11 commits intoKludex:masterfrom
adriangb:allow-using-formdata-as-cm
Feb 6, 2023
Merged

allow using Request.form() as a context manager#1903
adriangb merged 11 commits intoKludex:masterfrom
adriangb:allow-using-formdata-as-cm

Conversation

@adriangb
Copy link
Contributor

@adriangb adriangb commented Oct 6, 2022

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.

@adriangb adriangb closed this Oct 6, 2022
@adriangb adriangb force-pushed the allow-using-formdata-as-cm branch from f342f8c to fa974b6 Compare October 6, 2022 15:15
@adriangb adriangb reopened this Oct 6, 2022
@adriangb adriangb requested a review from tiangolo October 6, 2022 15:52
Copy link
Contributor

@tiangolo tiangolo left a 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! 🤩 👏

self._form = FormData()
return self._form

def form(self) -> AwaitableOrContextManager[FormData]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be async?

Suggested change
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?

Copy link
Contributor Author

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().

@Kludex Kludex added the hold Don't merge it label Oct 6, 2022
@Kludex Kludex requested a review from lovelydinosaur October 6, 2022 18:16
@adriangb adriangb requested a review from Kludex October 6, 2022 18:25
@Kludex Kludex removed the hold Don't merge it label Feb 4, 2023
@Kludex
Copy link
Owner

Kludex commented Feb 4, 2023

I would suggest that we then deprecate the await request.form() usage so that there is only 1 correct way to do things.

I can live with it. 🤷‍♂️

Also, since 3.11 warns, it shouldn't be much of an issue, I guess?

Copy link
Owner

@Kludex Kludex left a 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? 🙏

@Kludex
Copy link
Owner

Kludex commented Feb 4, 2023

I would suggest that we then deprecate the await request.form() usage so that there is only 1 correct way to do things.

I can live with it. man_shrugging

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.

@adriangb
Copy link
Contributor Author

adriangb commented Feb 5, 2023

I opted to just edit the usage in the docs examples. I do think we should recommend this usage over the await request.form() usage so I felt like adding a note along the lines of "by the way you can do it both ways" would create confusion with no benefit to users. If they're used to doing it the other way they can continue to do so (unless we decide to deprecate it at some point, which I am in favor of but I agree doesn't need to happen right now).

@Kludex Kludex mentioned this pull request Feb 5, 2023
7 tasks
@adriangb adriangb enabled auto-merge (squash) February 5, 2023 21:52
@adriangb adriangb merged commit c568b55 into Kludex:master Feb 6, 2023
@adriangb adriangb deleted the allow-using-formdata-as-cm branch February 6, 2023 07:14
aminalaee pushed a commit that referenced this pull request Feb 13, 2023
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
@rodja rodja mentioned this pull request Sep 27, 2025
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants