microsoft / vscode-pylint

Linting support for python using the pylint library.
https://marketplace.visualstudio.com/items?itemName=ms-python.pylint
MIT License
70 stars 34 forks source link

Dictionary is unsubscriptable false positive #483

Closed umnovI closed 8 months ago

umnovI commented 8 months ago

Dictionary is unsubscriptable error when using random.sample(...) unless I explicitly convert it to list list(random.sample(...)). Then the error goes away.

Code that gives an error:

import json
import random

a = '{"count":64,"next":"https://pokeapi.co/api/v2/berry/?offset=20&limit=20","previous":null,"results":[{"name":"cheri","url":"https://pokeapi.co/api/v2/berry/1/"},{"name":"chesto","url":"https://pokeapi.co/api/v2/berry/2/"},{"name":"pecha","url":"https://pokeapi.co/api/v2/berry/3/"},{"name":"rawst","url":"https://pokeapi.co/api/v2/berry/4/"},{"name":"aspear","url":"https://pokeapi.co/api/v2/berry/5/"},{"name":"leppa","url":"https://pokeapi.co/api/v2/berry/6/"},{"name":"oran","url":"https://pokeapi.co/api/v2/berry/7/"},{"name":"persim","url":"https://pokeapi.co/api/v2/berry/8/"},{"name":"lum","url":"https://pokeapi.co/api/v2/berry/9/"},{"name":"sitrus","url":"https://pokeapi.co/api/v2/berry/10/"},{"name":"figy","url":"https://pokeapi.co/api/v2/berry/11/"},{"name":"wiki","url":"https://pokeapi.co/api/v2/berry/12/"},{"name":"mago","url":"https://pokeapi.co/api/v2/berry/13/"},{"name":"aguav","url":"https://pokeapi.co/api/v2/berry/14/"},{"name":"iapapa","url":"https://pokeapi.co/api/v2/berry/15/"},{"name":"razz","url":"https://pokeapi.co/api/v2/berry/16/"},{"name":"bluk","url":"https://pokeapi.co/api/v2/berry/17/"},{"name":"nanab","url":"https://pokeapi.co/api/v2/berry/18/"},{"name":"wepear","url":"https://pokeapi.co/api/v2/berry/19/"},{"name":"pinap","url":"https://pokeapi.co/api/v2/berry/20/"}]}'

items: list[dict[str, str]] = random.sample(json.loads(a)["results"], 5)
for item in items:
    b = item["url"]

error message.

    {
        "type": "error",
        "module": "test",
        "obj": "",
        "line": 9,
        "column": 8,
        "endLine": 9,
        "endColumn": 12,
        "path": "test.py",
        "symbol": "unsubscriptable-object",
        "message": "Value 'item' is unsubscriptable",
        "message-id": "E1136"
    }
umnovI commented 8 months ago

version: v2023.11.13481007

screenshot: image

karthiknadig commented 8 months ago

This should be filed on pylint. This repo is for the extension that wraps pylint for VS Code. We don't control the error reported by pylint.