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:

  1. from typing import List
  2.  
  3. def log(x: object) -> None:
  4. print('log:', repr(x))
  5.  
  6. # Error: Function "t.log" is not valid as a type [valid-type]
  7. def log_all(objs: List[object], f: log) -> None:
  8. for x in objs:
  9. f(x)

You can use Callable as the type for callable objects:

  1. from typing import List, Callable
  2.  
  3. # OK
  4. def log_all(objs: List[object], f: Callable[[object], None]) -> None:
  5. for x in objs:
  6. f(x)