From 8f491af462efb6ea1c83259c7d99f8365f06fb34 Mon Sep 17 00:00:00 2001 From: LoliPix Date: Wed, 20 May 2026 14:10:40 +0000 Subject: [PATCH] Update documentation on type unions in Python Clarify type union usage in Python 3.10 and later, and explain the error in earlier versions. --- docs/en/docs/tutorial/extra-models.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/en/docs/tutorial/extra-models.md b/docs/en/docs/tutorial/extra-models.md index e231aea90a..88d6156d34 100644 --- a/docs/en/docs/tutorial/extra-models.md +++ b/docs/en/docs/tutorial/extra-models.md @@ -184,7 +184,9 @@ If it was in a type annotation we could have used the vertical bar, as: 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. +In Python 3.10 and later, using `PlaneItem | CarItem` in assignments such as `response_model=...` is valid, since `|` represents a type union (PEP 604) and produces a `UnionType` object at runtime. +In Python versions prior to 3.10, this syntax raises an error because `|` is interpreted as a bitwise OR operator. +Therefore, this limitation only applies to Python 3.9 and earlier. ## List of models { #list-of-models }