From c8ce96f504a671bd97d0aeb9225bbf967bd900d6 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Wed, 22 Apr 2026 11:43:59 +0100 Subject: [PATCH] Reinstate removed EventBase methods (#19712) Both `__getitem__` and `.user_id` were removed in #19680 to simplify the event class. However, `EventBase` is exposed to modules who might also make use of those methods, so let's reinstate them (but otherwise not reinstate the usage of them in the code). --- changelog.d/19712.misc | 1 + synapse/events/__init__.py | 9 +++++++++ tests/module_api/test_api.py | 18 ++++++++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 changelog.d/19712.misc diff --git a/changelog.d/19712.misc b/changelog.d/19712.misc new file mode 100644 index 0000000000..c8fa79bf47 --- /dev/null +++ b/changelog.d/19712.misc @@ -0,0 +1 @@ +Small simplifications to the events class. diff --git a/synapse/events/__init__.py b/synapse/events/__init__.py index f4a5624d1a..3c46d02e92 100644 --- a/synapse/events/__init__.py +++ b/synapse/events/__init__.py @@ -34,6 +34,7 @@ from typing import ( ) import attr +from typing_extensions import deprecated from unpaddedbase64 import encode_base64 from synapse.api.constants import ( @@ -219,6 +220,9 @@ class EventBase(metaclass=abc.ABCMeta): state_key: DictProperty[str] = DictProperty("state_key") type: DictProperty[str] = DictProperty("type") + # This is a deprecated property, use `sender` instead. Only used by modules. + user_id: DictProperty[str] = DictProperty("sender") + @property def event_id(self) -> str: raise NotImplementedError() @@ -360,6 +364,11 @@ class EventBase(metaclass=abc.ABCMeta): ">" ) + # Using `__getitem__` is deprecated. Only used by modules. + @deprecated("Use attribute access instead") + def __getitem__(self, field: str) -> Any | None: + return self._dict[field] + class FrozenEvent(EventBase): format_version = EventFormatVersions.ROOM_V1_V2 # All events of this type are V1 diff --git a/tests/module_api/test_api.py b/tests/module_api/test_api.py index 12c8942bc8..f1b20a12ec 100644 --- a/tests/module_api/test_api.py +++ b/tests/module_api/test_api.py @@ -828,6 +828,24 @@ class ModuleApiTestCase(BaseModuleApiTestCase): # Ensure the pushers were deleted after the callback. self.assertEqual(len(self.hs.get_pusherpool().pushers[user_id].values()), 0) + def test_event_deprecated_methods(self) -> None: + """Test that deprecated methods on events are still functional.""" + user_id = self.register_user("user", "password") + tok = self.login("user", "password") + + room_id = self.helper.create_room_as(tok=tok) + + state = self.get_success( + self.hs.get_storage_controllers().state.get_current_state(room_id) + ) + create_event = state[(EventTypes.Create, "")] + + # `.user_id` is a deprecated alias for `.sender`. + self.assertEqual(create_event.user_id, user_id) + + # The event supports looking up keys via `__getitem__` although deprecated + self.assertEqual(create_event["room_id"], room_id) + class ModuleApiWorkerTestCase(BaseModuleApiTestCase, BaseMultiWorkerStreamTestCase): """For testing ModuleApi functionality in a multi-worker setup"""