Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Allow using Unpack with arbritary bounded TypeVars #1897

Unanswered
PythonCoderAS asked this question in Q&A
Discussion options

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.

You must be logged in to vote

Replies: 1 comment

Comment options

There’s an open feature request for just this: #1399

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /