python-injector / python-injector/injector

Binding multiple interfaces to same implementation

Open
#206 2 comments 1 reaction 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!

This issue is somewhat related to #181. Let's say I have the following code structure:

from abc import ABC, abstractmethod


class FileReader(ABC):
    @abstractmethod
    def read(self, file_path: str) -> str:
        pass


class FileWriter(ABC):
    @abstractmethod
    def write(self, file_path: str) -> None:
        pass


class FileHandler(FileReader, FileWriter):
    def read(self, file_path: str) -> str:
        # Do file reading stuff

    def write(self, file_path: str) -> None:
        # Do file writing stuff

Essentially a class that implements two interfaces, where only one interface may be needed elsewhere at a given time (for instance, many classes may need to read the files but only a few may need to write to them).

The FileHandler class could be instantiated as a Singleton, which in turn makes it reasonable that both FileReader and FileWriter bind to the same object. As is mentioned in #181, the following does not work (it creates two instances of FileHandler):

injector = Injector()

injector.binder.bind(FileReader, to=FileHandler, scope=singleton)
injector.binder.bind(FileWriter, to=FileHandler, scope=singleton)

Instead, it is suggested to create a Module doing the following:

class FileModule(injector.Module):
    def configure(self, binder: injector.Binder) -> None:
        binder.bind(FileHandler, scope=singleton)

    @provider
    def provide_reader(self, implementation: FileHandler) -> FileReader:
        return implementation

    @provider
    def provide_writer(self, implementation: FileHandler) -> FileWriter:
        return implementation

This seems like a lot of code for a binding of several interfaces to one instance. In other DI frameworks, I've seen syntax similar to:

injector.binder.bind([FileReader, FileWriter], to=FileHandler, scope=singleton)

I don't know if I've missed something, but is this possible to do in injector? If not, I think it would be a nice addition to the library.

Thanks!

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 by reviewing the binder.bind API and the Module/provider pattern shown in the issue, along with related issue #181. Determine how multiple interface keys could resolve to one FileHandler singleton, then add coverage demonstrating shared identity for FileReader and FileWriter bindings and confirm the existing binding behavior remains intact.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
developer-experience
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.