**Python 3.6+** has support for optional "type hints".
**Python 3.6+**支持可选的“类型提示”。
These **"type hints"** are a new syntax (since Python 3.6+) that allow declaring the <abbrtitle="for example: str, int, float, bool">type</abbr> of a variable.
By declaring types for your variables, editors and tools can give you better support.
通过声明变量的类型,编辑器和工具可以为你提供更好的支持。
This is just a **quick tutorial / refresher** about Python type hints. It covers only the minimum necessary to use them with **FastAPI**... which is actually very little.
There are some data structures that can contain other values, like `dict`, `list`, `set` and `tuple`. And the internal values can have their own type too.
You declare the "shape" of the data as classes with attributes.
你将数据的“形状shape”声明为具有属性的类。
And each attribute has a type.
每个属性都有一个类型。
Then you create an instance of that class with some values and it will validate the values, convert them to the appropriate type (if that's the case) and give you an object with all the data.
You will see a lot more of all this in practice in the [Tutorial - User Guide](tutorial/index.md){.internal-link target=_blank}.
在[教程-用户指南](tutorial/README.md)中,你会在实践中看到更多的相关信息。
## Type hints in **FastAPI**
## **FastAPI**中的类型提示
**FastAPI** takes advantage of these type hints to do several things.
**FastAPI**利用这些类型提示来做几件事。
With **FastAPI** you declare parameters with type hints and you get:
使用**FastAPI**,您可以使用类型提示来声明参数,并得到:
***Editor support**.
***Type checks**.
***编辑器支持**。
***类型检查**。
...and **FastAPI** uses the same declarations to:
...**FastAPI**使用相同的声明来:
***Define requirements**: from request path parameters, query parameters, headers, bodies, dependencies, etc.
***Convert data**: from the request to the required type.
***Validate data**: coming from each request:
*Generating **automatic errors** returned to the client when the data is invalid.
***Document** the API using OpenAPI:
*which is then used by the automatic interactive documentation user interfaces.
***定义请求**:来自请求路径参数,查询参数,标头,正文,依赖项等。
***转换数据**:从请求到所需的类型。
***验证数据**:来自每个请求:
*当数据无效时,生成**自动错误**返回给客户端。
***文档** 使用OpenAPI记录API:
*然后由自动交互式文档用户界面使用。
This might all sound abstract. Don't worry. You'll see all this in action in the [Tutorial - User Guide](tutorial/index.md){.internal-link target=_blank}.
The important thing is that by using standard Python types, in a single place (instead of adding more classes, decorators, etc), **FastAPI** will do a lot of the work for you.
If you already went through all the tutorial and came back to see more about types, a good resource is <ahref="https://mypy.readthedocs.io/en/latest/cheat_sheet_py3.html"class="external-link"target="_blank">the "cheat sheet" from `mypy`</a>.