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

♀尐吖头ヾ 提交于 2020-12-05 04:58:45

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!