Repository: ettoreleandrotognoli/python-cdi

Issue:
@Producer and register_instance
register_instance should find producers at instance

Discussion:

Patch:
diff --git a/pycdi/__init__.py b/pycdi/__init__.py
index fff012f..bf06f43 100644
--- a/pycdi/__init__.py
+++ b/pycdi/__init__.py
@@ -1,3 +1,3 @@
 # -*- encoding: utf-8 -*-
-from .core import Inject, Producer, CDIContainer
+from .core import Inject, Producer, CDIContainer, Component, Service
 from .utils import Singleton, Provide
diff --git a/pycdi/core.py b/pycdi/core.py
index 3e08943..918d34b 100644
--- a/pycdi/core.py
+++ b/pycdi/core.py
@@ -86,6 +86,10 @@ def last(it):
     return it[-1]
 
 
+def is_producer(it):
+    return getattr(it, INJECT_RETURN, False)
+
+
 def sorted_producers(producers):
     none_priority = filter(lambda it: first(it) is None, producers)
     with_priority = filter(lambda it: first(it) is not None, producers)
@@ -102,6 +106,11 @@ class PyCDIContainer(CDIContainer):
         producer = (lambda *args, **kwargs: instance)
         produce_type = type(instance) if produce_type is None else produce_type
         self.register_producer(producer, produce_type, context, priority)
+        for key, value in instance.__class__.__dict__.items():
+            produce_type = is_producer(value)
+            if produce_type is False:
+                continue
+            self.register_producer(getattr(instance, key), produce_type)
         return instance
 
     def register_producer(self, producer, produce_type=object, context=DEFAULT_CONTEXT, priority=None):
@@ -295,7 +304,7 @@ class Component(Inject):
         )
 
         for name, value in component_class.__dict__.items():
-            produce_type = getattr(value, INJECT_RETURN, False)
+            produce_type = is_producer(value)
             if produce_type is False:
                 continue
             Inject(component_class, _container=self.container)(value)


Test patch:
diff --git a/tests/__init__.py b/tests/__init__.py
index 4f1ca3f..a29340a 100644
--- a/tests/__init__.py
+++ b/tests/__init__.py
@@ -1,5 +1,10 @@
 import unittest
 
+from pycdi.core import DEFAULT_CONTAINER
+
 
 class TestCase(unittest.TestCase):
-    pass
+    container = None
+
+    def setUp(self):
+        self.container = DEFAULT_CONTAINER.sub_container()
diff --git a/tests/common/test_producer.py b/tests/common/test_producer.py
index 78d66d3..3871bec 100644
--- a/tests/common/test_producer.py
+++ b/tests/common/test_producer.py
@@ -1,8 +1,7 @@
 # -*- encoding: utf-8 -*-
-from tests import TestCase
-
 from pycdi import Producer, Inject
 from pycdi.core import DEFAULT_CONTAINER
+from tests import TestCase
 
 SOME_STRING = 'some_string'
 
@@ -50,3 +49,23 @@ class TestMultipleProducer(TestCase):
 
         DEFAULT_CONTAINER.call(function_with_injection)
         self.assertTrue(self.called)
+
+
+class TestProducersInInstance(TestCase):
+
+    def test_simple(self):
+        class NewType:
+            pass
+
+        new_type = NewType()
+
+        class WithProducer:
+
+            @Producer(produce_type=NewType)
+            def make_int(self):
+                return new_type
+
+        instance = WithProducer()
+        self.container.register_instance(instance)
+        produced = self.container.produce(NewType)
+        self.assertIs(produced, new_type)