问题
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*
?
回答1:
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.
来源:https://stackoverflow.com/questions/50498575/what-does-the-asterisk-in-the-output-of-reveal-type-mean