Skip to content

Commit 9eaed2e

Browse files
authored
📝 Update all docs to use Annotated as the main recommendation, with new examples and tests (fastapi#9268)
* 🍱 Add new source examples with Annotated for Query Params and String Validations * 📝 Add new docs with Annotated for Query Params and String Validations * 🚚 Rename incorrectly named tests for Query Params and str validations * ✅ Add new tests with Annotated for Query Params and Sring Validations examples * 🍱 Add new examples with Annotated for Intro to Python Types * 📝 Update Python Types Intro, include Annotated * 🎨 Fix formatting in Query params and string validation, and highlight * 🍱 Add new Annotated source examples for Path Params and Numeric Validations * 📝 Update docs for Path Params and Numeric Validations with Annotated * 🍱 Add new source examples with Annotated for Body - Multiple Params * 📝 Update docs with Annotated for Body - Multiple Parameters * ✅ Add test for new Annotated examples in Body - Multiple Parameters * 🍱 Add new Annotated source examples for Body Fields * 📝 Update docs for Body Fields with new Annotated examples * ✅ Add new tests for new Annotated examples for Body Fields * 🍱 Add new Annotated source examples for Schema Extra (Example Data) * 📝 Update docs for Schema Extra with Annotated * ✅ Add tests for new Annotated examples for Schema Extra * 🍱 Add new Annnotated source examples for Extra Data Types * 📝 Update docs with Annotated for Extra Data Types * ✅ Add tests for new Annotated examples for Extra Data Types * 🍱 Add new Annotated source examples for Cookie Parameters * 📝 Update docs for Cookie Parameters with Annotated examples * ✅ Add tests for new Annotated source examples in Cookie Parameters * 🍱 Add new Annotated examples for Header Params * 📝 Update docs with Annotated examples for Header Parameters * ✅ Add tests for new Annotated examples for Header Params * 🍱 Add new Annotated examples for Form Data * 📝 Update Annotated docs for Form Data * ✅ Add tests for new Annotated examples in Form Data * 🍱 Add new Annotated source examples for Request Files * 📝 Update Annotated docs for Request Files * ✅ Test new Annotated examples for Request Files * 🍱 Add new Annotated source examples for Request Forms and Files * ✅ Add tests for new Anotated examples for Request Forms and Files * 🍱 Add new Annotated source examples for Dependencies and Advanced Dependencies * ✅ Add tests for new Annotated dependencies * 📝 Add new docs for using Annotated with dependencies including type aliases * 📝 Update docs for Classes as Dependencies with Annotated * 📝 Update docs for Sub-dependencies with Annotated * 📝 Update docs for Dependencies in path operation decorators with Annotated * 📝 Update docs for Global Dependencies with Annotated * 📝 Update docs for Dependencies with yield with Annotated * 🎨 Update format in example for dependencies with Annotated * 🍱 Add source examples with Annotated for Security * ✅ Add tests for new Annotated examples for security * 📝 Update docs for Security - First Steps with Annotated * 📝 Update docs for Security: Get Current User with Annotated * 📝 Update docs for Simple OAuth2 with Password and Bearer with Annotated * 📝 Update docs for OAuth2 with Password (and hashing), Bearer with JWT tokens with Annotated * 📝 Update docs for Request Forms and Files with Annotated * 🍱 Add new source examples for Bigger Applications with Annotated * ✅ Add new tests for Bigger Applications with Annotated * 📝 Update docs for Bigger Applications - Multiple Files with Annotated * 🍱 Add source examples for background tasks with Annotated * 📝 Update docs for Background Tasks with Annotated * ✅ Add test for Background Tasks with Anotated * 🍱 Add new source examples for docs for Testing with Annotated * 📝 Update docs for Testing with Annotated * ✅ Add tests for Annotated examples for Testing * 🍱 Add new source examples for Additional Status Codes with Annotated * ✅ Add tests for new Annotated examples for Additional Status Codes * 📝 Update docs for Additional Status Codes with Annotated * 📝 Update docs for Advanced Dependencies with Annotated * 📝 Update docs for OAuth2 scopes with Annotated * 📝 Update docs for HTTP Basic Auth with Annotated * 🍱 Add source examples with Annotated for WebSockets * ✅ Add tests for new Annotated examples for WebSockets * 📝 Update docs for WebSockets with new Annotated examples * 🍱 Add source examples with Annotated for Settings and Environment Variables * 📝 Update docs for Settings and Environment Variables with Annotated * 🍱 Add new source examples for testing dependencies with Annotated * ✅ Add tests for new examples for testing dependencies * 📝 Update docs for testing dependencies with new Annotated examples * ✅ Update and fix marker for Python 3.9 test * 🔧 Update Ruff ignores for source examples in docs * ✅ Fix some tests in the grid for Python 3.9 (incorrectly testing 3.10) * 🔥 Remove source examples and tests for (non existent) docs section about Annotated, as it's covered in all the rest of the docs
1 parent f63b3ad commit 9eaed2e

File tree

347 files changed

+21793
-567
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

347 files changed

+21793
-567
lines changed

docs/en/docs/advanced/additional-status-codes.md

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,41 @@ But you also want it to accept new items. And when the items didn't exist before
1414

1515
To achieve that, import `JSONResponse`, and return your content there directly, setting the `status_code` that you want:
1616

17-
```Python hl_lines="4 25"
18-
{!../../../docs_src/additional_status_codes/tutorial001.py!}
19-
```
17+
=== "Python 3.6 and above"
18+
19+
```Python hl_lines="4 26"
20+
{!> ../../../docs_src/additional_status_codes/tutorial001_an.py!}
21+
```
22+
23+
=== "Python 3.9 and above"
24+
25+
```Python hl_lines="4 25"
26+
{!> ../../../docs_src/additional_status_codes/tutorial001_an_py39.py!}
27+
```
28+
29+
=== "Python 3.10 and above"
30+
31+
```Python hl_lines="4 25"
32+
{!> ../../../docs_src/additional_status_codes/tutorial001_an_py310.py!}
33+
```
34+
35+
=== "Python 3.6 and above - non-Annotated"
36+
37+
!!! tip
38+
Try to use the main, `Annotated` version better.
39+
40+
```Python hl_lines="4 25"
41+
{!> ../../../docs_src/additional_status_codes/tutorial001.py!}
42+
```
43+
44+
=== "Python 3.10 and above - non-Annotated"
45+
46+
!!! tip
47+
Try to use the main, `Annotated` version better.
48+
49+
```Python hl_lines="2 23"
50+
{!> ../../../docs_src/additional_status_codes/tutorial001_py310.py!}
51+
```
2052

2153
!!! warning
2254
When you return a `Response` directly, like in the example above, it will be returned directly.

docs/en/docs/advanced/advanced-dependencies.md

Lines changed: 80 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,80 @@ Not the class itself (which is already a callable), but an instance of that clas
1818

1919
To do that, we declare a method `__call__`:
2020

21-
```Python hl_lines="10"
22-
{!../../../docs_src/dependencies/tutorial011.py!}
23-
```
21+
=== "Python 3.6 and above"
22+
23+
```Python hl_lines="11"
24+
{!> ../../../docs_src/dependencies/tutorial011_an.py!}
25+
```
26+
27+
=== "Python 3.9 and above"
28+
29+
```Python hl_lines="12"
30+
{!> ../../../docs_src/dependencies/tutorial011_an_py39.py!}
31+
```
32+
33+
=== "Python 3.6 and above - non-Annotated"
34+
35+
!!! tip
36+
Try to use the main, `Annotated` version better.
37+
38+
```Python hl_lines="10"
39+
{!> ../../../docs_src/dependencies/tutorial011.py!}
40+
```
2441

2542
In this case, this `__call__` is what **FastAPI** will use to check for additional parameters and sub-dependencies, and this is what will be called to pass a value to the parameter in your *path operation function* later.
2643

2744
## Parameterize the instance
2845

2946
And now, we can use `__init__` to declare the parameters of the instance that we can use to "parameterize" the dependency:
3047

31-
```Python hl_lines="7"
32-
{!../../../docs_src/dependencies/tutorial011.py!}
33-
```
48+
=== "Python 3.6 and above"
49+
50+
```Python hl_lines="8"
51+
{!> ../../../docs_src/dependencies/tutorial011_an.py!}
52+
```
53+
54+
=== "Python 3.9 and above"
55+
56+
```Python hl_lines="9"
57+
{!> ../../../docs_src/dependencies/tutorial011_an_py39.py!}
58+
```
59+
60+
=== "Python 3.6 and above - non-Annotated"
61+
62+
!!! tip
63+
Try to use the main, `Annotated` version better.
64+
65+
```Python hl_lines="7"
66+
{!> ../../../docs_src/dependencies/tutorial011.py!}
67+
```
3468

3569
In this case, **FastAPI** won't ever touch or care about `__init__`, we will use it directly in our code.
3670

3771
## Create an instance
3872

3973
We could create an instance of this class with:
4074

41-
```Python hl_lines="16"
42-
{!../../../docs_src/dependencies/tutorial011.py!}
43-
```
75+
=== "Python 3.6 and above"
76+
77+
```Python hl_lines="17"
78+
{!> ../../../docs_src/dependencies/tutorial011_an.py!}
79+
```
80+
81+
=== "Python 3.9 and above"
82+
83+
```Python hl_lines="18"
84+
{!> ../../../docs_src/dependencies/tutorial011_an_py39.py!}
85+
```
86+
87+
=== "Python 3.6 and above - non-Annotated"
88+
89+
!!! tip
90+
Try to use the main, `Annotated` version better.
91+
92+
```Python hl_lines="16"
93+
{!> ../../../docs_src/dependencies/tutorial011.py!}
94+
```
4495

4596
And that way we are able to "parameterize" our dependency, that now has `"bar"` inside of it, as the attribute `checker.fixed_content`.
4697

@@ -56,9 +107,26 @@ checker(q="somequery")
56107

57108
...and pass whatever that returns as the value of the dependency in our *path operation function* as the parameter `fixed_content_included`:
58109

59-
```Python hl_lines="20"
60-
{!../../../docs_src/dependencies/tutorial011.py!}
61-
```
110+
=== "Python 3.6 and above"
111+
112+
```Python hl_lines="21"
113+
{!> ../../../docs_src/dependencies/tutorial011_an.py!}
114+
```
115+
116+
=== "Python 3.9 and above"
117+
118+
```Python hl_lines="22"
119+
{!> ../../../docs_src/dependencies/tutorial011_an_py39.py!}
120+
```
121+
122+
=== "Python 3.6 and above - non-Annotated"
123+
124+
!!! tip
125+
Try to use the main, `Annotated` version better.
126+
127+
```Python hl_lines="20"
128+
{!> ../../../docs_src/dependencies/tutorial011.py!}
129+
```
62130

63131
!!! tip
64132
All this might seem contrived. And it might not be very clear how is it useful yet.

docs/en/docs/advanced/security/http-basic-auth.md

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,26 @@ Then, when you type that username and password, the browser sends them in the he
2020
* It returns an object of type `HTTPBasicCredentials`:
2121
* It contains the `username` and `password` sent.
2222

23-
```Python hl_lines="2 6 10"
24-
{!../../../docs_src/security/tutorial006.py!}
25-
```
23+
=== "Python 3.6 and above"
24+
25+
```Python hl_lines="2 7 11"
26+
{!> ../../../docs_src/security/tutorial006_an.py!}
27+
```
28+
29+
=== "Python 3.9 and above"
30+
31+
```Python hl_lines="4 8 12"
32+
{!> ../../../docs_src/security/tutorial006_an_py39.py!}
33+
```
34+
35+
=== "Python 3.6 and above - non-Annotated"
36+
37+
!!! tip
38+
Try to use the main, `Annotated` version better.
39+
40+
```Python hl_lines="2 6 10"
41+
{!> ../../../docs_src/security/tutorial006.py!}
42+
```
2643

2744
When you try to open the URL for the first time (or click the "Execute" button in the docs) the browser will ask you for your username and password:
2845

@@ -42,9 +59,26 @@ To handle that, we first convert the `username` and `password` to `bytes` encodi
4259

4360
Then we can use `secrets.compare_digest()` to ensure that `credentials.username` is `"stanleyjobson"`, and that `credentials.password` is `"swordfish"`.
4461

45-
```Python hl_lines="1 11-21"
46-
{!../../../docs_src/security/tutorial007.py!}
47-
```
62+
=== "Python 3.6 and above"
63+
64+
```Python hl_lines="1 12-24"
65+
{!> ../../../docs_src/security/tutorial007_an.py!}
66+
```
67+
68+
=== "Python 3.9 and above"
69+
70+
```Python hl_lines="1 12-24"
71+
{!> ../../../docs_src/security/tutorial007_an_py39.py!}
72+
```
73+
74+
=== "Python 3.6 and above - non-Annotated"
75+
76+
!!! tip
77+
Try to use the main, `Annotated` version better.
78+
79+
```Python hl_lines="1 11-21"
80+
{!> ../../../docs_src/security/tutorial007.py!}
81+
```
4882

4983
This would be similar to:
5084

@@ -108,6 +142,23 @@ That way, using `secrets.compare_digest()` in your application code, it will be
108142

109143
After detecting that the credentials are incorrect, return an `HTTPException` with a status code 401 (the same returned when no credentials are provided) and add the header `WWW-Authenticate` to make the browser show the login prompt again:
110144

111-
```Python hl_lines="23-27"
112-
{!../../../docs_src/security/tutorial007.py!}
113-
```
145+
=== "Python 3.6 and above"
146+
147+
```Python hl_lines="26-30"
148+
{!> ../../../docs_src/security/tutorial007_an.py!}
149+
```
150+
151+
=== "Python 3.9 and above"
152+
153+
```Python hl_lines="26-30"
154+
{!> ../../../docs_src/security/tutorial007_an_py39.py!}
155+
```
156+
157+
=== "Python 3.6 and above - non-Annotated"
158+
159+
!!! tip
160+
Try to use the main, `Annotated` version better.
161+
162+
```Python hl_lines="23-27"
163+
{!> ../../../docs_src/security/tutorial007.py!}
164+
```

0 commit comments

Comments
 (0)