How can I type hint a dictionary where the key is a specific tuple and the value is known?
For example I want to type hint a dict like this:
class A:
pass
class B:
pass
class_map: = {
(str,): A
(int,): B
}
some_cls = class_map[(str,)]
The use case will be to go from a known set of bases to a class that was previously defined using those bases.
One can do this by
__getitem__signature differs from the one that dict uses__getitem__which gets values from an input dict__getitem__with the tuple input and type hint outputAnother thing that we can do is require that the input to make the dict is a frozenset of tuples. Then one can type hint what is allowed in:
This enables functionality similar to TypedDict but with tuple keys.