First steps
Type system reference
Configuring and running mypy
Miscellaneous
Project Links
This chapter introduces some commonly used built-in types. We willcover many other kinds of types later.
Here are examples of some common built-in types:
Type | Description |
|---|---|
| integer |
| floating point number |
| boolean value (subclass of |
| text, sequence of unicode codepoints |
| 8-bit string, sequence of byte values |
| an arbitrary object ( |
All built-in classes can be used as types.
If you can’t find a good type for some value, you can always fall backtoAny:
Type | Description |
|---|---|
| dynamically typed value with an arbitrary type |
The typeAny is defined in thetyping module.SeeDynamically typed code for more details.
In Python 3.9 and later, built-in collection type objects supportindexing:
Type | Description |
|---|---|
| list of |
| tuple of two |
| tuple of an arbitrary number of |
| dictionary from |
| iterable object containing ints |
| sequence of booleans (read-only) |
| mapping from |
| type object of |
The typedict is ageneric class, signified by type arguments within[...]. For example,dict[int,str] is a dictionary from integers tostrings anddict[Any,Any] is a dictionary of dynamically typed(arbitrary) values and keys.list is another generic class.
Iterable,Sequence, andMapping are generic types that correspond toPython protocols. For example, astr object or alist[str] object isvalid whenIterable[str] orSequence[str] is expected.You can import them fromcollections.abc instead of importing fromtyping in Python 3.9.
SeeUsing generic builtins for more details, including how you canuse these in annotations also in Python 3.7 and 3.8.
These legacy types defined intyping are needed if you need to supportPython 3.8 and earlier:
Type | Description |
|---|---|
| list of |
| tuple of two |
| tuple of an arbitrary number of |
| dictionary from |
| iterable object containing ints |
| sequence of booleans (read-only) |
| mapping from |
| type object of |
List is an alias for the built-in typelist that supportsindexing (and similarly fordict/Dict andtuple/Tuple).
Note that even thoughIterable,Sequence andMapping looksimilar to abstract base classes defined incollections.abc(formerlycollections), they are not identical, since the latterdon’t support indexing prior to Python 3.9.