3333attributes.
3434"""
3535
36- import abc
3736from typing import (
3837 TYPE_CHECKING ,
3938 Sequence ,
4241from typing_extensions import TypeIs
4342
4443from synapse .events import EventBase
44+ from synapse .synapse_rust .events import Event
45+ from synapse .types import StrCollection
4546
4647if TYPE_CHECKING :
4748 from synapse .events .snapshot import EventContext , EventPersistencePair
4849
4950
50- class _DisableIsInstance (abc . ABCMeta ):
51+ class _DisableIsInstance (type ):
5152 """Metaclass which disables isinstance checks on classes which use it, by
5253 making isinstance() raise NotImplementedError.
5354
@@ -61,15 +62,38 @@ def __instancecheck__(cls, instance: object) -> bool:
6162 raise NotImplementedError ("Instance cannot be used." )
6263
6364
64- class EventProtocol (EventBase , metaclass = _DisableIsInstance ):
65- """Helper subclass that allows type narrowing for `EventBase` objects."""
65+ # We now define `EventProtocol` as a helper class for type narrowing.
66+ #
67+ # During type checking, we want the type narrowed event classes to still have
68+ # all the fields as a normal `Event`, so we make `EventProtocol` a subclass of
69+ # `Event`.
70+ #
71+ # However, at runtime we a) can't sublcass `Event` because it's a Rust class,
72+ # and b) don't want to allow `isinstance` checks against `EventProtocol` (as
73+ # it's purely a type annotation helper, not a real class). So at runtime, we
74+ # make `EventProtocol` a class with a metaclass that raises on `isinstance`
75+ # checks.
76+ if TYPE_CHECKING :
77+
78+ class EventProtocol (Event ):
79+ """Helper subclass that allows type narrowing for `EventBase` objects."""
80+
81+ else :
82+
83+ class EventProtocol (metaclass = _DisableIsInstance ):
84+ """Helper subclass that allows type narrowing for `EventBase` objects."""
85+
86+ def __new__ (cls ):
87+ raise NotImplementedError (
88+ f"{ cls .__name__ } cannot be instantiated as it is not a real class."
89+ )
6690
6791
6892class MSC4242Event (EventProtocol ):
6993 """Marker protocol for events in MSC4242 rooms. This allows us to narrow the
7094 type of events."""
7195
72- prev_state_events : list [ str ]
96+ prev_state_events : StrCollection
7397
7498
7599def supports_msc4242_state_dag (event : EventBase ) -> TypeIs [MSC4242Event ]:
0 commit comments