Merge pull request #3537 from ChrisLR/fix-component-fields-rm

[Components] Fix components improperly setting _fields
This commit is contained in:
Griatch 2024-06-14 11:41:46 +02:00 committed by GitHub
commit 15c4888f53
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 33 additions and 5 deletions

View file

@ -15,15 +15,22 @@ class BaseComponent(type):
This is the metaclass for components, This is the metaclass for components,
responsible for registering components to the listing. responsible for registering components to the listing.
""" """
def __new__(cls, name, parents, attrs):
@classmethod
def __new__(cls, *args):
""" """
Every class that uses this metaclass will be registered Every class that uses this metaclass will be registered
as a component in the Component Listing using its name. as a component in the Component Listing using its name.
All of them require a unique name. All of them require a unique name.
""" """
new_type = super().__new__(*args) attrs_name = attrs.get('name')
if attrs_name and not COMPONENT_LISTING.get(attrs_name):
new_fields = {}
attrs['_fields'] = new_fields
for parent in parents:
_parent_fields = getattr(parent, "_fields")
if _parent_fields:
new_fields.update(_parent_fields)
new_type = super().__new__(cls, name, parents, attrs)
if new_type.__base__ == object: if new_type.__base__ == object:
return new_type return new_type
@ -53,7 +60,7 @@ class Component(metaclass=BaseComponent):
name = "" name = ""
slot = None slot = None
_fields = {} _fields: dict | None = None
def __init__(self, host=None): def __init__(self, host=None):
assert self.name, "All Components must have a name" assert self.name, "All Components must have a name"

View file

@ -85,6 +85,27 @@ class TestComponents(EvenniaTest):
self.assertTrue(self.char1.test_a) self.assertTrue(self.char1.test_a)
self.assertTrue(self.char1.test_b) self.assertTrue(self.char1.test_b)
def test_character_components_set_fields_properly(self):
test_a_fields = self.char1.test_a._fields
self.assertIn('my_int', test_a_fields)
self.assertIn('my_list', test_a_fields)
self.assertEqual(len(test_a_fields), 2)
test_b_fields = self.char1.test_b._fields
self.assertIn('my_int', test_b_fields)
self.assertIn('my_list', test_b_fields)
self.assertIn('default_tag', test_b_fields)
self.assertIn('single_tag', test_b_fields)
self.assertIn('multiple_tags', test_b_fields)
self.assertIn('default_single_tag', test_b_fields)
self.assertEqual(len(test_b_fields), 6)
test_ic_a_fields = self.char1.ic_a._fields
self.assertIn('my_int', test_ic_a_fields)
self.assertIn('my_list', test_ic_a_fields)
self.assertIn('my_other_int', test_ic_a_fields)
self.assertEqual(len(test_ic_a_fields), 3)
def test_inherited_typeclass_does_not_include_child_class_components(self): def test_inherited_typeclass_does_not_include_child_class_components(self):
char_with_c = create.create_object( char_with_c = create.create_object(
InheritedTCWithComponents, key="char_with_c", location=self.room1, home=self.room1 InheritedTCWithComponents, key="char_with_c", location=self.room1, home=self.room1