Skip to content

Commit d240421

Browse files
authored
📝 Add docs about params as functions for mypy (fastapi#231)
1 parent ca27317 commit d240421

File tree

7 files changed

+29
-7
lines changed

7 files changed

+29
-7
lines changed

docs/release-notes.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## Next release
22

3+
* Make default parameter utilities exported from `fastapi` be functions instead of classes (the new functions return instances of those classes). To be able to override the return types and fix `mypy` errors in FastAPI's users' code. Applies to `Path`, `Query`, `Header`, `Cookie`, `Body`, `Form`, `File`, `Depends`, and `Security`. PR [#226](https://github.com/tiangolo/fastapi/pull/226).
4+
35
* Re-enable `black` formatting checks for Python 3.7. PR [#229](https://github.com/tiangolo/fastapi/pull/229) by [@zamiramir](https://github.com/zamiramir).
46

57
## 0.21.0

docs/tutorial/body-schema.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ You can then use `Schema` with model attributes:
2222

2323
`Schema` works the same way as `Query`, `Path` and `Body`, it has all the same parameters, etc.
2424

25-
26-
!!! info
25+
!!! note "Technical Details"
2726
Actually, `Query`, `Path` and others you'll see next are subclasses of a common `Param` which is itself a subclass of Pydantic's `Schema`.
2827

2928
`Body` is also a subclass of `Schema` directly. And there are others you will see later that are subclasses of `Body`.
3029

30+
But remember that when you import `Query`, `Path` and others from `fastapi`, <a href="https://fastapi.tiangolo.com/tutorial/path-params-numeric-validations/#recap" target="_blank">those are actually functions that return classes of the same name</a>.
31+
3132
!!! tip
3233
Notice how each model's attribute with a type, default value and `Schema` has the same structure as a path operation function's parameter, with `Schema` instead of `Path`, `Query` and `Body`.
3334

docs/tutorial/cookie-params.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ The first value is the default value, you can pass all the extra validation or a
1818
{!./src/cookie_params/tutorial001.py!}
1919
```
2020

21-
!!! info
21+
!!! note "Technical Details"
2222
`Cookie` is a "sister" class of `Path` and `Query`. It also inherits from the same common `Param` class.
2323

24+
But remember that when you import `Query`, `Path`, `Cookie` and others from `fastapi`, <a href="https://fastapi.tiangolo.com/tutorial/path-params-numeric-validations/#recap" target="_blank">those are actually functions that return classes of the same name</a>.
25+
2426
!!! info
2527
To declare cookies, you need to use `Cookie`, because otherwise the parameters would be interpreted as query parameters.
2628

docs/tutorial/header-params.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ The first value is the default value, you can pass all the extra validation or a
1818
{!./src/header_params/tutorial001.py!}
1919
```
2020

21-
!!! info
21+
!!! note "Technical Details"
2222
`Header` is a "sister" class of `Path`, `Query` and `Cookie`. It also inherits from the same common `Param` class.
2323

24+
But remember that when you import `Query`, `Path`, `Header`, and others from `fastapi`, <a href="https://fastapi.tiangolo.com/tutorial/path-params-numeric-validations/#recap" target="_blank">those are actually functions that return classes of the same name</a>.
25+
2426
!!! info
2527
To declare headers, you need to use `Header`, because otherwise the parameters would be interpreted as query parameters.
2628

docs/tutorial/path-params-numeric-validations.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,17 @@ And you can also declare numeric validations:
103103
* `le`: `l`ess than or `e`qual
104104

105105
!!! info
106-
`Query`, `Path` and others you will see later are subclasses of a common `Param` class (that you don't need to use).
107-
108-
And all of them share the same all these same parameters of additional validation and metadata you have seen.
106+
`Query`, `Path` and others you will see later subclasses of a common `Param` class (that you don't need to use).
107+
108+
And all of them share the same all these same parameters of additional validation and metadata you have seen.
109+
110+
!!! note "Technical Details"
111+
When you import `Query`, `Path` and others from `fastapi`, they are actually functions.
112+
113+
That when called, return instances of classes of the same name.
114+
115+
So, you import `Query`, which is a function. And when you call it, it returns an instance of a class also named `Query`.
116+
117+
These functions are there (instead of just using the classes directly) so that your editor doesn't mark errors about their types.
118+
119+
That way you can use your normal editor and coding tools without having to add custom configurations to disregard those errors.

docs/tutorial/request-files.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Create file parameters the same way you would for `Body` or `Form`:
1919
!!! info
2020
`File` is a class that inherits directly from `Form`.
2121

22+
But remember that when you import `Query`, `Path`, `File` and others from `fastapi`, <a href="https://fastapi.tiangolo.com/tutorial/path-params-numeric-validations/#recap" target="_blank">those are actually functions that return classes of the same name</a>.
23+
2224
!!! info
2325
To declare File bodies, you need to use `File`, because otherwise the parameters would be interpreted as query parameters or body (JSON) parameters.
2426

docs/tutorial/security/oauth2-scopes.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ In this case, it requires the scope `me` (it could require more than one scope).
108108

109109
But by using `Security` instead of `Depends`, **FastAPI** will know that it can declare security scopes, use them internally, and document the API with OpenAPI.
110110

111+
But when you import `Query`, `Path`, `Depends`, `Security` and others from `fastapi`, <a href="https://fastapi.tiangolo.com/tutorial/path-params-numeric-validations/#recap" target="_blank">those are actually functions that return classes of the same name</a>.
112+
111113
## Use `SecurityScopes`
112114

113115
Now update the dependency `get_current_user`.

0 commit comments

Comments
 (0)