Skip to content

🐛 Ignore Response classes on return annotation - #5855

Merged
tiangolo merged 6 commits into
fastapi:masterfrom
Kludex:fix/return-type-on-response-class
Jan 10, 2023
Merged

tiangolo merged 6 commits into
fastapi:masterfrom
Kludex:fix/return-type-on-response-class

Conversation

@Kludex

@Kludex Kludex commented Jan 8, 2023 •

Copy link
Copy Markdown
Contributor

@github-actions

github-actions Bot commented Jan 8, 2023

Copy link
Copy Markdown
Contributor

📝 Docs preview for commit bcde7e9 at: https://63ba7ea82674445278edeab6--fastapi.netlify.app

@Kludex
Kludex force-pushed the fix/return-type-on-response-class branch from bcde7e9 to 7ef60c7 Compare January 8, 2023 08:52
@Kludex
Kludex force-pushed the fix/return-type-on-response-class branch from 7ef60c7 to cae4af5 Compare January 8, 2023 08:52
@github-actions

github-actions Bot commented Jan 8, 2023

Copy link
Copy Markdown
Contributor

📝 Docs preview for commit cae4af5 at: https://63ba85339a71f156a9041f93--fastapi.netlify.app

Comment thread fastapi/routing.py Resolved
@github-actions

Copy link
Copy Markdown
Contributor

📝 Docs preview for commit 324bfce at: https://63bd4143d3db0f08a0b3e678--fastapi.netlify.app

@Kludex Kludex left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think the best approach would be to not accept this, revert to the previous release, and with more time, release the feature again with more time to think about the corner cases. 👀

Comment thread fastapi/routing.py
Comment on lines +362 to +379
return_annotation = get_typed_return_annotation(endpoint)
if get_origin(return_annotation) is Union:
union_args = get_args(return_annotation)
args = tuple(arg for arg in union_args if not issubclass(arg, Response))
if len(args) > 1:
response_model = Union[args]
elif len(args) == 1:
response_model = args[0]
else:
response_model = None
elif issubclass(return_annotation, Response):
response_model = None
else:
response_model = return_annotation

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Those are the cases we talked about:

  1. When there's a single Response class response_model becomes None.
  2. If it's an Union, we filter the Response classes. (I'm not sure if this is what we want ???)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah, this gets quite complicated. Also because a Union could contain internal Unions, etc.

I think just the lenient_issubclass(return_annotation, Response), to cover the simplest use cases.

And then I'll add a bit to the docs explaining that if the user wants to return and annotate something different than a Pydantic field, it needs a response_model=None.

@Kludex
Kludex force-pushed the fix/return-type-on-response-class branch from 324bfce to 920f95e Compare January 10, 2023 10:48
@Kludex
Kludex force-pushed the fix/return-type-on-response-class branch from 920f95e to f612ac1 Compare January 10, 2023 10:49
@github-actions

Copy link
Copy Markdown
Contributor

📝 Docs preview for commit f612ac1 at: https://63bd4390143ac804b98c706a--fastapi.netlify.app

Comment thread fastapi/routing.py
Comment on lines +362 to +379
return_annotation = get_typed_return_annotation(endpoint)
if get_origin(return_annotation) is Union:
union_args = get_args(return_annotation)
args = tuple(arg for arg in union_args if not issubclass(arg, Response))
if len(args) > 1:
response_model = Union[args]
elif len(args) == 1:
response_model = args[0]
else:
response_model = None
elif issubclass(return_annotation, Response):
response_model = None
else:
response_model = return_annotation

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah, this gets quite complicated. Also because a Union could contain internal Unions, etc.

I think just the lenient_issubclass(return_annotation, Response), to cover the simplest use cases.

And then I'll add a bit to the docs explaining that if the user wants to return and annotate something different than a Pydantic field, it needs a response_model=None.

Comment thread fastapi/routing.py Resolved
@tiangolo

Copy link
Copy Markdown
Member

Thank you!

The other thing that could be done later would be to update the error warning about this. But I can probably do that in another PR later.

@tiangolo tiangolo changed the title Ignore Response classes on return annotation 🐛 Ignore Response classes on return annotation Jan 10, 2023

@tiangolo tiangolo left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Great, thanks! 🚀 🍰 ☕

@tiangolo
tiangolo enabled auto-merge (squash) January 10, 2023 12:43
@tiangolo
tiangolo merged commit fba7493 into fastapi:master Jan 10, 2023
@Kludex
Kludex deleted the fix/return-type-on-response-class branch January 10, 2023 12:46
@tiangolo tiangolo mentioned this pull request Jan 10, 2023
9 tasks done
JonasKs pushed a commit to JonasKs/fastapi that referenced this pull request Jan 12, 2023
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
nickstenning added a commit to replicate/cog that referenced this pull request Jan 31, 2023
FastAPI 0.89.0 has a bug which causes the model to fail to start if
handlers annotate a response class of Response (or its subclasses).

See: fastapi/fastapi#5855
nickstenning added a commit to replicate/cog that referenced this pull request Jan 31, 2023
FastAPI 0.89.0 has a bug which causes the model to fail to start if
handlers annotate a response class of Response (or its subclasses).

See: fastapi/fastapi#5855
Signed-off-by: Nick Stenning <nick@whiteink.com>
nickstenning added a commit to replicate/cog that referenced this pull request Jan 31, 2023
FastAPI 0.89.0 has a bug which causes the model to fail to start if
handlers annotate a response class of Response (or its subclasses).

See: fastapi/fastapi#5855
Signed-off-by: Nick Stenning <nick@whiteink.com>
nickstenning added a commit to replicate/cog that referenced this pull request Jan 31, 2023
FastAPI 0.89.0 has a bug which causes the model to fail to start if
handlers annotate a response class of Response (or its subclasses).

See: fastapi/fastapi#5855
Signed-off-by: Nick Stenning <nick@whiteink.com>
axel584 pushed a commit to axel584/fastapi that referenced this pull request Mar 5, 2023
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
mahenzon pushed a commit to mahenzon/fastapi that referenced this pull request Mar 11, 2023
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Jupev6 added a commit to Jupev6/cog that referenced this pull request Aug 11, 2024
FastAPI 0.89.0 has a bug which causes the model to fail to start if
handlers annotate a response class of Response (or its subclasses).

See: fastapi/fastapi#5855
Signed-off-by: Nick Stenning <nick@whiteink.com>
apollo0202 pushed a commit to apollo0202/doc that referenced this pull request Feb 28, 2025
FastAPI 0.89.0 has a bug which causes the model to fail to start if
handlers annotate a response class of Response (or its subclasses).

See: fastapi/fastapi#5855
Signed-off-by: Nick Stenning <nick@whiteink.com>
richardokonicha pushed a commit to fugoku/cog that referenced this pull request Oct 23, 2026
FastAPI 0.89.0 has a bug which causes the model to fail to start if
handlers annotate a response class of Response (or its subclasses).

See: fastapi/fastapi#5855
Signed-off-by: Nick Stenning <nick@whiteink.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.

4 participants