Check validity of types [valid-type]
Mypy checks that each type annotation and any expression thatrepresents a type is a valid type. Examples of valid types includeclasses, union types, callable types, type aliases, and literal types.Examples of invalid types include bare integer literals, functions,variables, and modules.
This example incorrectly uses the function log
as a type:
- from typing import List
- def log(x: object) -> None:
- print('log:', repr(x))
- # Error: Function "t.log" is not valid as a type [valid-type]
- def log_all(objs: List[object], f: log) -> None:
- for x in objs:
- f(x)
You can use Callable
as the type for callable objects:
- from typing import List, Callable
- # OK
- def log_all(objs: List[object], f: Callable[[object], None]) -> None:
- for x in objs:
- f(x)