This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 87
Why a class with @pinject.inject is always singleton? Is it a bug? #40
Comments
By default Pinject uses singleton, you can specify
https://github.com/google/pinject/blob/master/README.rst#scopes |
I'm facing the same issue. class MyRegistry:
def reg(self):
return 42
class FooClass:
@pinject.inject(['my_registry'])
def __init__(self, my_registry, param):
self.my_registry = my_registry
self.param = param
class MainClass:
def __init__(self, provide_foo_class):
a = provide_foo_class(param=1)
b = provide_foo_class(param=2)
assert a.param != b.param
class MyBindingSpec(pinject.BindingSpec):
def configure(self, bind):
bind('foo_class', to_class=FooClass, in_scope=pinject.PROTOTYPE)
if __name__ == '__main__':
obj_graph = pinject.new_object_graph(binding_specs=[MyBindingSpec()])
obj_graph.provide(MainClass)
|
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
OS: macOS 10.14.5
Python: 3.7
Dependencies:
Thank you for the lib. I enjoy your design decisions which make it the closest thing to dependency injection in Python I've seen so far. I'd like to use it. But have a problem in the code as follows:
I tried to override
FooClass
scope in custom spec, but it throws the exception of ambiguity sinceFooClass
is registered assingleton
. I can work around it with theFooClassFactory
that creates instances ofFooClass
, but it seems unnecessary when there isprovide_
facility.How to make it work properly?
The text was updated successfully, but these errors were encountered: