]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Fixed bug in the mutable extension whereby
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 1 Jul 2011 15:32:07 +0000 (11:32 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 1 Jul 2011 15:32:07 +0000 (11:32 -0400)
    if None or a non-corresponding type were set,
    an error would be raised.  None is now accepted
    which assigns None to all attributes,
    illegal values raise ValueError.

CHANGES
lib/sqlalchemy/ext/mutable.py
test/ext/test_mutable.py

diff --git a/CHANGES b/CHANGES
index 92881a3416464e24a25a5ce870bf7a19fbc419fd..4c2243f9fb1d297a0e69030eb3f3e53b734ddea8 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -89,6 +89,12 @@ CHANGES
     mapping, the attributes beyond the first
     would not get instrumented.
 
+  - Fixed bug in the mutable extension whereby
+    if None or a non-corresponding type were set, 
+    an error would be raised.  None is now accepted
+    which assigns None to all attributes,
+    illegal values raise ValueError.
+
 - examples
   - Repaired the examples/versioning test runner
     to not rely upon SQLAlchemy test libs,
index b31710ca8b80325036d9790ce3f154bf9dc5404b..ece7e3ad48209f7417131d5d0c97c292d4f62763 100644 (file)
@@ -346,6 +346,16 @@ class MutableBase(object):
 
         return weakref.WeakKeyDictionary()
 
+    @classmethod
+    def coerce(cls, key, value):
+        """Given a value, coerce it into this type.
+
+        By default raises ValueError.
+        """
+        if value is None:
+            return None
+        raise ValueError("Attribute '%s' does not accept objects of type %s" % (key, type(value)))
+
     @classmethod
     def _listen_on_attribute(cls, attribute, coerce, parent_cls):
         """Establish this type as a mutation listener for the given 
@@ -423,16 +433,6 @@ class Mutable(MutableBase):
         for parent, key in self._parents.items():
             flag_modified(parent, key)
 
-    @classmethod
-    def coerce(cls, key, value):
-        """Given a value, coerce it into this type.
-
-        By default raises ValueError.
-        """
-        if value is None:
-            return None
-        raise ValueError("Attribute '%s' does not accept objects of type %s" % (key, type(value)))
-
     @classmethod
     def associate_with_attribute(cls, attribute):
         """Establish this type as a mutation listener for the given 
index 9d0a4a0c89cdf21506a396396d6a681da0132846..df4006c01b45709009486d90e8662169abce99aa 100644 (file)
@@ -355,6 +355,24 @@ class MutableCompositesTest(_CompositeTestBase, fixtures.MappedTest):
             f2.data.y = 12
             assert f2 in sess.dirty
 
+    def test_set_none(self):
+        sess = Session()
+        f1 = Foo(data=None)
+        sess.add(f1)
+        sess.commit()
+        eq_(f1.data, Point(None, None))
+
+        f1.data.y = 5
+        sess.commit()
+        eq_(f1.data, Point(None, 5))
+
+    def test_set_illegal(self):
+        f1 = Foo()
+        assert_raises_message(
+            ValueError,
+            "Attribute 'data' does not accept objects",
+            setattr, f1, 'data', 'foo'
+        )
 
 class MutableInheritedCompositesTest(_CompositeTestBase, fixtures.MappedTest):
     @classmethod