Skip to content

Differentiating between multiple providers #234

Open
@cswartzvi

Description

@cswartzvi

Hi! First off, this is a fantastic library - thank you for all your efforts. If I define multiple providers for the same type and then pass both providers to the Injector, the "last" provider takes precedence (see below). While I am pretty sure this is intentional - and aligns nicely with how python handles similarly named functions - I was wondering if there was a way to a) differentiate between the providers list the multiple providers for a type or b) raise an exception if multiple providers have been defined for the same type. I would be happy to contribute a PR if you think there are any worthwhile features to be added. Thanks again!

from dataclasses import dataclass
from injector import Module, singleton, provider, Injector


@dataclass
class Output:
    value: str


class Provider01(Module):
    @singleton
    @provider
    def provide_output(self) -> Output:
        return Output("Using Provider01")


class Provider02(Module):
    @singleton
    @provider
    def provide_output(self) -> Output:
        return Output("Using Provider02")


injector = Injector([Provider01(), Provider02()])
output = injector.get(Output)
print(output.value)  # Using Provider02

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions