pombreda / appengine-ndb-experiment

Automatically exported from code.google.com/p/appengine-ndb-experiment
Other
0 stars 0 forks source link

Default value's properties get mutated (if it's a mutable object) #228

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
class InnerEntity(ndb.Model):
    value = ndb.IntegerProperty()

class TestEntity(ndb.Model):
    baz = ndb.StructuredProperty(InnerEntity, default=InnerEntity(value=42))

We can observe the following behavior:

e1 = TestEntity()
self.assertEqual(42, e1.baz.value) #ok, we have the default value
e2 = TestEntity()
self.assertEqual(42, e2.baz.value) #here too
e2.baz.value = 7
self.assertEqual(7, e2.baz.value) #ok, we updated the value
self.assertEqual(42, e1.baz.value) #whoa! spooky action at a distance!

I uploaded a patch to fix this behaviour by deepcopying the default value 
before returning: https://codereview.appspot.com/6870063/

Original issue reported on code.google.com by dify....@gmail.com on 5 Dec 2012 at 12:53

GoogleCodeExporter commented 9 years ago
I just spent two days chasing a bug that finally turned out to be caused by 
default mutable values. I realize that this is similar to how default params 
work in python functions, but I didn't expect ndb to follow that rule. Please 
apply the provided patch and spare other users the pain.

Original comment by wal...@ninua.com on 21 Nov 2013 at 7:45

GoogleCodeExporter commented 9 years ago
I no longer have control over this project and I don't know if Google watches 
this tracker. If you want a response please use the official App Engine tracker.

Original comment by gvanrossum@gmail.com on 21 Nov 2013 at 3:21

GoogleCodeExporter commented 9 years ago

Original comment by pcoste...@google.com on 27 Nov 2013 at 5:32

GoogleCodeExporter commented 9 years ago

Original comment by pcoste...@google.com on 27 Nov 2013 at 5:36

GoogleCodeExporter commented 9 years ago
Whoa, got bit by this bug too right now.

For some reason, using `LocalStructuredProperty` doesn't have this issue.

Original comment by hq%ozbur...@gtempaccount.com on 18 Jun 2014 at 12:38