use specific Injector instance as `inject` decorator

Open
#249 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
30/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
python
Domain
backend

Research direction

Start by tracing the existing inject decorator and Injector behavior, then compare them with the container.get workaround described here. Done means the project has a decided API for selecting a specific Injector instance at decoration time and clearly defined behavior for modules with different dependency configurations.

Written by the indexing model from the issue text.

Description

This has been in my mind for some time so I decided to finally write it.

Would that be possible to specify the Injector instance that should be used to inject dependencies? I see it the following way

container = Injector([module])

...

@container.inject    # or maybe @inject(container)
def do_something(a: Dependency):
    ...

My motivation is that I have multiple python modules, each one configuring it's own dependencies so each module (in general) expects different instances of dependencies.

I know that the proposed solution requires a variable container to be defined at interpretation time but not always is that undesireable. What do you think?

My workaround currently is calling container.get but I don't really like it.

Or is there something I don't understand about how injector works and inject chooses "the most local" container?

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

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.

More from python-injector/injector

All issues in python-injector/injector

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.