From 412ba087271cf9a05dd6280470893e4953ff8767 Mon Sep 17 00:00:00 2001 From: lkkmpn Date: Wed, 18 Feb 2026 17:10:43 +0100 Subject: [PATCH 1/4] =?UTF-8?q?=F0=9F=93=9D=20Update=20doc=20to=20reflect?= =?UTF-8?q?=20source=20example?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/en/docs/tutorial/extra-models.md | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/docs/en/docs/tutorial/extra-models.md b/docs/en/docs/tutorial/extra-models.md index da4f8f2bda..5230f93c73 100644 --- a/docs/en/docs/tutorial/extra-models.md +++ b/docs/en/docs/tutorial/extra-models.md @@ -156,36 +156,22 @@ That way, we can declare just the differences between the models (with plaintext {* ../../docs_src/extra_models/tutorial002_py310.py hl[7,13:14,17:18,21:22] *} -## `Union` or `anyOf` { #union-or-anyof } +## Union or `anyOf` { #union-or-anyof } -You can declare a response to be the `Union` of two or more types, that means, that the response would be any of them. +You can declare a response to be the union of two or more types, that means, that the response would be any of them. It will be defined in OpenAPI with `anyOf`. -To do that, use the standard Python type hint `typing.Union`: +To do that, use the standard Python type hint union: /// note -When defining a `Union`, include the most specific type first, followed by the less specific type. In the example below, the more specific `PlaneItem` comes before `CarItem` in `Union[PlaneItem, CarItem]`. +When defining a union, include the most specific type first, followed by the less specific type. In the example below, the more specific `PlaneItem` comes before `CarItem` in `PlaneItem | CarItem`. /// {* ../../docs_src/extra_models/tutorial003_py310.py hl[1,14:15,18:20,33] *} -### `Union` in Python 3.10 { #union-in-python-3-10 } - -In this example we pass `Union[PlaneItem, CarItem]` as the value of the argument `response_model`. - -Because we are passing it as a **value to an argument** instead of putting it in a **type annotation**, we have to use `Union` even in Python 3.10. - -If it was in a type annotation we could have used the vertical bar, as: - -```Python -some_variable: PlaneItem | CarItem -``` - -But if we put that in the assignment `response_model=PlaneItem | CarItem` we would get an error, because Python would try to perform an **invalid operation** between `PlaneItem` and `CarItem` instead of interpreting that as a type annotation. - ## List of models { #list-of-models } The same way, you can declare responses of lists of objects. From 5cee11f00aa49ea1ec0c9d2455f8112571328a31 Mon Sep 17 00:00:00 2001 From: lkkmpn Date: Wed, 18 Feb 2026 17:17:53 +0100 Subject: [PATCH 2/4] Update highlighted source lines --- docs/en/docs/tutorial/extra-models.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/docs/tutorial/extra-models.md b/docs/en/docs/tutorial/extra-models.md index 5230f93c73..57f8fcf635 100644 --- a/docs/en/docs/tutorial/extra-models.md +++ b/docs/en/docs/tutorial/extra-models.md @@ -170,7 +170,7 @@ When defining a union, include the most specific type first, followed by the les /// -{* ../../docs_src/extra_models/tutorial003_py310.py hl[1,14:15,18:20,33] *} +{* ../../docs_src/extra_models/tutorial003_py310.py hl[12:13,16:18,31] *} ## List of models { #list-of-models } From 55745554096561920736ce2c884d146457de4e40 Mon Sep 17 00:00:00 2001 From: lkkmpn Date: Wed, 18 Feb 2026 18:42:34 +0100 Subject: [PATCH 3/4] Small sentence update Co-authored-by: Motov Yurii <109919500+YuriiMotov@users.noreply.github.com> --- docs/en/docs/tutorial/extra-models.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/docs/tutorial/extra-models.md b/docs/en/docs/tutorial/extra-models.md index 57f8fcf635..849f7feb0a 100644 --- a/docs/en/docs/tutorial/extra-models.md +++ b/docs/en/docs/tutorial/extra-models.md @@ -162,7 +162,7 @@ You can declare a response to be the union of two or more types, that means, tha It will be defined in OpenAPI with `anyOf`. -To do that, use the standard Python type hint union: +To do that, use the standard Python union syntax: /// note From 1eee9c096d23d9cf32974145b3e746735956b3e6 Mon Sep 17 00:00:00 2001 From: lkkmpn Date: Mon, 9 Mar 2026 10:03:13 +0100 Subject: [PATCH 4/4] Update pydantic docs url Co-authored-by: Motov Yurii <109919500+YuriiMotov@users.noreply.github.com> --- docs/en/docs/tutorial/extra-models.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/docs/tutorial/extra-models.md b/docs/en/docs/tutorial/extra-models.md index f186cdca51..e1ab504106 100644 --- a/docs/en/docs/tutorial/extra-models.md +++ b/docs/en/docs/tutorial/extra-models.md @@ -166,7 +166,7 @@ To do that, use the standard Python union syntax: /// note -When defining a union, include the most specific type first, followed by the less specific type. In the example below, the more specific `PlaneItem` comes before `CarItem` in `PlaneItem | CarItem`. +When defining a [union](https://docs.pydantic.dev/latest/concepts/unions/), include the most specific type first, followed by the less specific type. In the example below, the more specific `PlaneItem` comes before `CarItem` in `PlaneItem | CarItem`. ///