-
Hi. I'm adding strict typing to my project. I've been using this: class DefaultsDict(collections.UserDict):
def __init__(self, *args, defaults, **kwargs):
super().__init__(*args, **kwargs)
self.defaults = defaults
def __missing__(self, key):
return self.defaults[key] I use Any idea how to do this? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
This is not possible, the only type you can use with a |
Beta Was this translation helpful? Give feedback.
-
I thought of one way you could make this work for a specific if TYPE_CHECKING:
class Foo(TypedDict):
a: int
b: str
else:
Foo = DefaultsDict
def takes_foo(x: Foo): ...
foo = Foo({'a': 5, 'b': ''})
foo['a'] = 6 # ok
takes_foo(foo) # also ok but then the type checker would not be aware of the |
Beta Was this translation helpful? Give feedback.
-
So, until intersection types are available, I guess I'm gonna stick to something like this: Click to expandfrom collections import UserDict
from collections.abc import Mapping
from typing import Any, cast, Generic, TypedDict, TypeVar
_KT = TypeVar("_KT")
_VT = TypeVar("_VT")
class DefaultsDict(UserDict[_KT, _VT]):
def __init__(
self,
*args: Any,
defaults: Mapping[_KT, _VT],
**kwargs: Any,
) -> None:
super().__init__(*args, **kwargs)
self.defaults = defaults
def __missing__(self, key: _KT) -> _VT:
return self.defaults[key]
_TDKT = str
_TDVT = object
_TDT = TypeVar("_TDT", bound=Mapping[_TDKT, _TDVT])
class TypedDefaultsDict(DefaultsDict[_TDKT, _TDVT], Generic[_TDT]):
data: _TDT # type: ignore[assignment]
defaults: _TDT
def __init__(
self,
*args: Any,
defaults: _TDT,
**kwargs: Any,
) -> None:
super().__init__(*args, defaults=defaults, **kwargs)
def to_typeddict(self) -> _TDT:
return cast(_TDT, self)
class Foo(TypedDict):
...
def use_foo(foo: Foo):
...
foo: Foo = {...}
foo_with_defaults = TypedDefaultsDict[Foo](defaults=foo)
use_foo(foo)
use_foo(foo_with_defaults.to_typeddict())
|
Beta Was this translation helpful? Give feedback.
This is not possible, the only type you can use with a
TypedDict
isdict
or more specifically a dict literal, since otherwise you lose the types of the individual key value pairs, you cannot even use subclasses ofdict
currently, although PEP 728 may carve out some exceptions for a uniform closedTypedDict
. This wouldn't help with your use-case though.