-
Notifications
You must be signed in to change notification settings - Fork 288
Allow using Unpack with arbritary bounded TypeVars #1897
Unanswered
PythonCoderAS
asked this question in
Q&A
-
I want to make a class where the values of kwargs are specified through a TypeVar, something like this:
from typing import * from abc import ABC class EmptyTypedDict(TypedDict): pass DictT = TypeVar("DictT", bound=TypedDict, default=EmptyTypedDict) class MyClass(ABC, Generic[DictT]): @abstractmethod def my_func_inner(self, **extra: Unpack[DictT]): ... def my_func(self, arg: int, **extra: Unpack[DictT]): print(f"Arg: {arg}, extra: {extra}") return self.my_func_inner(self, **extra) class SubExtra(TypedDict): x: int y: str class MyClassSub(MyClass[SubExtra]): def my_func_inner(self, *, x: int, y: str): print(f"x = {x}, y = {y}")
Unfortunately, there seems to be no way to do this, firstly because TypedDict is not a valid bound type, and second because Unpack will not take TypeVars.
I'm thinking that maybe we could make it so that TypedDict can be a proper bound, or an equivalent bound type that will make Unpack happy.
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment
-
There’s an open feature request for just this: #1399
Beta Was this translation helpful? Give feedback.
All reactions
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment