Skip to content

Conversation

@mlb-
Copy link

@mlb- mlb- commented Apr 11, 2025

To start, thank you all for FastAPI! It's been a a joy to use FastAPI at $DAYJOB.

The problem

While writing a unit test for an endpoint that handles a file, I stumbled across a discrepancy in what FastAPI documentation describes and what actually gets passed to the handler (a starlette.datastructures.UploadFile via starlette.requests.Request.form).

The coding standard at $DAYJOB includes typeguard (via the pytest-typeguard plugin). In the unit test, typeguard enforcement fails because the upload file was not a FastAPI instance, but a Starlette instance. (Of course, bypassing typeguard allows the tests to pass, as expected.)

typeguard.TypeCheckError: argument "file" (starlette.datastructures.UploadFile) is not an instance of fastapi.datastructures.UploadFile

A quick look at the implementation reveals the UploadFile creation is handled by starlette.requests and then passed to FastAPI's run_function_endpoint, without any transforms to a fastapi.datastructures.UploadFile. Thankfully, in practice, this doesn't appear to be an issue at runtime, since the FastAPI UploadFile merely extends the Starlette UploadFile to add Pydantic support.

A solution

  • An UploadFile.from_starlette classmethod is added to transform from a StarletteUploadFile to Starlette's UploadFile. FastAPI is already aware of the Starlette UploadFile implementation because it's extending it, so adding the transform to the FastAPI class felt like adding a missing puzzle piece
  • This can be applied in fastapi.routing.run_endpoint_function to check the values dict for non-FastAPI UploadFiles and convert them prior to handling off to any endpoint function
  • Finally, a quick test that doesn't require typeguard to demonstrate the type signature is consistent

Thanks again!

I look forward to feedback!

@mlb- mlb- force-pushed the fix/uploadfile-downcast branch 3 times, most recently from 45cc706 to 0b51a5e Compare April 11, 2025 21:41
@svlandeg svlandeg changed the title Fix: Ensure handlers receive fastapi.UploadFile instances, not Starlette's 🐛 Ensure handlers receive fastapi.UploadFile instances, not Starlette's Apr 17, 2025
@svlandeg svlandeg added the bug Something isn't working label Apr 17, 2025
@svlandeg

This comment was marked as resolved.

@mlb- mlb- force-pushed the fix/uploadfile-downcast branch 2 times, most recently from f181f15 to a27771d Compare April 18, 2025 01:17
@mlb-

This comment was marked as resolved.

@mlb- mlb- force-pushed the fix/uploadfile-downcast branch from a27771d to 5b820bd Compare April 22, 2025 17:52
@TheCaffinatedDeveloper
Copy link

This is great! I need this fix too actually - @svlandeg @mlb- can we get this merged?

@mlb- mlb- force-pushed the fix/uploadfile-downcast branch from 5b820bd to 4e49249 Compare April 28, 2025 21:56
@mlb- mlb- force-pushed the fix/uploadfile-downcast branch from 4e49249 to 06ec400 Compare May 11, 2025 23:21
@mlb- mlb- force-pushed the fix/uploadfile-downcast branch 2 times, most recently from 56e5dc7 to cbc18c6 Compare May 23, 2025 19:29
@mlb- mlb- force-pushed the fix/uploadfile-downcast branch from cbc18c6 to f9ad724 Compare June 2, 2025 18:43
@YuriiMotov

This comment was marked as resolved.

@mlb- mlb- force-pushed the fix/uploadfile-downcast branch from f9ad724 to 687442f Compare June 11, 2025 22:52
@mlb-

This comment was marked as resolved.

@mlb- mlb- force-pushed the fix/uploadfile-downcast branch from 687442f to 6a7d0fb Compare June 17, 2025 20:37
@YuriiMotov

This comment was marked as resolved.

@mlb- mlb- force-pushed the fix/uploadfile-downcast branch from 6a7d0fb to 1293cac Compare June 24, 2025 21:58
@mlb-

This comment was marked as resolved.

@mlb- mlb- force-pushed the fix/uploadfile-downcast branch from 1293cac to 7e29c99 Compare July 7, 2025 17:12
@mlb- mlb- force-pushed the fix/uploadfile-downcast branch from 7e29c99 to 480b267 Compare July 14, 2025 19:43
@mlb- mlb- force-pushed the fix/uploadfile-downcast branch from 480b267 to bc9ad12 Compare July 21, 2025 20:45
@mlb-
Copy link
Author

mlb- commented Jul 21, 2025

@tiangolo: Do you think this is an easy enough bugfix you could merge?

@mlb- mlb- force-pushed the fix/uploadfile-downcast branch from bc9ad12 to ecfbcc2 Compare September 26, 2025 20:19
YuriiMotov

This comment was marked as outdated.

@github-actions

This comment was marked as outdated.

@github-actions

This comment was marked as outdated.

@github-actions github-actions bot closed this Nov 20, 2025
@YuriiMotov YuriiMotov reopened this Nov 24, 2025
Copy link
Member

@YuriiMotov YuriiMotov left a comment

Choose a reason for hiding this comment

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

LGTM!

@github-actions github-actions bot removed the waiting label Nov 24, 2025
@github-actions github-actions bot added the conflicts Automatically generated when a PR has a merge conflict label Dec 4, 2025
@github-actions

This comment was marked as resolved.

@github-actions github-actions bot removed the conflicts Automatically generated when a PR has a merge conflict label Dec 5, 2025
@github-actions github-actions bot added the conflicts Automatically generated when a PR has a merge conflict label Dec 17, 2025
@github-actions
Copy link
Contributor

This pull request has a merge conflict that needs to be resolved.

@github-actions github-actions bot removed the conflicts Automatically generated when a PR has a merge conflict label Jan 8, 2026
@YuriiMotov YuriiMotov force-pushed the fix/uploadfile-downcast branch from 79f225f to 2234be9 Compare January 8, 2026 13:03
@codspeed-hq
Copy link

codspeed-hq bot commented Jan 8, 2026

Merging this PR will not alter performance

Summary

✅ 20 untouched benchmarks
⏩ 40 skipped benchmarks1


Comparing mlb-:fix/uploadfile-downcast (8dd0d21) with master (f2687dc)

Open in CodSpeed

Footnotes

  1. 40 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants