Search code examples
pythonvisual-studio-codepylint

pylint warning - unsubscriptable-object


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)

Solution

  • 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