Typing and mypyΒΆ

Providers are mypy-friendly.

Providers module goes with the typing stubs. It provides typing information to mypy and your IDE.

from dependency_injector import providers


class Animal:
    ...


class Cat(Animal)
    ...


provider = providers.Factory(Cat)


if __name__ == '__main__':
    animal = provider()  # mypy knows that animal is of type "Cat"

You can use Provider as a generic type. This helps when a provider is an argument of a function or method.

from dependency_injector import providers


class Animal:
    ...


class Cat(Animal)
    ...


provider: providers.Provider[Animal] = providers.Factory(Cat)


if __name__ == '__main__':
    animal = provider()  # mypy knows that animal is of type "Animal"