Merge pull request #3733 from InspectorCaracal/setattr-get-fix

Fix viewing attributes with `CmdSetAttribute`
This commit is contained in:
Griatch 2026-02-14 08:04:50 +01:00 committed by GitHub
commit d8808ba370
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 34 additions and 6 deletions

View file

@ -1858,12 +1858,21 @@ class CmdSetAttribute(ObjManipCommand):
nested = False
for key, nested_keys in self.split_nested_attr(attr):
nested = True
if obj.attributes.has(key):
val = obj.attributes.get(key)
val = self.do_nested_lookup(val, *nested_keys)
if val is not self.not_found:
return f"\nAttribute {obj.name}/|w{attr}|n [category:{category}] = {val}"
error = f"\nAttribute {obj.name}/|w{attr} [category:{category}] does not exist."
if obj.attributes.has(key, category):
if nested_keys:
val = obj.attributes.get(key, category=category)
deep = self.do_nested_lookup(val, *nested_keys[:-1])
if deep is not self.not_found:
try:
val = deep[nested_keys[-1]]
except (IndexError, KeyError, TypeError):
continue
return f"\nAttribute {obj.name}/|w{attr}|n [category:{category}] = {val}"
else:
val = obj.attributes.get(key, category=category)
if val:
return f"\nAttribute {obj.name}/|w{attr}|n [category:{category}] = {val}"
error = f"\nAttribute {obj.name}/|w{attr}|n [category:{category}] does not exist."
if nested:
error += " (Nested lookups attempted)"
return error

View file

@ -1927,6 +1927,25 @@ class TestBuilding(BaseEvenniaCommandTest):
building.CmdSpawn(), "/examine NO_EXISTS", "No prototype named 'NO_EXISTS' was found."
)
def test_setattr_view_with_category(self):
"""
Test checking attributes with a category, including nested attributes.
"""
self.obj1.attributes.add("test", "value", category="cat")
self.call(
building.CmdSetAttribute(),
"Obj/test:cat",
"Attribute Obj/test [category:cat] = value",
)
# nested dict
self.obj1.attributes.add("testdict", {"key": "value"}, category="cat")
self.call(
building.CmdSetAttribute(),
"Obj/testdict['key']:cat",
"Attribute Obj/testdict['key'] [category:cat] = value",
)
import evennia.commands.default.comms as cmd_comms # noqa
from evennia.comms.comms import DefaultChannel # noqa