pylint warning - unsubscriptable-object

Viewed 5452

with the below code snipped in visual studio code editor

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

pylint is giving the following warning in the second line for a[1].Is it correct to show the warning? Shouldn't the check for None cover it ?

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

I don't have enough reputation points to comment and the edit queue is full so a quick note on Jay Moody's response.

The answer is correct, but it needs a tweak (at least for me). The statement# pylint disable=unsubscriptable-object needs a colon after pylint.

What works for me is # pylint: disable=unsubscriptable-object.

pylint is either not detecting the right type, and you can suppress the warning via:

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

or (since var b isn't in your post), it's correct, and b.get('c') returns an unsubscriptable type, for example:

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