`reveal_type` 输出中的星号是什么意思? [英] What does the asterisk in the output of `reveal_type` mean?

查看:49
本文介绍了`reveal_type` 输出中的星号是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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'

intint* 有什么区别?

推荐答案

表示特定类型被 mypy 推断为执行类型变量替换的一部分.

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

比如blah[0]其实就是在做blah.__getitem__(0):原来定义了__getitem__方法返回一些 _T 类型的值,其中 _T 是列表中包含的任何类型*.

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 知道 blah 包含整数,因此推断 _T 返回类型是 int 类型.

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

相比之下,只有 1blah[0] * 2 不会进行类型变量推断.前者是文字;后者正在调用 int.__mul__(...) 方法,该方法被输入为专门返回一个 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.

*嗯,这实际上不是确切的签名,但已经足够接近了.

在大多数情况下,您可以忽略这一点,将其视为 mypy 的一个实现细节.它的存在主要是因为能够判断一个类型是否被推断出来,这在您修补或调试 mypy 内部结构时偶尔会很有用.

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.

这篇关于`reveal_type` 输出中的星号是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