Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the asterisk in the output of `reveal_type` mean?

Tags:

python

types

mypy

reveal_type(1) # Revealed type is 'builtins.int'
bla = [1,2,3]
reveal_type(bla[0]) # Revealed type is 'builtins.int*'
reveal_type(bla[0] * 2) # Revealed type is 'builtins.int'

What is the difference between int and int*?

like image 933
Erik Avatar asked May 23 '18 22:05

Erik


1 Answers

It means that particular type was inferred by mypy as a part of performing type variable substitution.

For example, blah[0] is actually doing blah.__getitem__(0): it turns out that the __getitem__ method is defined to return some value of type _T, where _T is whatever type is contained within the list*.

Mypy understands that blah contains ints, and so inferred that the _T return type is of type int.

In contrast, there's no type variable inference going on with just 1 and blah[0] * 2. The former is a literal; the latter is invoking the int.__mul__(...) method, which is typed to return specifically an int.

*Well, that's not actually the exact signature, but close enough.


For the most part, you can ignore this and just treat it as an implementation detail of mypy. It exists mostly because being able to tell whether or not a type was inferred is occasionally useful to have when you're tinkering with or debugging mypy internals.

like image 89
Michael0x2a Avatar answered Oct 19 '22 16:10

Michael0x2a