Displaying the type of an expression

You can use reveal_type(expr) to ask mypy to display the inferredstatic type of an expression. This can be useful when you don’t quiteunderstand how mypy handles a particular piece of code. Example:

  1. reveal_type((1, 'hello')) # Revealed type is 'Tuple[builtins.int, builtins.str]'

You can also use reveal_locals() at any line in a fileto see the types of all local variables at once. Example:

  1. a = 1
  2. b = 'one'
  3. reveal_locals()
  4. # Revealed local types are:
  5. # a: builtins.int
  6. # b: builtins.str

Note

reveal_type and reveal_locals are only understood by mypy anddon’t exist in Python. If you try to run your program, you’ll have toremove any reveal_type and reveal_locals calls before you canrun your code. Both are always available and you don’t need to importthem.