models: Make sure parent fields are used by children.

Without this change any sub-class would end up with an empty _fields and none
of the actual fields would be writable even internally.
This commit is contained in:
Thomas Adamcik 2015-05-03 22:58:43 +02:00
parent 94039e06dc
commit bb95dc3b9b
2 changed files with 15 additions and 1 deletions

View File

@ -15,7 +15,9 @@ class ImmutableObjectMeta(type):
def __new__(cls, name, bases, attrs):
fields = {}
for key, value in attrs.items():
for base in bases: # Copy parent fields over to our state
fields.update(getattr(base, '_fields', {}))
for key, value in attrs.items(): # Add our own fields
if isinstance(value, Field):
fields[key] = '_' + key
value._name = key

View File

@ -19,6 +19,7 @@ class InheritanceTest(unittest.TestCase):
pass
def test_sub_class_can_have_its_own_slots(self):
# Needed for things like SpotifyTrack in mopidy-spotify 1.x
class Foo(Track):
__slots__ = ('_foo',)
@ -26,6 +27,17 @@ class InheritanceTest(unittest.TestCase):
f = Foo()
f._foo = 123
def test_sub_class_can_be_initialized(self):
# Fails with following error if fields are not handled across classes.
# TypeError: __init__() got an unexpected keyword argument "type"
# Essentially this is testing that sub-classes take parent _fields into
# account.
class Foo(Ref):
pass
Foo.directory()
class CachingTest(unittest.TestCase):