20
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*?

Nova
  • 2,623
  • 4
  • 26
  • 45

1 Answers1

20

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.

Michael0x2a
  • 58,192
  • 30
  • 175
  • 224
  • Is that documented somewhere? – wjandrea Sep 19 '22 at 18:24
  • 1
    @wjandrea -- Not anymore. This question and answer is actually a bit out-of-date now -- mypy [stopped using the asterisk to mark inferred types](https://github.com/python/mypy/issues/10076) earlier this year. – Michael0x2a Sep 19 '22 at 23:29