Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Ignore type assignments for mocked methods
Browse files Browse the repository at this point in the history
mypy doesn't appear to have a better way to handle this.
python/mypy#2427

Convert method assignments to Mock usages were possible.
  • Loading branch information
anoadragon453 committed Jan 21, 2023
1 parent 65e1fb7 commit fd894ae
Showing 1 changed file with 33 additions and 27 deletions.
60 changes: 33 additions & 27 deletions tests/handlers/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from typing import Dict, List, Set, cast
from unittest.mock import ANY, Mock, call

from twisted.internet import defer
from twisted.test.proto_helpers import MemoryReactor
from twisted.web.resource import Resource

Expand Down Expand Up @@ -110,24 +109,24 @@ def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
self.event_source = hs.get_event_sources().sources.typing

self.datastore = hs.get_datastores().main

self.datastore.get_destination_retry_timings = Mock(
return_value=make_awaitable(None)
)

self.datastore.get_device_updates_by_remote = Mock(
self.datastore.get_device_updates_by_remote = Mock( # type: ignore[assignment]
return_value=make_awaitable((0, []))
)

self.datastore.get_destination_last_successful_stream_ordering = Mock(
self.datastore.get_destination_last_successful_stream_ordering = Mock( # type: ignore[assignment]
return_value=make_awaitable(None)
)

def get_received_txn_response(*args):
return defer.succeed(None)

self.datastore.get_received_txn_response = get_received_txn_response
self.datastore.get_received_txn_response = Mock( # type: ignore[assignment]
return_value=make_awaitable(None)
)

self.room_members = []
self.room_members: List[UserID] = []

async def check_user_in_room(room_id: str, requester: Requester) -> None:
if requester.user.to_string() not in [
Expand All @@ -136,47 +135,54 @@ async def check_user_in_room(room_id: str, requester: Requester) -> None:
raise AuthError(401, "User is not in the room")
return None

hs.get_auth().check_user_in_room = check_user_in_room
hs.get_auth().check_user_in_room = Mock( # type: ignore[assignment]
side_effect=check_user_in_room
)

async def check_host_in_room(room_id: str, server_name: str) -> bool:
return room_id == ROOM_ID

hs.get_event_auth_handler().is_host_in_room = check_host_in_room
hs.get_event_auth_handler().is_host_in_room = Mock( # type: ignore[assignment]
side_effect=check_host_in_room
)

async def get_current_hosts_in_room(room_id: str):
async def get_current_hosts_in_room(room_id: str) -> Set[str]:
return {member.domain for member in self.room_members}

hs.get_storage_controllers().state.get_current_hosts_in_room = (
get_current_hosts_in_room
hs.get_storage_controllers().state.get_current_hosts_in_room = Mock( # type: ignore[assignment]
side_effect=get_current_hosts_in_room
)

hs.get_storage_controllers().state.get_current_hosts_in_room_or_partial_state_approximation = (
get_current_hosts_in_room
hs.get_storage_controllers().state.get_current_hosts_in_room_or_partial_state_approximation = Mock( # type: ignore[assignment]
side_effect=get_current_hosts_in_room
)

async def get_users_in_room(room_id: str):
async def get_users_in_room(room_id: str) -> Set[str]:
return {str(u) for u in self.room_members}

self.datastore.get_users_in_room = get_users_in_room
self.datastore.get_users_in_room = Mock(side_effect=get_users_in_room)

self.datastore.get_user_directory_stream_pos = Mock(
self.datastore.get_user_directory_stream_pos = Mock( # type: ignore[assignment]
side_effect=(
# we deliberately return a non-None stream pos to avoid doing an initial_spam
# we deliberately return a non-None stream pos to avoid
# doing an initial_sync
lambda: make_awaitable(1)
)
)

self.datastore.get_partial_current_state_deltas = Mock(return_value=(0, None))
self.datastore.get_partial_current_state_deltas = Mock(return_value=(0, None)) # type: ignore[assignment]

self.datastore.get_to_device_stream_token = lambda: 0
self.datastore.get_new_device_msgs_for_remote = (
lambda *args, **kargs: make_awaitable(([], 0))
self.datastore.get_to_device_stream_token = Mock( # type: ignore[assignment]
side_effect=lambda: 0
)
self.datastore.get_new_device_msgs_for_remote = Mock( # type: ignore[assignment]
side_effect=lambda *args, **kargs: make_awaitable(([], 0))
)
self.datastore.delete_device_msgs_for_remote = (
lambda *args, **kargs: make_awaitable(None)
self.datastore.delete_device_msgs_for_remote = Mock( # type: ignore[assignment]
side_effect=lambda *args, **kargs: make_awaitable(None)
)
self.datastore.set_received_txn_response = (
lambda *args, **kwargs: make_awaitable(None)
self.datastore.set_received_txn_response = Mock( # type: ignore[assignment]
side_effect=lambda *args, **kwargs: make_awaitable(None)
)

def test_started_typing_local(self) -> None:
Expand Down

0 comments on commit fd894ae

Please sign in to comment.