DetachHead / basedpyright

pyright fork with various type checking improvements, improved vscode support and pylance features built into the language server
https://docs.basedpyright.com
Other
1.12k stars 20 forks source link

`Unpack` should work on generics that are bound to a `TypedDict` #618

Open KotlinIsland opened 2 months ago

KotlinIsland commented 2 months ago
from typing import TypedDict, Unpack

class TDict(TypedDict):
    pass

def f[T: TDict](**kwargs: Unpack[T]) -> T: ...

reveal_type(f(a=1))
reveal_type(f(b="a"))

because TypedDict is structural, this should allow all TypedDicts to work

DetachHead commented 2 months ago

doesn't necessarily need to be a TypedDict, i don't see why it shouldn't work if oit's bound to Mapping[str, object]:

from typing import Unpack
from collections.abc import Mapping

class Foo[T: Mapping[str, object]]:
    def f(self, **kwargs: Unpack[T]) -> T: ...

_ = Foo[Mapping[str, object]]().f(a='asdf')
KotlinIsland commented 2 months ago

doesn't necessarily need to be a TypedDict, i don't see why it shouldn't work if oit's bound to Mapping[str, object]:

from typing import Unpack
from collections.abc import Mapping

class Foo[T: Mapping[str, object]]:
    def f(self, **kwargs: Unpack[T]) -> T: ...

_ = Foo[Mapping[str, object]]().f(a='asdf')
class AmongusMapping(Mapping[str, str]):...
Foo[AmongusMapping]().f(a="asdf")
DetachHead commented 2 months ago

it should still be able to unpack them even if it's a different subtype of mapping imo