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:
- 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:
- a = 1
- b = 'one'
- reveal_locals()
- # Revealed local types are:
- # a: builtins.int
- # 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.