pylint 警告 - 无法订阅的对象

pylint warning - unsubscriptable-object

在 visual studio 代码编辑器

中截取以下代码
a = b.get('c') if b else None
d = a[1] if a else None

pylinta[1] 的第二行发出以下警告。显示警告是否正确? None 的支票不应该涵盖它吗?

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

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

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

或者(因为 var b 不在你的 post 中),它是正确的,并且 b.get('c') returns 是一个不可订阅的类型,例如:

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

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

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

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