python-injector / python-injector/injector

Multibind should respect the `@singleton` decorator

Open
#298 0 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

A failing test case:

def test_multibind_respects_the_singleton_decorator() -> None:
    @singleton
    class SingletonPlugin(Plugin):
        pass

    def configure(binder: Binder) -> None:
        binder.bind(Plugin, to=SingletonPlugin)
        binder.multibind(List[Plugin], to=SingletonPlugin)
        binder.multibind(List[Plugin], to=[SingletonPlugin])
        binder.multibind(Dict[str, Plugin], to={'singleton1': SingletonPlugin, 'singleton2': SingletonPlugin})

    injector = Injector([configure])
    plugin_singleton = injector.get(Plugin)
    plugins_list = injector.get(List[Plugin])
    plugins_dict = injector.get(Dict[str, Plugin])

    assert plugin_singleton is injector.get(Plugin)
    assert plugins_list[0] is plugins_list[1]  # Fails
    assert plugins_dict['singleton1'] is plugins_dict['singleton2']  # Fails

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 locating and running the provided test named test_multibind_respects_the_singleton_decorator. Trace the Injector behavior for the direct Plugin binding and the list and dictionary multibind cases. Done means the repeated SingletonPlugin entries resolve to the same instance in both collections and the assertions pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.