You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Feature request: Allow unpacking of type variable bound to typed dict #1399
Reacted by Jae-Won Chung, Kevin Shi, Lucas Colombo, Albin Skott, Krzysztof Jagiello, Ren, Pavel Dedik, Fynn Beuttenmüller, Karim Amzil, Joren Hammudoglu and 55 more
This would be kwargs equivalent of being able to unpack TypeVarTuple. This would I think solve this request to be able to use Paramspec kwargs only.
I have a couple small use cases where this would be nice although they mostly follow other issue of more flexibility with preserving just args/kwargs/similar manipulation.
N=TypeVar("N") # type for node IDA=TypeVar("A", bound=TypedDict) # type for node attributeclassGraph(Generic[N, A]):
defadd_node(self, node_for_adding: N, **attr: Unpack[A]) ->None: ...
Reacted by Neil Girdhar, Karim Amzil, Joren Hammudoglu, Mike Perlov, Pavel Dedik, ., Gerhard Adler Skartveit, Pritam Dey, Paul Garner, Mircea Mironenco and 8 more
I would also very much like to see this. I stumbled across this limitation when trying to implement a generic class with a typevar that specifies the kwargs a given method can accept.
Reacted by Neil Girdhar, Pascal Mathis, Mircea Mironenco, Fedor Fedorenko, Chase Sterling, Alexander Malyga and Yurii
I am making a generic class that stores configuration data for my application. Also it takes some metadata as **kwargs in __init__ method. I want to add typing to it using Generic and TypedDict.
classMyTD(TypedDict, total=False):
arg1: intarg2: stra: MyClass[MyTD] =MyClass(arg1=4, arg2="foo")
b: MyClass[MyTD] =MyClass(arg1="bar", arg2=6) # Should be type error herereveal_type(a.init_kwargs['arg1']) # Should be intreveal_type(a.init_kwargs['arg2']) # Should be str
But type checker (mypy) throws error:
def__init__(self, **kwargs: Unpack[TV]) ->None:
# ^^# Unpack item in ** argument must be a TypedDict
Activity
hmc-cs-mdrissi commentedon May 3, 2023
This would be kwargs equivalent of being able to unpack TypeVarTuple. This would I think solve this request to be able to use Paramspec kwargs only.
I have a couple small use cases where this would be nice although they mostly follow other issue of more flexibility with preserving just args/kwargs/similar manipulation.
jaywonchung commentedon Sep 8, 2023
I believe I hit this issue when I was trying to write a type stub for
networkx(ornx).nx.Graphand its variants allow arbitrary attributes to be associated with nodes, edges, and graphs. For example, adding a node has signature (ref):I was trying to do this:
NeilGirdhar commentedon Sep 9, 2023
Yup, perfect example.
paveldedik commentedon May 6, 2024
Is there something I could help with to speed this up? Probably only possible way to halp is to draft a PEP?
Anyway, it would be really useful for a framework I'm building - https://github.com/paveldedik/ludic/blob/main/ludic/base.py#L293-L308
It seems I can already bound a subclass of a
TypedDictinTypeVarand it works in mypy at least:This seems to work in mypy but I'm afraid there is not PEP for it.
sennalen commentedon Jan 7, 2025
The trouble is
Unpack[T]does not accept a TypeVar in place of a concrete TypedDict, regardless of being bounded by a TypedDictmharding-hpe commentedon May 3, 2025
I would also very much like to see this. I stumbled across this limitation when trying to implement a generic class with a typevar that specifies the kwargs a given method can accept.
t1m013y commentedon Jun 29, 2025
I need this feature too.
I am making a generic class that stores configuration data for my application. Also it takes some metadata as
**kwargsin__init__method. I want to add typing to it usingGenericandTypedDict.Usage example:
But type checker (mypy) throws error:
RBerga06 commentedon Dec 8, 2025
Related: #1395
frezX commentedon Jan 10, 2026
I need it too
faithleysath commentedon Jan 11, 2026
I need it too, I need Unpack can take a TypeVar which bond to TypeDict.
andrewsonin commentedon Apr 11, 2026
I need it too