Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork8.7k
Comments
Add param functions, to override types, to make mypy happy#226
Merged
Add param functions, to override types, to make mypy happy#226
Conversation
codecovbot commentedMay 14, 2019 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Codecov Report
@@ Coverage Diff @@## master #226 +/- ##===================================== Coverage 100% 100% ===================================== Files 170 171 +1 Lines 4122 4142 +20 =====================================+ Hits 4122 4142 +20
Continue to review full report at Codecov.
|
Victor-Savu commentedMay 14, 2019
👍 This is a very nice use of Thank you for placing the emphasis on user experience! |
MemberAuthor
tiangolo commentedMay 16, 2019
Thanks@Victor-Savu ! 😄 🎉 |
lmignon pushed a commit to acsone/fastapi that referenced this pull requestSep 19, 2024
Signed-off-by simahawk
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
✨ Add param functions, to override types, to make
mypyhappy.This would solve#219 and other cases of
mypycomplaining about the default types.Simple solution
The simplest approach would be to make
mypyignore the types in those parameters, using comments with# type: ignorelike in:Drawback
But asking all the users to add comments to ignore type checks each time they use these classes would be difficult and cumbersome (for end users/developers).
Reason
The issue is that
mypyis right, from the typing point of view. Putting an instance ofQueryas the "default value" of a parameter with typestris not really correct (from the typing point of view).But here in FastAPI we are "hacking" the typing system, and extending its use to the extreme (which has proven to work really well for many users/use-cases). Thepath operation function is only called by FastAPI (not by the user or any other code), and FastAPI makes sure to pass the correct type, those classes are only for meta-information, used by FastAPI.
Alternatives
I tried overriding the type declaration of the parameter classes, but there's no way to achieve that.
Then I tried adding a typing stub (
parameters.pyi), but although it removedmypyerrors, there where other errors in places used by FastAPI internally.And then, by relaxing the type declarations to avoid the internal errors, the type hints of those parameters were also lost for final users.
There's a lot of work (and code) there just to have completion for all those parameters in classes... One of the main objectives is for FastAPI to be as comfortable for final developers as possible, even if that is at the expense of the developers of the library itself (me and the community that submits PRs 😁 ).
Final solution
Here's the solution. We can't override the type declaration of a class (the class itself).
But we can override the return type declaration of a function.
So, I converted all these parameter classes exposed by FastAPI (
Depends,Query,File, etc) to functions. But then, as the function is actually called when the code is run and FastAPI needs to inspect it afterwards when the app is running, each of these functions returns the original class instances.So, the default values of the parameters are actually those classes, and can be checked by the rest of the FastAPI code (dependencies, etc).