From 94039e06dc95ba7f994c28363e6b4693fbe3505f Mon Sep 17 00:00:00 2001 From: Thomas Adamcik Date: Wed, 29 Apr 2015 21:32:43 +0200 Subject: [PATCH] models: Make sure sub-classes can extend models --- mopidy/models/immutable.py | 3 ++- tests/models/test_models.py | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/mopidy/models/immutable.py b/mopidy/models/immutable.py index 2b7bfa5b..8e282c91 100644 --- a/mopidy/models/immutable.py +++ b/mopidy/models/immutable.py @@ -22,7 +22,8 @@ class ImmutableObjectMeta(type): attrs['_fields'] = fields attrs['_instances'] = weakref.WeakValueDictionary() - attrs['__slots__'] = ['_hash'] + fields.values() + attrs['__slots__'] = list(attrs.get('__slots__', [])) + attrs['__slots__'].extend(['_hash'] + fields.values()) for ancestor in [b for base in bases for b in inspect.getmro(base)]: if '__weakref__' in getattr(ancestor, '__slots__', []): diff --git a/tests/models/test_models.py b/tests/models/test_models.py index bdfd1896..f0f5ff6c 100644 --- a/tests/models/test_models.py +++ b/tests/models/test_models.py @@ -18,6 +18,14 @@ class InheritanceTest(unittest.TestCase): class Foo(Track): pass + def test_sub_class_can_have_its_own_slots(self): + + class Foo(Track): + __slots__ = ('_foo',) + + f = Foo() + f._foo = 123 + class CachingTest(unittest.TestCase):