Skip to content
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

Merged
merged 11 commits into from Feb 6, 2023

Conversation

adriangb
Copy link
Member

@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 reopened this Oct 6, 2022
@adriangb adriangb requested a review from tiangolo October 6, 2022 15:52
Copy link
Sponsor Member

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

@@ -265,8 +267,11 @@ async def form(self) -> FormData:
self._form = FormData()
return self._form

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

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
Member 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
@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
Sponsor Member

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
Sponsor Member

@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
Sponsor Member

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

docs/requests.md Outdated Show resolved Hide resolved
@Kludex Kludex mentioned this pull request Feb 5, 2023
7 tasks
@adriangb adriangb enabled auto-merge (squash) February 5, 2023 21:52
starlette/_utils.py Outdated Show resolved Hide resolved
@adriangb adriangb merged commit c568b55 into encode: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>
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.

None yet

4 participants