Skip to content

Commit

Permalink
📝 Update all docs to use Annotated as the main recommendation, with…
Browse files Browse the repository at this point in the history
… new examples and tests (#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
  • Loading branch information
tiangolo authored Mar 18, 2023
1 parent f63b3ad commit 9eaed2e
Show file tree
Hide file tree
Showing 347 changed files with 21,793 additions and 567 deletions.
38 changes: 35 additions & 3 deletions docs/en/docs/advanced/additional-status-codes.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,41 @@ But you also want it to accept new items. And when the items didn't exist before

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

```Python hl_lines="4 25"
{!../../../docs_src/additional_status_codes/tutorial001.py!}
```
=== "Python 3.6 and above"

```Python hl_lines="4 26"
{!> ../../../docs_src/additional_status_codes/tutorial001_an.py!}
```

=== "Python 3.9 and above"

```Python hl_lines="4 25"
{!> ../../../docs_src/additional_status_codes/tutorial001_an_py39.py!}
```

=== "Python 3.10 and above"

```Python hl_lines="4 25"
{!> ../../../docs_src/additional_status_codes/tutorial001_an_py310.py!}
```

=== "Python 3.6 and above - non-Annotated"

!!! tip
Try to use the main, `Annotated` version better.

```Python hl_lines="4 25"
{!> ../../../docs_src/additional_status_codes/tutorial001.py!}
```

=== "Python 3.10 and above - non-Annotated"

!!! tip
Try to use the main, `Annotated` version better.

```Python hl_lines="2 23"
{!> ../../../docs_src/additional_status_codes/tutorial001_py310.py!}
```

!!! warning
When you return a `Response` directly, like in the example above, it will be returned directly.
Expand Down
92 changes: 80 additions & 12 deletions docs/en/docs/advanced/advanced-dependencies.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,80 @@ Not the class itself (which is already a callable), but an instance of that clas

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

```Python hl_lines="10"
{!../../../docs_src/dependencies/tutorial011.py!}
```
=== "Python 3.6 and above"

```Python hl_lines="11"
{!> ../../../docs_src/dependencies/tutorial011_an.py!}
```

=== "Python 3.9 and above"

```Python hl_lines="12"
{!> ../../../docs_src/dependencies/tutorial011_an_py39.py!}
```

=== "Python 3.6 and above - non-Annotated"

!!! tip
Try to use the main, `Annotated` version better.

```Python hl_lines="10"
{!> ../../../docs_src/dependencies/tutorial011.py!}
```

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.

## Parameterize the instance

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

```Python hl_lines="7"
{!../../../docs_src/dependencies/tutorial011.py!}
```
=== "Python 3.6 and above"

```Python hl_lines="8"
{!> ../../../docs_src/dependencies/tutorial011_an.py!}
```

=== "Python 3.9 and above"

```Python hl_lines="9"
{!> ../../../docs_src/dependencies/tutorial011_an_py39.py!}
```

=== "Python 3.6 and above - non-Annotated"

!!! tip
Try to use the main, `Annotated` version better.

```Python hl_lines="7"
{!> ../../../docs_src/dependencies/tutorial011.py!}
```

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

## Create an instance

We could create an instance of this class with:

```Python hl_lines="16"
{!../../../docs_src/dependencies/tutorial011.py!}
```
=== "Python 3.6 and above"

```Python hl_lines="17"
{!> ../../../docs_src/dependencies/tutorial011_an.py!}
```

=== "Python 3.9 and above"

```Python hl_lines="18"
{!> ../../../docs_src/dependencies/tutorial011_an_py39.py!}
```

=== "Python 3.6 and above - non-Annotated"

!!! tip
Try to use the main, `Annotated` version better.

```Python hl_lines="16"
{!> ../../../docs_src/dependencies/tutorial011.py!}
```

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

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

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

```Python hl_lines="20"
{!../../../docs_src/dependencies/tutorial011.py!}
```
=== "Python 3.6 and above"

```Python hl_lines="21"
{!> ../../../docs_src/dependencies/tutorial011_an.py!}
```

=== "Python 3.9 and above"

```Python hl_lines="22"
{!> ../../../docs_src/dependencies/tutorial011_an_py39.py!}
```

=== "Python 3.6 and above - non-Annotated"

!!! tip
Try to use the main, `Annotated` version better.

```Python hl_lines="20"
{!> ../../../docs_src/dependencies/tutorial011.py!}
```

!!! tip
All this might seem contrived. And it might not be very clear how is it useful yet.
Expand Down
69 changes: 60 additions & 9 deletions docs/en/docs/advanced/security/http-basic-auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,26 @@ Then, when you type that username and password, the browser sends them in the he
* It returns an object of type `HTTPBasicCredentials`:
* It contains the `username` and `password` sent.

```Python hl_lines="2 6 10"
{!../../../docs_src/security/tutorial006.py!}
```
=== "Python 3.6 and above"

```Python hl_lines="2 7 11"
{!> ../../../docs_src/security/tutorial006_an.py!}
```

=== "Python 3.9 and above"

```Python hl_lines="4 8 12"
{!> ../../../docs_src/security/tutorial006_an_py39.py!}
```

=== "Python 3.6 and above - non-Annotated"

!!! tip
Try to use the main, `Annotated` version better.

```Python hl_lines="2 6 10"
{!> ../../../docs_src/security/tutorial006.py!}
```

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:

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

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

```Python hl_lines="1 11-21"
{!../../../docs_src/security/tutorial007.py!}
```
=== "Python 3.6 and above"

```Python hl_lines="1 12-24"
{!> ../../../docs_src/security/tutorial007_an.py!}
```

=== "Python 3.9 and above"

```Python hl_lines="1 12-24"
{!> ../../../docs_src/security/tutorial007_an_py39.py!}
```

=== "Python 3.6 and above - non-Annotated"

!!! tip
Try to use the main, `Annotated` version better.

```Python hl_lines="1 11-21"
{!> ../../../docs_src/security/tutorial007.py!}
```

This would be similar to:

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

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:

```Python hl_lines="23-27"
{!../../../docs_src/security/tutorial007.py!}
```
=== "Python 3.6 and above"

```Python hl_lines="26-30"
{!> ../../../docs_src/security/tutorial007_an.py!}
```

=== "Python 3.9 and above"

```Python hl_lines="26-30"
{!> ../../../docs_src/security/tutorial007_an_py39.py!}
```

=== "Python 3.6 and above - non-Annotated"

!!! tip
Try to use the main, `Annotated` version better.

```Python hl_lines="23-27"
{!> ../../../docs_src/security/tutorial007.py!}
```
Loading

0 comments on commit 9eaed2e

Please sign in to comment.