From 38dfde05f5be3cdd69e6fee66e7968b776b0ed9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Barrois?= Date: Mon, 29 Apr 2013 23:00:32 +0200 Subject: declarations: Rename RelatedFactory.name (See #58). MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use less conflict-prone factory_related_name. Signed-off-by: Raphaƫl Barrois --- docs/changelog.rst | 4 ++++ docs/reference.rst | 4 ++-- factory/declarations.py | 14 ++++++++++++-- tests/test_declarations.py | 11 +++++++++++ 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index f072b1c..da22b13 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -9,6 +9,10 @@ ChangeLog - Add :class:`~factory.fuzzy.FuzzyDate` thanks to `saulshanabrook `_ - Add :class:`~factory.fuzzy.FuzzyDateTime` and :class:`~factory.fuzzy.FuzzyNaiveDateTime`. +*Deprecation:* + + - Rename :class:`~factory.RelatedFactory`'s ``name`` argument to ``factory_related_name`` (See #58) + 2.0.2 (2013-04-16) ------------------ diff --git a/docs/reference.rst b/docs/reference.rst index ad91a8d..09d9ceb 100644 --- a/docs/reference.rst +++ b/docs/reference.rst @@ -1025,7 +1025,7 @@ as keyword arguments; ``{'post_x': 2}`` will be passed to ``SomeFactory.FACTORY_ RelatedFactory """""""""""""" -.. class:: RelatedFactory(factory, name='', **kwargs) +.. class:: RelatedFactory(factory, factory_related_name='', **kwargs) .. OHAI_VIM** @@ -1045,7 +1045,7 @@ RelatedFactory .. attribute:: name The generated object (where the :class:`RelatedFactory` attribute will - set) may be passed to the related factory if the :attr:`name` parameter + set) may be passed to the related factory if the :attr:`factory_related_name` parameter is set. It will be passed as a keyword argument, using the :attr:`name` value as diff --git a/factory/declarations.py b/factory/declarations.py index 974b4ac..17f4434 100644 --- a/factory/declarations.py +++ b/factory/declarations.py @@ -22,6 +22,7 @@ import itertools +import warnings from . import compat from . import utils @@ -428,9 +429,18 @@ class RelatedFactory(PostGenerationDeclaration): calling the related factory """ - def __init__(self, factory, name='', **defaults): + def __init__(self, factory, factory_related_name='', **defaults): super(RelatedFactory, self).__init__() - self.name = name + if factory_related_name == '' and defaults.get('name') is not None: + warnings.warn( + "Usage of RelatedFactory(SomeFactory, name='foo') is deprecated" + " and will be removed in the future. Please use the" + " RelatedFactory(SomeFactory, 'foo') or" + " RelatedFactory(SomeFactory, factory_related_name='foo')" + " syntax instead", PendingDeprecationWarning, 2) + factory_related_name = defaults.pop('name') + + self.name = factory_related_name self.defaults = defaults if isinstance(factory, type): diff --git a/tests/test_declarations.py b/tests/test_declarations.py index 4c08dfa..90e54c2 100644 --- a/tests/test_declarations.py +++ b/tests/test_declarations.py @@ -210,6 +210,17 @@ class RelatedFactoryTestCase(unittest.TestCase): # IMPORTANT: restore attribute. datetime.date = orig_date + def test_deprecate_name(self): + with warnings.catch_warnings(record=True) as w: + + warnings.simplefilter('always') + f = declarations.RelatedFactory('datetime.date', name='blah') + + self.assertEqual('blah', f.name) + self.assertEqual(1, len(w)) + self.assertIn('RelatedFactory', str(w[0].message)) + self.assertIn('factory_related_name', str(w[0].message)) + class PostGenerationMethodCallTestCase(unittest.TestCase): def setUp(self): -- cgit v1.2.3