models: Make .copy(foo=None) null out field.

This commit is contained in:
Thomas Adamcik 2014-01-15 19:39:11 +01:00
parent 807bedad85
commit b0b5d4972f
2 changed files with 10 additions and 2 deletions

View File

@ -69,10 +69,14 @@ class ImmutableObject(object):
data = {}
for key in self.__dict__.keys():
public_key = key.lstrip('_')
data[public_key] = values.pop(public_key, self.__dict__[key])
value = values.pop(public_key, self.__dict__[key])
if value is not None:
data[public_key] = value
for key in values.keys():
if hasattr(self, key):
data[key] = values.pop(key)
value = values.pop(key)
if value is not None:
data[key] = value
if values:
raise TypeError(
'copy() got an unexpected keyword argument "%s"' % key)

View File

@ -53,6 +53,10 @@ class GenericCopyTest(unittest.TestCase):
test = lambda: Track().copy(invalid_key=True)
self.assertRaises(TypeError, test)
def test_copying_track_to_remove(self):
track = Track(name='foo').copy(name=None)
self.assertEquals(track.__dict__, Track().__dict__)
class RefTest(unittest.TestCase):
def test_uri(self):