I want to use type hint with string as a key and different classes as a value in a dictionary like below.
configurations: Dict[str, what_class??] = {
"dev": Dev,
"product": Product,
"test": Test,
}
The classes Dev, Product, Test inherit from a base class "Parent".
class Parent:
pass
class Dev(Parent):
pass
class Product(Parent):
pass
class Test(Parent):
pass
What is the best way to do the type hint for this?
Do I need to set just "Any" as their type?
Depends on how accurate you want your hints to be,
configurations: Dict[str, Parent] = {
"dev": Dev,
"product": Product,
"test": Test,
}
from typing import Union
configurations: Dict[str, Union[Dev, Product, Test]] = {
"dev": Dev,
"product": Product,
"test": Test,
}
from typing import TypedDict
class EnvDict(TypedDict):
dev: Dev
product: Product
test: Test
configurations: EnvDict = {
"dev": Dev,
"product": Product,
"test": Test,
}