python-injector / python-injector/injector

Differentiating between multiple providers

Open
#234 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.5k
Forks
94
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Injector entry point and review how multiple provider modules are registered and resolved for the same type. Clarify whether the desired outcome is listing providers, differentiating them, or raising an exception, then define tests that distinguish the selected behavior from the current last-provider precedence.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.