disallows clients to add class attributes (through the state manager)

Note that adding dictionary keys still works. You can also append to lists.
This commit is contained in:
Mose Müller 2024-07-30 09:18:22 +02:00
parent 75e355faf9
commit 5d8471fd47

View File

@ -281,6 +281,15 @@ class StateManager:
processed_key = parse_serialized_key(path_parts[-1]) processed_key = parse_serialized_key(path_parts[-1])
target_obj[processed_key] = value # type: ignore target_obj[processed_key] = value # type: ignore
else: else:
# Don't allow adding attributes to objects through state manager
if self.__attr_exists_on_target_obj(
target_obj=target_obj, name=path_parts[-1]
):
raise AttributeError(
f"{target_obj.__class__.__name__!r} object has no attribute "
f"{path_parts[-1]!r}"
)
setattr(target_obj, path_parts[-1], value) setattr(target_obj, path_parts[-1], value)
def __is_loadable_state_attribute(self, full_access_path: str) -> bool: def __is_loadable_state_attribute(self, full_access_path: str) -> bool:
@ -322,3 +331,8 @@ class StateManager:
path_parts[-1], path_parts[-1],
) )
return False return False
def __attr_exists_on_target_obj(self, target_obj: Any, name: str) -> bool:
return not is_property_attribute(target_obj, name) and not hasattr(
target_obj, name
)