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
Define strict return value typing for Depends and Security #11255
base: master
Are you sure you want to change the base?
Conversation
e365b6e
to
a910d87
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's a good fix!
I would only change the title and description to make things clearer.
In fact, this fix does not cause FastAPI to show any errors if the returned dependency type does not match the parameter type. But this fix allows you to find this error using mypy
.
With this fix, 'mypy' will show error: Incompatible default for argument "value" (default has type "str", argument has type "int") [assignment]
.
UPD: why don't you add the same to the Security
as well?
I didn't touch Edit: Made the change. |
The checks seem to be stuck, can someone give them a nudge? |
Depends can be used with a direct form, which although not recommended, is still supported. The following code is problematic wrt. type checking. def dependency() -> str: return "dependency" @router.get("/") def endpoint(value: int = Depends(dependency)): pass Currently, `Depends(dependency)` returns Any, which then happily gets assigned to the int. This patch changes it to return a type matching what the dependency returns, making the above code fail type checking with mypy as it should.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good useful fix!
Good fix! Hoping this gets merged soon... |
Depends can be used with a direct form, which although not recommended, is still supported. The following code is problematic wrt. type checking.
Currently,
Depends(dependency)
returns Any, which then happily gets assigned to the int. This patch changes it to return a type matching what the dependency returns, making the above code fail type checking with mypy as it should.