From d2fdc31b5e05b483614d4450b477918c4ee47ac2 Mon Sep 17 00:00:00 2001 From: ChrisLR Date: Sun, 2 Apr 2023 17:00:27 -0400 Subject: [PATCH 1/2] Prevent child typeclasses class components from merging to inherited class components --- evennia/contrib/base_systems/components/holder.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/evennia/contrib/base_systems/components/holder.py b/evennia/contrib/base_systems/components/holder.py index 91d4330f58..7d19d344b0 100644 --- a/evennia/contrib/base_systems/components/holder.py +++ b/evennia/contrib/base_systems/components/holder.py @@ -36,9 +36,11 @@ class ComponentProperty: raise Exception("Cannot set a class property") def __set_name__(self, owner, name): - class_components = getattr(owner, "_class_components", None) + # Retrieve the class_components set on the direct class only + class_components = owner.__dict__.get("_class_components") if not class_components: - class_components = [] + # Create a new list, including inherited class components + class_components = list(getattr(owner, "_class_components", [])) setattr(owner, "_class_components", class_components) class_components.append((self.component_name, self.values)) From c1c7ccbca0184252495faa55f251c22d90a3d8d3 Mon Sep 17 00:00:00 2001 From: ChrisLR Date: Sun, 2 Apr 2023 22:11:01 -0400 Subject: [PATCH 2/2] Added test case --- evennia/contrib/base_systems/components/tests.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/evennia/contrib/base_systems/components/tests.py b/evennia/contrib/base_systems/components/tests.py index 3e90b67344..eb1717c939 100644 --- a/evennia/contrib/base_systems/components/tests.py +++ b/evennia/contrib/base_systems/components/tests.py @@ -42,6 +42,10 @@ class CharacterWithComponents(ComponentHolderMixin, DefaultCharacter): test_b = ComponentProperty("test_b", my_int=3, my_list=[1, 2, 3]) +class InheritedTCWithComponents(CharacterWithComponents): + test_c = ComponentProperty("test_c") + + class TestComponents(EvenniaTest): character_typeclass = CharacterWithComponents @@ -49,6 +53,14 @@ class TestComponents(EvenniaTest): assert self.char1.test_a assert self.char1.test_b + def test_inherited_typeclass_does_not_include_child_class_components(self): + char_with_c = create.create_object( + InheritedTCWithComponents, key="char_with_c", location=self.room1, home=self.room1 + ) + assert self.char1.test_a + assert not self.char1.cmp.get('test_c') + assert char_with_c.test_c + def test_character_instances_components_properly(self): assert isinstance(self.char1.test_a, ComponentTestA) assert isinstance(self.char1.test_b, ComponentTestB)