From 75765b6722c906cd9241a6f433fdcc5d34788d08 Mon Sep 17 00:00:00 2001
From: Stefano Borini <sborini@enthought.com>
Date: Wed, 25 Apr 2018 10:50:15 +0100
Subject: [PATCH] Using types, not Instance. Needs fixing of circular imports

---
 force_bdss/data_sources/base_data_source.py   |  2 +-
 .../data_sources/base_data_source_factory.py  |  9 ++++-----
 .../data_sources/i_data_source_factory.py     |  6 +++---
 force_bdss/kpi/base_kpi_calculator_factory.py |  6 +++---
 force_bdss/kpi/i_kpi_calculator_factory.py    |  6 +++---
 .../tests/test_base_kpi_calculator_factory.py | 19 +++++++++++++++++++
 force_bdss/mco/base_mco_factory.py            |  8 ++++----
 force_bdss/mco/i_mco_factory.py               |  8 ++++----
 .../base_notification_listener_factory.py     |  6 +++---
 force_bdss/ui_hooks/base_ui_hooks_factory.py  |  4 ++--
 10 files changed, 46 insertions(+), 28 deletions(-)

diff --git a/force_bdss/data_sources/base_data_source.py b/force_bdss/data_sources/base_data_source.py
index 2102b40..81a46a5 100644
--- a/force_bdss/data_sources/base_data_source.py
+++ b/force_bdss/data_sources/base_data_source.py
@@ -1,5 +1,5 @@
-from traits.api import ABCHasStrictTraits, Instance
 import abc
+from traits.api import ABCHasStrictTraits, Instance
 
 from ..data_sources.i_data_source_factory import IDataSourceFactory
 
diff --git a/force_bdss/data_sources/base_data_source_factory.py b/force_bdss/data_sources/base_data_source_factory.py
index 6d1a3e8..b276613 100644
--- a/force_bdss/data_sources/base_data_source_factory.py
+++ b/force_bdss/data_sources/base_data_source_factory.py
@@ -1,10 +1,10 @@
 import logging
-from traits.api import ABCHasStrictTraits, provides, String, Instance
+from traits.api import ABCHasStrictTraits, provides, String, Instance, Type
 from envisage.plugin import Plugin
 
 from force_bdss.data_sources.base_data_source import BaseDataSource
 from force_bdss.data_sources.base_data_source_model import BaseDataSourceModel
-from .i_data_source_factory import IDataSourceFactory
+from force_bdss.data_sources.i_data_source_factory import IDataSourceFactory
 
 log = logging.getLogger(__name__)
 
@@ -24,12 +24,11 @@ class BaseDataSourceFactory(ABCHasStrictTraits):
     name = String()
 
     #: The data source to be instantiated. Define this to your DataSource
-    data_source_class = Instance(BaseDataSource)
+    data_source_class = Type(BaseDataSource)
 
     #: The model associated to the data source.
     #: Define this to your DataSourceModel
-    model_class = Instance(BaseDataSourceModel)
-
+    model_class = Type(BaseDataSourceModel)
 
     #: Reference to the plugin that carries this factory
     #: This is automatically set by the system. you should not define it
diff --git a/force_bdss/data_sources/i_data_source_factory.py b/force_bdss/data_sources/i_data_source_factory.py
index 080c12f..7cbaa3b 100644
--- a/force_bdss/data_sources/i_data_source_factory.py
+++ b/force_bdss/data_sources/i_data_source_factory.py
@@ -1,5 +1,5 @@
 from envisage.api import Plugin
-from traits.api import Interface, String, Instance
+from traits.api import Interface, String, Instance, Type
 
 from force_bdss.data_sources.base_data_source import BaseDataSource
 from force_bdss.data_sources.base_data_source_model import BaseDataSourceModel
@@ -15,9 +15,9 @@ class IDataSourceFactory(Interface):
 
     name = String()
 
-    data_source_class = Instance(BaseDataSource)
+    data_source_class = Type(BaseDataSource)
 
-    model_class = Instance(BaseDataSourceModel)
+    model_class = Type(BaseDataSourceModel)
 
     plugin = Instance(Plugin)
 
diff --git a/force_bdss/kpi/base_kpi_calculator_factory.py b/force_bdss/kpi/base_kpi_calculator_factory.py
index f9b4c30..a2b7e8b 100644
--- a/force_bdss/kpi/base_kpi_calculator_factory.py
+++ b/force_bdss/kpi/base_kpi_calculator_factory.py
@@ -1,6 +1,6 @@
 import logging
 from envisage.plugin import Plugin
