网页
综合
更新时间 2026-11-14 05:19:21
Python Types Intro - FastAPI
自动蜘蛛
fastapi.tiangolo.com
正文约 3020 字
Use the official FastAPI skill for your installed version. Install it from your project directory:
See Develop with AI.
Python has support for optional "type hints" (also called "type annotations").
These "type hints" or annotations are a special syntax that allows declaring the type 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.
FastAPI is all based on these type hints, they give it many advantages and benefits.
But even if you never use FastAPI, you would benefit from learning a bit about them.
If you are a Python expert, and you already know everything about type hints, skip to the next chapter.
Let's start with a simple example:
Calling this program outputs:
The function does the following:
It's a very simple program.
But now imagine that you were writing it from scratch.
At some point you start defining the function, and you have the parameters ready...
But then you have to call "that method that converts the first letter to upper case".
Was it upper? Was it uppercase? first_uppercase? capitalize?
Then, you try with the old programmer's friend, editor autocompletion.
You type the first parameter of the function, first_name, then a dot (.) and then hit Ctrl+Space to trigger the completion.
But, sadly, you get nothing useful:
Let's modify a single line from the previous version.
We will change exactly this fragment, the parameters of the function, from:
Those are the "type hints":
That is not the same as declaring default values like it would be with:
It's a different thing.
We are using colons (:), not equals (=).
And adding type hints normally doesn't change what happens from what would happen without them.
But now, imagine you are again in the middle of creating that function, but with type hints.
At the same point, you try to trigger the autocomplete with Ctrl+Space and you see:
With that, you can scroll, seeing the options, until you find the one that "rings a bell":
Check this function, it already has type hints:
Because the editor knows the types of the variables, you don't only get completion, you also get error checks:
Now you know that you have to fix it, convert age to a string with str(age):
You just saw the main place to declare type hints. As function parameters.
This is also the main place you would use them with FastAPI.
You can declare all the standard Python types, not only str.
You can use, for example:
For some additional use cases, you might need to import some things from the standard library typing module, for example when you want to declare that something has "any type", you can use Any from typing:
Some types can take "type parameters" in square brackets, to define their internal types, for example a "list of strings" would be declared list[str].
These types that can take type parameters are called Generic types or Generics.
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。