Add type hints to handlers.message and events.builder (#8067)
parent
d68e10f308
commit
5dd73d029e
|
@ -0,0 +1 @@
|
||||||
|
Add type hints to `synapse.handlers.message` and `synapse.events.builder`.
|
3
mypy.ini
3
mypy.ini
|
@ -81,3 +81,6 @@ ignore_missing_imports = True
|
||||||
|
|
||||||
[mypy-rust_python_jaeger_reporter.*]
|
[mypy-rust_python_jaeger_reporter.*]
|
||||||
ignore_missing_imports = True
|
ignore_missing_imports = True
|
||||||
|
|
||||||
|
[mypy-nacl.*]
|
||||||
|
ignore_missing_imports = True
|
||||||
|
|
|
@ -17,6 +17,7 @@ from typing import Optional
|
||||||
import attr
|
import attr
|
||||||
from nacl.signing import SigningKey
|
from nacl.signing import SigningKey
|
||||||
|
|
||||||
|
from synapse.api.auth import Auth
|
||||||
from synapse.api.constants import MAX_DEPTH
|
from synapse.api.constants import MAX_DEPTH
|
||||||
from synapse.api.errors import UnsupportedRoomVersionError
|
from synapse.api.errors import UnsupportedRoomVersionError
|
||||||
from synapse.api.room_versions import (
|
from synapse.api.room_versions import (
|
||||||
|
@ -27,6 +28,8 @@ from synapse.api.room_versions import (
|
||||||
)
|
)
|
||||||
from synapse.crypto.event_signing import add_hashes_and_signatures
|
from synapse.crypto.event_signing import add_hashes_and_signatures
|
||||||
from synapse.events import EventBase, _EventInternalMetadata, make_event_from_dict
|
from synapse.events import EventBase, _EventInternalMetadata, make_event_from_dict
|
||||||
|
from synapse.state import StateHandler
|
||||||
|
from synapse.storage.databases.main import DataStore
|
||||||
from synapse.types import EventID, JsonDict
|
from synapse.types import EventID, JsonDict
|
||||||
from synapse.util import Clock
|
from synapse.util import Clock
|
||||||
from synapse.util.stringutils import random_string
|
from synapse.util.stringutils import random_string
|
||||||
|
@ -42,45 +45,46 @@ class EventBuilder(object):
|
||||||
|
|
||||||
Attributes:
|
Attributes:
|
||||||
room_version: Version of the target room
|
room_version: Version of the target room
|
||||||
room_id (str)
|
room_id
|
||||||
type (str)
|
type
|
||||||
sender (str)
|
sender
|
||||||
content (dict)
|
content
|
||||||
unsigned (dict)
|
unsigned
|
||||||
internal_metadata (_EventInternalMetadata)
|
internal_metadata
|
||||||
|
|
||||||
_state (StateHandler)
|
_state
|
||||||
_auth (synapse.api.Auth)
|
_auth
|
||||||
_store (DataStore)
|
_store
|
||||||
_clock (Clock)
|
_clock
|
||||||
_hostname (str): The hostname of the server creating the event
|
_hostname: The hostname of the server creating the event
|
||||||
_signing_key: The signing key to use to sign the event as the server
|
_signing_key: The signing key to use to sign the event as the server
|
||||||
"""
|
"""
|
||||||
|
|
||||||
_state = attr.ib()
|
_state = attr.ib(type=StateHandler)
|
||||||
_auth = attr.ib()
|
_auth = attr.ib(type=Auth)
|
||||||
_store = attr.ib()
|
_store = attr.ib(type=DataStore)
|
||||||
_clock = attr.ib()
|
_clock = attr.ib(type=Clock)
|
||||||
_hostname = attr.ib()
|
_hostname = attr.ib(type=str)
|
||||||
_signing_key = attr.ib()
|
_signing_key = attr.ib(type=SigningKey)
|
||||||
|
|
||||||
room_version = attr.ib(type=RoomVersion)
|
room_version = attr.ib(type=RoomVersion)
|
||||||
|
|
||||||
room_id = attr.ib()
|
room_id = attr.ib(type=str)
|
||||||
type = attr.ib()
|
type = attr.ib(type=str)
|
||||||
sender = attr.ib()
|
sender = attr.ib(type=str)
|
||||||
|
|
||||||
content = attr.ib(default=attr.Factory(dict))
|
content = attr.ib(default=attr.Factory(dict), type=JsonDict)
|
||||||
unsigned = attr.ib(default=attr.Factory(dict))
|
unsigned = attr.ib(default=attr.Factory(dict), type=JsonDict)
|
||||||
|
|
||||||
# These only exist on a subset of events, so they raise AttributeError if
|
# These only exist on a subset of events, so they raise AttributeError if
|
||||||
# someone tries to get them when they don't exist.
|
# someone tries to get them when they don't exist.
|
||||||
_state_key = attr.ib(default=None)
|
_state_key = attr.ib(default=None, type=Optional[str])
|
||||||
_redacts = attr.ib(default=None)
|
_redacts = attr.ib(default=None, type=Optional[str])
|
||||||
_origin_server_ts = attr.ib(default=None)
|
_origin_server_ts = attr.ib(default=None, type=Optional[int])
|
||||||
|
|
||||||
internal_metadata = attr.ib(
|
internal_metadata = attr.ib(
|
||||||
default=attr.Factory(lambda: _EventInternalMetadata({}))
|
default=attr.Factory(lambda: _EventInternalMetadata({})),
|
||||||
|
type=_EventInternalMetadata,
|
||||||
)
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
|
@ -15,7 +15,7 @@
|
||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
import logging
|
import logging
|
||||||
from typing import TYPE_CHECKING, List, Optional, Tuple
|
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple
|
||||||
|
|
||||||
from canonicaljson import encode_canonical_json, json
|
from canonicaljson import encode_canonical_json, json
|
||||||
|
|
||||||
|
@ -93,11 +93,11 @@ class MessageHandler(object):
|
||||||
|
|
||||||
async def get_room_data(
|
async def get_room_data(
|
||||||
self,
|
self,
|
||||||
user_id: str = None,
|
user_id: str,
|
||||||
room_id: str = None,
|
room_id: str,
|
||||||
event_type: Optional[str] = None,
|
event_type: str,
|
||||||
state_key: str = "",
|
state_key: str,
|
||||||
is_guest: bool = False,
|
is_guest: bool,
|
||||||
) -> dict:
|
) -> dict:
|
||||||
""" Get data from a room.
|
""" Get data from a room.
|
||||||
|
|
||||||
|
@ -407,7 +407,7 @@ class EventCreationHandler(object):
|
||||||
#
|
#
|
||||||
# map from room id to time-of-last-attempt.
|
# map from room id to time-of-last-attempt.
|
||||||
#
|
#
|
||||||
self._rooms_to_exclude_from_dummy_event_insertion = {} # type: dict[str, int]
|
self._rooms_to_exclude_from_dummy_event_insertion = {} # type: Dict[str, int]
|
||||||
|
|
||||||
# we need to construct a ConsentURIBuilder here, as it checks that the necessary
|
# we need to construct a ConsentURIBuilder here, as it checks that the necessary
|
||||||
# config options, but *only* if we have a configuration for which we are
|
# config options, but *only* if we have a configuration for which we are
|
||||||
|
@ -707,7 +707,7 @@ class EventCreationHandler(object):
|
||||||
async def create_and_send_nonmember_event(
|
async def create_and_send_nonmember_event(
|
||||||
self,
|
self,
|
||||||
requester: Requester,
|
requester: Requester,
|
||||||
event_dict: EventBase,
|
event_dict: dict,
|
||||||
ratelimit: bool = True,
|
ratelimit: bool = True,
|
||||||
txn_id: Optional[str] = None,
|
txn_id: Optional[str] = None,
|
||||||
) -> Tuple[EventBase, int]:
|
) -> Tuple[EventBase, int]:
|
||||||
|
@ -971,7 +971,7 @@ class EventCreationHandler(object):
|
||||||
# Validate a newly added alias or newly added alt_aliases.
|
# Validate a newly added alias or newly added alt_aliases.
|
||||||
|
|
||||||
original_alias = None
|
original_alias = None
|
||||||
original_alt_aliases = set()
|
original_alt_aliases = [] # type: List[str]
|
||||||
|
|
||||||
original_event_id = event.unsigned.get("replaces_state")
|
original_event_id = event.unsigned.get("replaces_state")
|
||||||
if original_event_id:
|
if original_event_id:
|
||||||
|
@ -1019,6 +1019,10 @@ class EventCreationHandler(object):
|
||||||
|
|
||||||
current_state_ids = await context.get_current_state_ids()
|
current_state_ids = await context.get_current_state_ids()
|
||||||
|
|
||||||
|
# We know this event is not an outlier, so this must be
|
||||||
|
# non-None.
|
||||||
|
assert current_state_ids is not None
|
||||||
|
|
||||||
state_to_include_ids = [
|
state_to_include_ids = [
|
||||||
e_id
|
e_id
|
||||||
for k, e_id in current_state_ids.items()
|
for k, e_id in current_state_ids.items()
|
||||||
|
|
|
@ -16,7 +16,7 @@
|
||||||
import abc
|
import abc
|
||||||
import logging
|
import logging
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
from typing import Dict, Iterable, List, Optional, Tuple, Union
|
from typing import TYPE_CHECKING, Dict, Iterable, List, Optional, Tuple, Union
|
||||||
|
|
||||||
from unpaddedbase64 import encode_base64
|
from unpaddedbase64 import encode_base64
|
||||||
|
|
||||||
|
@ -37,6 +37,10 @@ from synapse.util.distributor import user_joined_room, user_left_room
|
||||||
|
|
||||||
from ._base import BaseHandler
|
from ._base import BaseHandler
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from synapse.server import HomeServer
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@ -48,7 +52,7 @@ class RoomMemberHandler(object):
|
||||||
|
|
||||||
__metaclass__ = abc.ABCMeta
|
__metaclass__ = abc.ABCMeta
|
||||||
|
|
||||||
def __init__(self, hs):
|
def __init__(self, hs: "HomeServer"):
|
||||||
self.hs = hs
|
self.hs = hs
|
||||||
self.store = hs.get_datastore()
|
self.store = hs.get_datastore()
|
||||||
self.auth = hs.get_auth()
|
self.auth = hs.get_auth()
|
||||||
|
@ -207,7 +211,7 @@ class RoomMemberHandler(object):
|
||||||
return duplicate.event_id, stream_id
|
return duplicate.event_id, stream_id
|
||||||
|
|
||||||
stream_id = await self.event_creation_handler.handle_new_client_event(
|
stream_id = await self.event_creation_handler.handle_new_client_event(
|
||||||
requester, event, context, extra_users=[target], ratelimit=ratelimit
|
requester, event, context, extra_users=[target], ratelimit=ratelimit,
|
||||||
)
|
)
|
||||||
|
|
||||||
prev_state_ids = await context.get_prev_state_ids()
|
prev_state_ids = await context.get_prev_state_ids()
|
||||||
|
@ -1000,7 +1004,7 @@ class RoomMemberMasterHandler(RoomMemberHandler):
|
||||||
|
|
||||||
check_complexity = self.hs.config.limit_remote_rooms.enabled
|
check_complexity = self.hs.config.limit_remote_rooms.enabled
|
||||||
if check_complexity and self.hs.config.limit_remote_rooms.admins_can_join:
|
if check_complexity and self.hs.config.limit_remote_rooms.admins_can_join:
|
||||||
check_complexity = not await self.hs.auth.is_server_admin(user)
|
check_complexity = not await self.auth.is_server_admin(user)
|
||||||
|
|
||||||
if check_complexity:
|
if check_complexity:
|
||||||
# Fetch the room complexity
|
# Fetch the room complexity
|
||||||
|
|
|
@ -144,7 +144,9 @@ class RetentionTestCase(unittest.HomeserverTestCase):
|
||||||
# Get the create event to, later, check that we can still access it.
|
# Get the create event to, later, check that we can still access it.
|
||||||
message_handler = self.hs.get_message_handler()
|
message_handler = self.hs.get_message_handler()
|
||||||
create_event = self.get_success(
|
create_event = self.get_success(
|
||||||
message_handler.get_room_data(self.user_id, room_id, EventTypes.Create)
|
message_handler.get_room_data(
|
||||||
|
self.user_id, room_id, EventTypes.Create, state_key="", is_guest=False
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
# Send a first event to the room. This is the event we'll want to be purged at the
|
# Send a first event to the room. This is the event we'll want to be purged at the
|
||||||
|
|
2
tox.ini
2
tox.ini
|
@ -179,6 +179,7 @@ commands = mypy \
|
||||||
synapse/appservice \
|
synapse/appservice \
|
||||||
synapse/config \
|
synapse/config \
|
||||||
synapse/event_auth.py \
|
synapse/event_auth.py \
|
||||||
|
synapse/events/builder.py \
|
||||||
synapse/events/spamcheck.py \
|
synapse/events/spamcheck.py \
|
||||||
synapse/federation \
|
synapse/federation \
|
||||||
synapse/handlers/auth.py \
|
synapse/handlers/auth.py \
|
||||||
|
@ -186,6 +187,7 @@ commands = mypy \
|
||||||
synapse/handlers/directory.py \
|
synapse/handlers/directory.py \
|
||||||
synapse/handlers/federation.py \
|
synapse/handlers/federation.py \
|
||||||
synapse/handlers/identity.py \
|
synapse/handlers/identity.py \
|
||||||
|
synapse/handlers/message.py \
|
||||||
synapse/handlers/oidc_handler.py \
|
synapse/handlers/oidc_handler.py \
|
||||||
synapse/handlers/presence.py \
|
synapse/handlers/presence.py \
|
||||||
synapse/handlers/room_member.py \
|
synapse/handlers/room_member.py \
|
||||||
|
|
Loading…
Reference in New Issue