python-injector / python-injector/injector
Singleton annotated class instances are separate for interface and implementation binding
Open
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.5k
- Forks
- 94
- PR merge metrics
- No merged PRs in 30d
Description
Unless I am doing something wrong, I will expect following binding to get single instance, but I am getting two different instances:
Code (python3):
#!/usr/bin/env python3
import abc
import injector
class SomeInterface(metaclass=abc.ABCMeta):
"""Interface with multiple implementations."""
@abc.abstractmethod
def some_method(self) -> None:
pass
@injector.singleton
class SomeImplementation(SomeInterface):
"""One implementation."""
def __init__(self):
print(f'Created instance: {self}')
def some_method(self) -> None:
pass
class SomeModule(injector.Module):
"""Injector module."""
def configure(self, binder: injector.Binder) -> None:
binder.bind(SomeImplementation)
binder.bind(SomeInterface, to=SomeImplementation)
def test_singleton() -> None:
inj = injector.Injector((SomeModule(),), auto_bind=False)
# Following two create different instances. Expected to be same since the implementation is marked singleton
print(inj.get(SomeInterface))
print(inj.get(SomeImplementation))
# These get the two instances created above.
print(inj.get(SomeInterface))
print(inj.get(SomeImplementation))
if __name__ == '__main__':
test_singleton()
Output:
Created instance: <cli.cli_lib.SomeImplementation object at 0x7f69b1bb3ef0>
<cli.cli_lib.SomeImplementation object at 0x7f69b1bb3ef0>
Created instance: <cli.cli_lib.SomeImplementation object at 0x7f69b1bb3e80>
<cli.cli_lib.SomeImplementation object at 0x7f69b1bb3e80>
<cli.cli_lib.SomeImplementation object at 0x7f69b1bb3ef0>
<cli.cli_lib.SomeImplementation object at 0x7f69b1bb3e80>
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the provided Python example, then trace how bindings for SomeInterface and SomeImplementation are resolved and cached. The issue names no repository files or tests; done means both lookups return the same singleton instance while preserving the interface-to-implementation binding.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100