-from traits.api import ABCHasStrictTraits, provides, String, Instance
+from traits.api import ABCHasStrictTraits, provides, String, Instance, Type
 
 from force_bdss.kpi.base_kpi_calculator import BaseKPICalculator
 from force_bdss.kpi.base_kpi_calculator_model import BaseKPICalculatorModel
@@ -26,11 +26,11 @@ class BaseKPICalculatorFactory(ABCHasStrictTraits):
     name = String()
 
     #: The KPI calculator to be instantiated. Define this to your KPICalculator
-    kpi_calculator_class = Instance(BaseKPICalculator)
+    kpi_calculator_class = Type(BaseKPICalculator)
 
     #: The model associated to the KPI calculator.
     #: Define this to your KPICalculatorModel
-    model_class = Instance(BaseKPICalculatorModel)
+    model_class = Type(BaseKPICalculatorModel)
 
     #: A reference to the plugin that holds this factory.
     plugin = Instance(Plugin)
diff --git a/force_bdss/kpi/i_kpi_calculator_factory.py b/force_bdss/kpi/i_kpi_calculator_factory.py
index 87d00d0..dc2a163 100644
--- a/force_bdss/kpi/i_kpi_calculator_factory.py
+++ b/force_bdss/kpi/i_kpi_calculator_factory.py
@@ -1,4 +1,4 @@
-from traits.api import Interface, String, Instance
+from traits.api import Interface, String, Instance, Type
 from envisage.plugin import Plugin
 
 from force_bdss.kpi.base_kpi_calculator import BaseKPICalculator
@@ -15,9 +15,9 @@ class IKPICalculatorFactory(Interface):
 
     name = String()
 
-    kpi_calculator_class = Instance(BaseKPICalculator)
+    kpi_calculator_class = Type(BaseKPICalculator)
 
-    model_class = Instance(BaseKPICalculatorModel)
+    model_class = Type(BaseKPICalculatorModel)
 
     plugin = Instance(Plugin)
 
diff --git a/force_bdss/kpi/tests/test_base_kpi_calculator_factory.py b/force_bdss/kpi/tests/test_base_kpi_calculator_factory.py
index b6b26bb..188bc4c 100644
--- a/force_bdss/kpi/tests/test_base_kpi_calculator_factory.py
+++ b/force_bdss/kpi/tests/test_base_kpi_calculator_factory.py
@@ -1,6 +1,9 @@
 import unittest
 from envisage.plugin import Plugin
 
+from force_bdss.kpi.base_kpi_calculator import BaseKPICalculator
+from force_bdss.kpi.base_kpi_calculator_model import BaseKPICalculatorModel
+
 try:
     import mock
 except ImportError:
@@ -9,6 +12,9 @@ except ImportError:
 from force_bdss.kpi.base_kpi_calculator_factory import \
     BaseKPICalculatorFactory
 
+kpi_calculator = mock.Mock(spec=BaseKPICalculator)
+kpi_calculator_model = mock.Mock(spec=BaseKPICalculatorModel)
+
 
 class DummyKPICalculatorFactory(BaseKPICalculatorFactory):
     id = "foo"
@@ -22,8 +28,21 @@ class DummyKPICalculatorFactory(BaseKPICalculatorFactory):
         pass
 
 
+class DummyKPICalculatorFactory2(BaseKPICalculatorFactory):
+    id = "foo"
+
+    name = "bar"
+
+    kpi_calculator_class = kpi_calculator
+
+    model_class = kpi_calculator_model
+
+
 class TestBaseKPICalculatorFactory(unittest.TestCase):
     def test_initialization(self):
         factory = DummyKPICalculatorFactory(mock.Mock(spec=Plugin))
         self.assertEqual(factory.id, 'foo')
         self.assertEqual(factory.name, 'bar')
+
+    def test_fast_definition(self):
+        factory = DummyKPICalculatorFactory2(mock.Mock(spec=Plugin))
diff --git a/force_bdss/mco/base_mco_factory.py b/force_bdss/mco/base_mco_factory.py
index 00879c8..281351e 100644
--- a/force_bdss/mco/base_mco_factory.py
+++ b/force_bdss/mco/base_mco_factory.py
@@ -1,5 +1,5 @@
 import logging
