From 85ef24f9179282ff6e8e3a1882d119739fe70c50 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Fri, 23 Jan 2026 16:49:12 +0000 Subject: [PATCH] Remove test runtime checks for AsyncMock vs. Mock Turns out they're always AsyncMock. --- .../synapse/tests/__init__.py | 18 ++--------- .../tests/test_guest_registration_servlet.py | 30 +++++++++---------- 2 files changed, 16 insertions(+), 32 deletions(-) diff --git a/modules/restricted-guests/synapse/tests/__init__.py b/modules/restricted-guests/synapse/tests/__init__.py index f62fc2cbf4..4f990b54f2 100644 --- a/modules/restricted-guests/synapse/tests/__init__.py +++ b/modules/restricted-guests/synapse/tests/__init__.py @@ -9,8 +9,8 @@ import sqlite3 from asyncio import Future -from typing import Any, Awaitable, Callable, Dict, List, Tuple, TypeVar -from unittest.mock import AsyncMock, Mock +from typing import Any, Awaitable, Callable, Dict, Tuple, TypeVar +from unittest.mock import Mock from synapse.http.client import SimpleHttpClient from synapse.module_api import ModuleApi @@ -81,20 +81,6 @@ def make_awaitable(result: TV) -> Awaitable[TV]: return future -def set_async_return_value(target: Any, value: Any) -> None: - if isinstance(target, AsyncMock): - target.return_value = value - else: - target.return_value = make_awaitable(value) - - -def set_async_side_effect(target: Any, values: List[Any]) -> None: - if isinstance(target, AsyncMock): - target.side_effect = values - else: - target.side_effect = [make_awaitable(value) for value in values] - - def get_qualified_user_id(username: str) -> str: return f"@{username}:matrix.local" diff --git a/modules/restricted-guests/synapse/tests/test_guest_registration_servlet.py b/modules/restricted-guests/synapse/tests/test_guest_registration_servlet.py index ca72d17964..f6008cbded 100644 --- a/modules/restricted-guests/synapse/tests/test_guest_registration_servlet.py +++ b/modules/restricted-guests/synapse/tests/test_guest_registration_servlet.py @@ -92,22 +92,20 @@ class GuestUserReaperTest(aiounittest.AsyncTestCase): module.registration_servlet._mas_admin_client._generate_device_id = ( # type: ignore[method-assign,union-attr] lambda: "MASDEVICE123" ) - set_async_return_value( - module_api.http_client.post_urlencoded_get_json, - {"access_token": "mas_admin_token"}, - ) - set_async_side_effect( - module_api.http_client.post_json_get_json, - [ - {"data": {"id": "mas-user-id"}}, - { - "data": { - "id": "MASDEVICE123", - "attributes": {"access_token": "mas_access_token"}, - } - }, - ], - ) + + # `make_awaitable` is not needed here as both methods are already `AsyncMock`. + module_api.http_client.post_urlencoded_get_json.return_value = { + "access_token": "mas_admin_token" + } + module_api.http_client.post_json_get_json.side_effect = [ + {"data": {"id": "mas-user-id"}}, + { + "data": { + "id": "MASDEVICE123", + "attributes": {"access_token": "mas_access_token"}, + } + }, + ] status, response = await module.registration_servlet._async_render_POST(request)