Pylance中int与字面量无法比较时,应如何正确比较数字?
Pylance整数比较报错的困惑
我在代码中使用表达式:
random.randint(1, 10) == 5
结果代码检查工具(Pylance)报错:'Operator "==" not supported for types "int" and "Literal[0]'。
为了转换成int/int类型比较,我尝试修改为:
random.randint(1, 10) == int(5)
此时Pylance提示int构造函数“Expected 0 positional arguments”。我猜测需要显式指定关键字参数,于是又尝试:
random.randint(1, 10) == int(x=5)
但这次出现了三个完全让人困惑的错误:
Operator "==" not supported for types "int" and "int"(比较两个整数无疑是所有语言中最基础的合法操作之一)Using deprecated argument x of method int()(官方文档未提及该参数已弃用)Expected no arguments to int constructor
请问Pylance到底期望我如何比较两个整数?我查阅了Python类型提示、Pylance、int构造函数等相关文档,均未找到对应的说明。
内容的提问来源于stack exchange,提问作者GreenTriangle
相关产品推荐
相关产品推荐