-from traits.api import ABCHasStrictTraits, String, provides, Instance
+from traits.api import ABCHasStrictTraits, String, provides, Instance, Type
 from envisage.plugin import Plugin
 
 from force_bdss.mco.base_mco import BaseMCO
@@ -24,13 +24,13 @@ class BaseMCOFactory(ABCHasStrictTraits):
     name = String()
 
     #: The optimizer class to instantiate. Define this to your MCO class.
-    optimizer_class = Instance(BaseMCO)
+    optimizer_class = Type(BaseMCO)
 
     #: The model associated to the MCO. Define this to your MCO model class.
-    model_class = Instance(BaseMCOModel)
+    model_class = Type(BaseMCOModel)
 
     #: The communicator associated to the MCO. Define this to your MCO comm.
-    communicator_class = Instance(BaseMCOCommunicator)
+    communicator_class = Type(BaseMCOCommunicator)
 
     #: A reference to the Plugin that holds this factory.
     plugin = Instance(Plugin)
diff --git a/force_bdss/mco/i_mco_factory.py b/force_bdss/mco/i_mco_factory.py
index aef7c06..9ddfeb7 100644
--- a/force_bdss/mco/i_mco_factory.py
+++ b/force_bdss/mco/i_mco_factory.py
@@ -1,4 +1,4 @@
-from traits.api import Interface, String, Instance
+from traits.api import Interface, String, Instance, Type
 from envisage.plugin import Plugin
 
 from force_bdss.mco.base_mco import BaseMCO
@@ -16,11 +16,11 @@ class IMCOFactory(Interface):
 
     name = String()
 
-    optimizer_class = Instance(BaseMCO)
+    optimizer_class = Type(BaseMCO)
 
-    model_class = Instance(BaseMCOModel)
+    model_class = Type(BaseMCOModel)
 
-    communicator_class = Instance(BaseMCOCommunicator)
+    communicator_class = Type(BaseMCOCommunicator)
 
     plugin = Instance(Plugin)
 
diff --git a/force_bdss/notification_listeners/base_notification_listener_factory.py b/force_bdss/notification_listeners/base_notification_listener_factory.py
index 76f9025..d3a4c68 100644
--- a/force_bdss/notification_listeners/base_notification_listener_factory.py
+++ b/force_bdss/notification_listeners/base_notification_listener_factory.py
@@ -1,5 +1,5 @@
 import logging
-from traits.api import ABCHasStrictTraits, Instance, String, provides
+from traits.api import ABCHasStrictTraits, Instance, String, provides, Type
 from envisage.plugin import Plugin
 
 from force_bdss.notification_listeners.base_notification_listener import \
@@ -26,11 +26,11 @@ class BaseNotificationListenerFactory(ABCHasStrictTraits):
 
     #: The listener class that must be instantiated. Define this to your
     #: listener class.
-    listener_class = Instance(BaseNotificationListener)
+    listener_class = Type(BaseNotificationListener)
 
     #: The associated model to the listener. Define this to your
     #: listener model class.
-    model_class = Instance(BaseNotificationListenerModel)
+    model_class = Type(BaseNotificationListenerModel)
 
     #: A reference to the containing plugin
     plugin = Instance(Plugin)
diff --git a/force_bdss/ui_hooks/base_ui_hooks_factory.py b/force_bdss/ui_hooks/base_ui_hooks_factory.py
index 9dbb093..adb07b9 100644
--- a/force_bdss/ui_hooks/base_ui_hooks_factory.py
+++ b/force_bdss/ui_hooks/base_ui_hooks_factory.py
@@ -1,5 +1,5 @@
 import logging
-from traits.api import ABCHasStrictTraits, Instance, String, provides
+from traits.api import ABCHasStrictTraits, Instance, String, provides, Type
 from envisage.plugin import Plugin
 
 from force_bdss.ui_hooks.base_ui_hooks_manager import BaseUIHooksManager
@@ -22,7 +22,7 @@ class BaseUIHooksFactory(ABCHasStrictTraits):
 
     #: The UI Hooks manager class to instantiate. Define this to your
     #: base hook managers.
-    ui_hooks_manager_class = Instance(BaseUIHooksManager)
+    ui_hooks_manager_class = Type(BaseUIHooksManager)
 
     #: A reference to the containing plugin
     plugin = Instance(Plugin)
-- 
GitLab