Featurecore
Basic type annotations
Also called list[int], dict[str, int], tuple[int, ...], Optional, X | None, Union, Any, Callable, object type, built-in generics, PEP 585, PEP 604.
The basic hints are classes such as int and str, built-in generics such as list[str] and dict[str, int], unions written X | Y, X | None for a value that can be None, Callable, Any and object.
- Language
- Python
- Proposals
- pep-484, pep-585, pep-604
Synopsis
from collections.abc import Callable
def summary(
names: list[str],
ages: dict[str, int],
point: tuple[int, int],
note: str | None,
show: Callable[[str], str],
) -> str:
oldest = max(names, key=lambda name: ages[name])
extra = note if note is not None else "-"
return show(f"{oldest} {point} {extra}")
print(summary(["ann", "bo"], {"ann": 31, "bo": 40}, (2, 3), None, str.upper))
BO (2, 3) -
Each parameter has a hint: a list of strings, a dictionary from strings to integers, a pair of integers, a string or None, and a function from a string to a string. mypy --strict accepts the file.
Semantics
r1. A class is a type. Use int, str, float, bool, bytes and your own classes directly as hints.
r2. A collection class takes the types of its items in square brackets: list[int], set[str], dict[str, int]. "In type annotations you can now use built-in collection types such as list and dict as generic types instead of importing the corresponding capitalized types (e.g. List or Dict) from typing."1 The old names are deprecated: typing.List is a "Deprecated alias to list."2
r3. A tuple is a special case. "tuple accepts any number of type arguments".2 "To denote a tuple which could be of any length, and in which all elements are of the same type T, use the literal ellipsis ...: tuple[T, ...]. To denote an empty tuple, use tuple[()]."2 Other containers take a fixed number of arguments: "list only accepts one type argument".2 Example: two-args.
r4. A union is a value of one of several types. "Union[X, Y] is equivalent to X | Y and means either X or Y."2 "Using that shorthand is recommended."2 PEP 604 "proposes overloading the | operator on types to allow writing Union[X, Y] as X | Y, and allows it to appear in isinstance and issubclass calls."3 For a value that can be None, write X | None. "Optional[X] is equivalent to X | None (or Union[X, None])."2 Example: union-runtime.
r5. Optional does not mean an optional argument. "Note that this is not the same concept as an optional argument, which is one that has a default."2 A default of None needs X | None in the hint. PEP 484 says of the old shortcut: "This is no longer the recommended behavior. Type checkers should move towards requiring the optional type to be made explicit."4 Example: default-none.
r6. A function value has the type Callable[[A, B], R]. "Callable[[int], str] signifies a function that takes a single parameter of type int and returns a str."2 "The subscription syntax must always be used with exactly two values: the argument list and the return type."2 Import Callable from collections.abc, because typing.Callable is a "Deprecated alias to collections.abc.Callable."2 Example: callback.
r7. Any turns the checks off. "Every type is assignable to Any. Any is assignable to every type."2 object is a normal type that holds every value: "Unlike Any, object is an ordinary static type (it is similar to Object in Java), and only operations valid for all types are accepted for object values."5 Example: any-vs-object.
Examples
from typing import Optional, Union
print(int | None)
print(Optional[int] == (int | None))
print(Union[int, str] == (int | str))
print(isinstance(1, int | str), isinstance(1.5, int | str))
int | None
True
True
True False
The | form and the typing forms make equal objects. A union also works in isinstance: "This new syntax is also accepted as the second argument to isinstance() and issubclass()".6
This code raises AttributeError, on purpose.
from collections.abc import Callable
def apply(values: list[int], step: Callable[[int], int]) -> list[int]:
return [step(v) for v in values]
def double(n: int) -> int:
return n * 2
def shout(text: str) -> str:
return text.upper()
print(apply([1, 2], double))
print(apply([1, 2], lambda n: n + 1))
print(apply([1, 2], shout))
The first two calls pass a function from int to int. The third passes a function that takes a str, and it fails at run time. The checker reports it first (rule r6):
$ mypy --strict callback.py
callback.py:18: error: Argument 2 to "apply" has incompatible type "Callable[[str], str]"; expected "Callable[[int], int]" [arg-type]
Found 1 error in 1 file (checked 1 source file)
Versions
Since 3.9
The built-in collections take type arguments, as in
list[int], from PEP 585.71
Since 3.10
The
X | Yform of a union is added, from PEP 604. "A new type union operator was introduced which enables the syntax X | Y."6
Pitfalls
This code runs but gives a wrong answer on purpose.
def greet(name: str = None) -> str:
return "hello " + (name or "world")
def greet_ok(name: str | None = None) -> str:
return "hello " + (name or "world")
print(greet(), greet_ok("ann"))
hello world hello ann
Both functions run, but the hint of greet says str, and its default is None (rule r5). The checker rejects it:
$ mypy --strict default_none.py
default_none.py:1: error: Incompatible default for parameter "name" (default has type "None", parameter has type "str") [assignment]
default_none.py:1: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True
default_none.py:1: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase
Found 1 error in 1 file (checked 1 source file)
This code raises TypeError, on purpose.
from typing import Any
def size_any(value: Any) -> int:
return len(value)
def size_object(value: object) -> int:
return len(value)
print(size_any("abc"))
print(size_any(5))
With Any, the checker accepts len(value) and the call size_any(5), and the program fails at run time. With object, the checker rejects len(value), because not every object has a length (rule r7):
$ mypy --strict obj.py
obj.py:9: error: Argument 1 to "len" has incompatible type "object"; expected "Sized" [arg-type]
Found 1 error in 1 file (checked 1 source file)
Use object for "any value" when the code only passes the value on, and narrow it with isinstance before you use it.
This code runs but gives a wrong answer on purpose.
pairs: list[int, str] = [1, "a"]
print(pairs)
[1, 'a']
Python accepts list[int, str] at run time, but the type is wrong: a list has one item type (rule r3). Use list[int | str], or tuple[int, str] for a fixed pair.
$ mypy --strict twoargs.py
twoargs.py:1: error: "list" expects 1 type argument, but 2 given [type-arg]
Found 1 error in 1 file (checked 1 source file)
Comparison
In Python, a hint is an expression, and the common types are the classes that the program uses at run time, such as list[int] (rule r2). The same objects work in isinstance for unions. The type Any has no check at all, and object is the top of the class tree with the checks on (rule r7). For the theory of a type that holds every value, see Top type. For the abstract collection types, such as Sequence and Mapping, see collections.abc.
References
- What’s New In Python 3.9, Type Hinting Generics in Standard Collections
- typing, Support for type hints, Python 3.15 documentation, Annotating tuples; Annotating callable objects; typing.Any; typing.Union; typing.Optional; typing.List
- PEP 604: Allow writing union types as X | Y, Abstract
- PEP 484: Type Hints, Union types
- Dynamically typed code, mypy documentation, Any vs. object
- What’s New In Python 3.10, PEP 604: New Type Union Operator
- PEP 585: Type Hinting Generics In Standard Collections, Abstract
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag DD6ZFJ Edit this page History Report an error Markdown