I want to narrow an unambiguously defined dict[...] type in a superclass to a specific TypedDict in an inheriting class but I cannot figure out a way to specify a dict-based supertype that the TypedDict can get assigned to. I'm using pylance (standard).
The following works fine:
from typing import TypedDict
Base_t = TypedDict('Base_t', {})
class Base[T: Base_t]:
def get(self) -> T:
...
class Implementation_t(TypedDict):
one: str
two: int
class Implementation[U: Implementation_t](Base[U]):
def get(self) -> U:
...
However, if I want to specify Base_t more narrowly (i.e. specify the types of the map), along the idea of:
type Base_t = dict[str, str|int]
the type checker fails assigning Implementation_t to Base_t. I need a way to define a supertype to the TypedDict Implementation_t that specifies key & value types but not any specific fields.
N.B.: Naturally, Implementation_t's key and value types are subsets of Base_t's, respectively.
Is there a nice way to connotate this?
dictdict[A, B]for any A and B (maybe except for literal keys, but that still isn't allowed AFAIC). I doubt you can do this right now, but PEP728 (link to the relevant section) may change this situation when mypy implements it.