microsoft / pyright

Static Type Checker for Python
Other
12.7k stars 1.35k forks source link

TypedDict shows error and cannot assign it in dict[str, TypedDict] #8297

Closed vladimir-sama closed 2 weeks ago

vladimir-sama commented 2 weeks ago

Argument of type "dict[str, str | list[str] | int | bool | None]" cannot be assigned to parameter "value" of type "Game" in function "__setItem__"

So this worked in VSCode with Pylance, now I am trying to use Zed with Pyright but it doesn't handle TypedDict. That or I'm using it wrong.


class Game(TypedDict):
    title:str
    image:str
    description:str
    command:List[str]
    is_shell:bool
    time_played:int
    last_played:int
    time_added:int

game_list : Dict[str, Game] = {}

# Asume all variables are there
        game_list[item_id] = {
            'title' : item_title,
            'description' : item_description,
            'command' : shlex.split(item_command),
            'image' : image,
            'last_played' : 0,
            'time_played' : 0,
            'time_added' : int(time.time()),
            'is_shell' : False
        }
erictraut commented 2 weeks ago

Could you provide a self-contained, minimal code sample that exhibits the problem? The code sample above references a bunch of symbols that are not defined.

erictraut commented 2 weeks ago

Here's an attempt to create a minimal repro, but this type checks without errors in pyright:

from typing import TypedDict

class Game(TypedDict):
    title: str

x: dict[str, Game] = {"test": {"title": "test"}}
vladimir-sama commented 2 weeks ago
game_list[item_id] = {
            'title' : item_title,
            'description' : item_description,
            'command' : shlex.split(item_command),
            'image' : image,
            'last_played' : 0,
            'time_played' : 0,
            'time_added' : int(time.time()),
            'is_shell' : False
        }

Issue is here, defining an element like game_list[string_key] = {All keys in Game}

Edit:

from typing import TypedDict, Dict

class Game(TypedDict):
    title:str
    description:str

game_list : Dict[str, Game] = {}

game_list['KEY'] = {
    'title' : 'GAME TITLE',
    'description' : 'GAME DESC'
}

Somehow no error? Maybe adding more types to keys?

vladimir-sama commented 2 weeks ago
from typing import TypedDict, Dict, List

class Game(TypedDict):
    title:str
    description:str
    dates_played:List[int]

game_list : Dict[str, Game] = {}

game_list['KEY'] = {
    'title' : 'GAME TITLE',
    'description' : 'GAME DESC',
    'dates_played' : [1, 2, 3]
}

Argument of type "dict[str, str | list[int]]" cannot be assigned to parameter "value" of type "Game" in function "__setitem__"

erictraut commented 2 weeks ago

I'm not able to repro the problem with the above code sample. Here's the code in pyright playground.

Perhaps you have an outdated version of pyright installed? Or maybe you have some custom configuration options specified?

vladimir-sama commented 2 weeks ago

Found it, item_description could have been None in my code. This needs better errors, thanks for the help!