-
-
Notifications
You must be signed in to change notification settings - Fork 339
Open
Labels
Description
Background
My usual usecase of Selector is when I have an interface with multiple implementations:
class Animal:
def __init__(self) -> None:
pass
class Dog(Animal):
pass
class Cat(Animal):
passAnd then the container:
class Container(containers.DeclarativeContainer):
get_dog = providers.Singleton(Dog)
get_cat = providers.Singleton(Cat)
get_animal = providers.Selector(lambda: random.choice(["cat", "dog"]), cat=get_cat, dog=get_dog)Usage:
def main() -> None:
container = Container()
animal = container.get_animal()
reveal_type(animal) # Revealed type is AnyDesired behaviors
Ideally, Selector should:
- Allow users to specify type:
get_animal = providers.Selector[Animal](lambda: random.choice(["cat", "dog"]), cat=get_cat, dog=get_dog) - Automatically infer from the kwargs of providers: cat and dog are both animals, so get_animal is
Selector[Animal]