I have read that I can reveal the type of variables by using a function called reveal_type
, but I can't find how to use it or from where to import it.
Asked
Active
Viewed 2.7k times
73

Mark Amery
- 143,130
- 81
- 406
- 459

Yuval Pruss
- 8,716
- 15
- 42
- 67
-
1Also see: https://sobolevn.me/2019/08/testing-mypy-types – sobolevn Sep 09 '19 at 09:46
2 Answers
101
I found out in the end how to use it: You should just put and use the reveal_type
in the code, and run it with the mypy
program. Then, it will log a message that look like this:
Revealed type is 'builtins.str*'
From the mypy
documentation:
reveal_type is only understood by mypy and doesn’t exist in Python, if you try to run your program. You’ll have to remove any reveal_type calls before you can run your code. reveal_type is always available and you don’t need to import it.
For more reading: here.

Yuval Pruss
- 8,716
- 15
- 42
- 67
0
To add to the current answer, you can use the TYPE_CHECKING
flag from typing
to have Python skip code that is only for mypy, or to have mypy skip code you don't want it to check.
# myfile.py
from typing import TYPE_CHECKING
a = 'spam' and 5
print(a)
if TYPE_CHECKING:
reveal_type(a) # python skips this line
else:
a = 1/2 # mypy can't see this type error
print(a)
❯ python3 myfile.py
5
0.5
❯ mypy myfile.py
myfile.py:8: note: Revealed type is "Union[builtins.str, builtins.int]"
Success: no issues found in 1 source file

John Cole
- 153
- 1
- 8