[1.7.x] Fixed #23560 -- Fixed MigrationWrite to handle builtin types without imports.

Thanks Tim Graham for the review.

Backport of b23d47412c from master
This commit is contained in:
Loic Bistuer 2014-09-27 00:01:34 +07:00
parent 091289d950
commit 7355885ef5
3 changed files with 12 additions and 1 deletions

View File

@ -351,7 +351,10 @@ class MigrationWriter(object):
return string, set(imports) return string, set(imports)
if hasattr(value, "__module__"): if hasattr(value, "__module__"):
module = value.__module__ module = value.__module__
return "%s.%s" % (module, value.__name__), set(["import %s" % module]) if module == six.moves.builtins.__name__:
return value.__name__, set()
else:
return "%s.%s" % (module, value.__name__), set(["import %s" % module])
# Other iterables # Other iterables
elif isinstance(value, collections.Iterable): elif isinstance(value, collections.Iterable):
imports = set() imports = set()

View File

@ -83,3 +83,6 @@ Bugfixes
* Coerced the ``related_name`` model field option to unicode during migration * Coerced the ``related_name`` model field option to unicode during migration
generation to generate migrations that work with both Python 2 and 3 generation to generate migrations that work with both Python 2 and 3
(:ticket:`23455`). (:ticket:`23455`).
* Fixed ``MigrationWriter`` to handle builtin types without imports
(:ticket:`23560`).

View File

@ -81,6 +81,11 @@ class WriterTests(TestCase):
self.assertSerializedEqual(set([2, 3, "eighty"])) self.assertSerializedEqual(set([2, 3, "eighty"]))
self.assertSerializedEqual({"lalalala": ["yeah", "no", "maybe"]}) self.assertSerializedEqual({"lalalala": ["yeah", "no", "maybe"]})
self.assertSerializedEqual(_('Hello')) self.assertSerializedEqual(_('Hello'))
# Builtins
self.assertSerializedEqual([list, tuple, dict, set])
string, imports = MigrationWriter.serialize([list, tuple, dict, set])
self.assertEqual(string, "[list, tuple, dict, set]")
self.assertEqual(imports, set())
# Functions # Functions
with six.assertRaisesRegex(self, ValueError, 'Cannot serialize function: lambda'): with six.assertRaisesRegex(self, ValueError, 'Cannot serialize function: lambda'):
self.assertSerializedEqual(lambda x: 42) self.assertSerializedEqual(lambda x: 42)