【问题标题】:pylint warning - unsubscriptable-objectpylint 警告 - 不可订阅对象
【发布时间】:2020-09-07 14:38:37
【问题描述】:

在 Visual Studio 代码编辑器中截取以下代码

a = b.get('c') if b else None
d = a[1] if a else None

pylint 在第二行为a[1] 提供以下警告。显示警告是否正确? None 的检查不应该覆盖它吗?

a: NoneType
Value 'a' is unsubscriptable pylint(unsubscriptable-object)

【问题讨论】:

  • 这很好,linter 不够聪明,无法检测到 None 检查。显式检查 (if a is not None: ...) 可能会解决此问题,否则最好添加异常或直接忽略

标签: python visual-studio-code pylint


【解决方案1】:

pylint 没有检测到正确的类型,您可以通过以下方式抑制警告:

d = a[1] if a else None  # pylint: disable=unsubscriptable-object

或者(因为你的帖子中没有 var b),是正确的,b.get('c') 返回一个不可订阅的类型,例如:

b = {"c": 1}
a = b.get('c') if b else None
# a = 1, 1 is not subscriptable

【讨论】:

    【解决方案2】:

    我没有足够的声望点来发表评论,而且编辑队列已满,所以请快速记录一下 Jay Moody 的回复。

    答案是正确的,但需要调整(至少对我而言)。语句# pylint disable=unsubscriptable-object 在pylint 后需要一个冒号。

    对我有用的是# pylint: disable=unsubscriptable-object

    【讨论】:

      猜你喜欢
      • 2018-04-29
      • 2022-12-11
      • 1970-01-01
      • 2023-01-15
      • 1970-01-01
      • 1970-01-01
      • 2022-11-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多