You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 18:30:41