fastapi icon indicating copy to clipboard operation
fastapi copied to clipboard

fix: allow None as return type for http 204

Open hofrob opened this issue 3 years ago • 2 comments

Fixes #9424

If the return annotation of the route function is exactly "None", return None for the typed annotation. This allows the status code 204 No Content to be used.

hofrob avatar Apr 21 '23 14:04 hofrob

📝 Docs preview for commit 243f74ac0b87a5f24a6823144e951f34d8782946 at: https://6442a49fa75498079da29ef3--fastapi.netlify.app

github-actions[bot] avatar Apr 21 '23 14:04 github-actions[bot]

📝 Docs preview for commit bb5ef4abbc8865a51fab0236503695b30a1ce3b3 at: https://647f33cb7357732441e2ff02--fastapi.netlify.app

github-actions[bot] avatar Jun 06 '23 13:06 github-actions[bot]

I tested this using pip install git+https://github.com/hofrob/fastapi@fix-return-no-content-future-annotations and it fixes my issue in #9723. :+1:

fnep avatar Jun 24 '23 04:06 fnep

@csbasil I rebased after you approved it.

Are there any plans on merging this PR?

hofrob avatar Jul 18 '23 10:07 hofrob

There's an alternative to this on https://github.com/tiangolo/fastapi/pull/5860.

Kludex avatar Jul 18 '23 19:07 Kludex

There's an alternative to this on https://github.com/tiangolo/fastapi/pull/5860.

Yes, I thought it's not necessary to get the actual type in this case, since None is a reserved word anyway.

It might be a bit faster to compare the string further up in the stack, but that's probably negligible.

There may be other arguments about using one over the other.

hofrob avatar Jul 18 '23 19:07 hofrob