Skip to main content

Simple types

Here are examples of common built-in types:
TypeDescription
intInteger
floatFloating point number
boolBoolean value (subclass of int)
strText, sequence of unicode codepoints
bytes8-bit string, sequence of byte values
objectAn arbitrary object (object is the common base class)
All built-in classes can be used as types:
def process_number(x: int) -> float:
    return float(x) * 1.5

def greet(name: str) -> None:
    print(f"Hello, {name}")

The Any type

If you can’t find a good type for some value, you can use Any:
from typing import Any

def process_data(data: Any) -> None:
    # Can perform any operation on data
    print(data.anything())  # No type checking
The type Any is dynamically typed. Mypy doesn’t know anything about the possible runtime types of such values. Use it as an “escape hatch” only when necessary.
See the dynamic typing section for more details.

Generic types

In Python 3.9 and later, built-in collection type objects support indexing:
# Built-in generic types
items: list[str]              # List of str objects
coordinates: tuple[int, int]  # Tuple of two int objects
data: dict[str, int]          # Dictionary from str keys to int values

# Variable length
values: tuple[int, ...]       # Tuple of arbitrary number of ints

Common generic types

TypeDescription
list[str]List of str objects
tuple[int, int]Tuple of two int objects
tuple[int, ...]Tuple of an arbitrary number of int objects
dict[str, int]Dictionary from str keys to int values
set[int]Set of int objects

Protocol types

Iterable, Sequence, and Mapping are generic types that correspond to Python protocols:
from collections.abc import Iterable, Sequence, Mapping

def process_items(items: Iterable[int]) -> None:
    for item in items:
        print(item)

# Works with different types
process_items([1, 2, 3])        # list
process_items((1, 2, 3))        # tuple
process_items({1, 2, 3})        # set
A str object or a list[str] object is valid when Iterable[str] or Sequence[str] is expected.

Type objects

You can use type[C] to refer to the type object of a class:
class User:
    pass

def create_instance(cls: type[User]) -> User:
    return cls()

user = create_instance(User)  # OK
See The type of class objects for more details.

Common examples

from typing import List, Dict

# Lists
names: list[str] = ["Alice", "Bob"]
numbers: list[int] = [1, 2, 3]
mixed: list[int | str] = [1, "two", 3]

# Dictionaries
scores: dict[str, int] = {"Alice": 95, "Bob": 87}
config: dict[str, str | int] = {"host": "localhost", "port": 8080}

Compatibility note

In Python 3.9 and later, you can use built-in collection types directly with type parameters (e.g., list[int]). For Python 3.8 and earlier, import from the typing module instead:
from typing import List, Dict, Tuple, Set

items: List[int] = [1, 2, 3]