diff options
author | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2012-01-13 00:01:01 +0100 |
---|---|---|
committer | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2012-01-13 00:01:01 +0100 |
commit | 6329a894c0b148f1c8bba2ebf224201419cf44ff (patch) | |
tree | 936a0bd7673840016dc49ddad6f2618e0e4e66ca | |
parent | c60c3106c700f5f42ad4fcf82f327da98ec5eb9e (diff) | |
download | factory-boy-6329a894c0b148f1c8bba2ebf224201419cf44ff.tar factory-boy-6329a894c0b148f1c8bba2ebf224201419cf44ff.tar.gz |
Add a PendingDeprecationWarning on associated class auto-discovery.
Signed-off-by: Raphaël Barrois <raphael.barrois@polytechnique.org>
-rw-r--r-- | factory/base.py | 30 | ||||
-rw-r--r-- | tests/test_base.py | 16 |
2 files changed, 35 insertions, 11 deletions
diff --git a/factory/base.py b/factory/base.py index b2f437d..c17b7ce 100644 --- a/factory/base.py +++ b/factory/base.py @@ -22,6 +22,7 @@ import re import sys +import warnings from factory import containers @@ -156,29 +157,36 @@ class FactoryMetaClass(BaseFactoryMetaClass): if FACTORY_CLASS_DECLARATION in attrs: return attrs[FACTORY_CLASS_DECLARATION] + # No specific associated calss was given, and one was defined for our + # parent, use it. + if inherited is not None: + return inherited + if '__module__' in attrs: factory_module = sys.modules[attrs['__module__']] if class_name.endswith('Factory'): # Try a module lookup used_auto_discovery = True - associated_class_name = class_name[:-len('Factory')] - if associated_class_name: - # Class name was longer than just 'Factory'. - try: - return getattr(factory_module, associated_class_name) - except AttributeError: - pass + associated_name = class_name[:-len('Factory')] + if associated_name and hasattr(factory_module, associated_name): + warnings.warn( + "Auto-discovery of associated class is deprecated, and " + "will be removed in the future. Please set '%s = %s' " + "in the %s class definition." % ( + FACTORY_CLASS_DECLARATION, + associated_name, + class_name, + ), PendingDeprecationWarning) + + return getattr(factory_module, associated_name) # Unable to guess a good option; return the inherited class. - if inherited is not None: - return inherited - # Unable to find an associated class; fail. if used_auto_discovery: raise Factory.AssociatedClassError( FactoryMetaClass.ERROR_MESSAGE_AUTODISCOVERY.format( FACTORY_CLASS_DECLARATION, - associated_class_name, + associated_name, class_name, factory_module,)) else: diff --git a/tests/test_base.py b/tests/test_base.py index 4f77421..8da655e 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -21,6 +21,7 @@ # THE SOFTWARE. import unittest +import warnings from factory import base from factory import declarations @@ -153,6 +154,21 @@ class FactoryCreationTestCase(unittest.TestCase): self.assertTrue(isinstance(TestObjectFactory.build(), TestObject)) + def testDeprecationWarning(self): + """Make sure the 'auto-discovery' deprecation warning is issued.""" + + with warnings.catch_warnings(record=True) as w: + # Clear the warning registry. + if hasattr(base, '__warningregistry__'): + base.__warningregistry__.clear() + + warnings.simplefilter('always') + class TestObjectFactory(base.Factory): + pass + + self.assertEqual(1, len(w)) + self.assertIn('deprecated', str(w[0].message)) + def testStub(self): class TestFactory(base.StubFactory): pass |