-1

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?

Eric Lee
  • 700
  • 2
  • 9
  • 30
  • Does this answer your question? [Subclass in type hinting](https://stackoverflow.com/questions/46092104/subclass-in-type-hinting) – Marius ROBERT Jun 21 '22 at 07:25
  • @MariusROBERT the answer you linked gave me a hint to think about this situation but the most accurate answer was the one from Ron Serruya. It was good to read the answer to get a hint. – Eric Lee Jun 21 '22 at 08:28

2 Answers2

1

In your case, creating a dictionary without using type hints seems the most optimal solution, as you can access all classes directly from the dict variable configurations, using the proper string key value:

class Parent:
    pass

class Dev(Parent):
    pass

class Product(Parent):
    pass

class Test(Parent):
    pass

configurations = {
    "dev": Dev,
    "product": Product,
    "test": Test,
}

print(configurations)

Output:

{'dev': <class '__main__.Dev'>, 'product': <class '__main__.Product'>, 'test': <class '__main__.Test'>}

Also, using the Parent class works well if you still want to use type hints:

import typing
class Parent:
    pass

class Dev(Parent):
    pass

class Product(Parent):
    pass

class Test(Parent):
    pass

configurations: typing.Dict[str, Parent] = {
    "dev": Dev,
    "product": Product,
    "test": Test,
}
print(configurations)

It outputs the same dictionary as the above code.

Cardstdani
  • 4,999
  • 3
  • 12
  • 31
  • What do you mean by "without using type hints seems the most optimal solution"? is your view to type hinting is optional? – Eric Lee Jun 21 '22 at 08:34
  • @eric-lee I just proposed it as another solution, which I consider the best. However, I don't know the full project that is behind that solution. – Cardstdani Jun 21 '22 at 08:35
  • I see. I just wanted to implement type hinting for my code looking better for others. Thanks for the advices. – Eric Lee Jun 22 '22 at 01:48
1

Depends on how accurate you want your hints to be,

  1. Use the parent class
configurations: Dict[str, Parent] = {
    "dev": Dev,
    "product": Product,
    "test": Test,
}
  1. Specify the classes in a union
from typing import Union
configurations: Dict[str, Union[Dev, Product, Test]] = {
    "dev": Dev,
    "product": Product,
    "test": Test,
}
  1. Create a TypedDict type for this specific dict
from typing import TypedDict

class EnvDict(TypedDict):
    dev: Dev
    product: Product
    test: Test
    
configurations: EnvDict = {
    "dev": Dev,
    "product": Product,
    "test": Test,
}
Ron Serruya
  • 3,988
  • 1
  • 16
  • 26