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

Properly typecheck tests.app #14984

Merged
merged 4 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/14984.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve type hints.
4 changes: 3 additions & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ exclude = (?x)
|synapse/storage/schema/

|tests/api/test_auth.py
|tests/app/test_openid_listener.py
|tests/appservice/test_scheduler.py
|tests/federation/test_federation_catch_up.py
|tests/federation/test_federation_sender.py
Expand Down Expand Up @@ -74,6 +73,9 @@ disallow_untyped_defs = False
[mypy-tests.*]
disallow_untyped_defs = False

[mypy-tests.app.*]
disallow_untyped_defs = True

[mypy-tests.config.*]
disallow_untyped_defs = True

Expand Down
2 changes: 1 addition & 1 deletion tests/app/test_homeserver_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@


class HomeserverAppStartTestCase(ConfigFileTestCase):
def test_wrong_start_caught(self):
def test_wrong_start_caught(self) -> None:
# Generate a config with a worker_app
self.generate_config()
# Add a blank line as otherwise the next addition ends up on a line with a comment
Expand Down
16 changes: 11 additions & 5 deletions tests/app/test_openid_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,32 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import List
from unittest.mock import Mock, patch

from parameterized import parameterized

from twisted.test.proto_helpers import MemoryReactor

from synapse.app.generic_worker import GenericWorkerServer
from synapse.app.homeserver import SynapseHomeServer
from synapse.config.server import parse_listener_def
from synapse.server import HomeServer
from synapse.types import JsonDict
from synapse.util import Clock

from tests.server import make_request
from tests.unittest import HomeserverTestCase


class FederationReaderOpenIDListenerTests(HomeserverTestCase):
def make_homeserver(self, reactor, clock):
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
hs = self.setup_test_homeserver(
federation_http_client=None, homeserver_to_use=GenericWorkerServer
)
return hs

def default_config(self):
def default_config(self) -> JsonDict:
conf = super().default_config()
# we're using FederationReaderServer, which uses a SlavedStore, so we
# have to tell the FederationHandler not to try to access stuff that is only
Expand All @@ -47,7 +53,7 @@ def default_config(self):
(["openid"], "auth_fail"),
]
)
def test_openid_listener(self, names, expectation):
def test_openid_listener(self, names: List[str], expectation: str) -> None:
"""
Test different openid listener configurations.

Expand Down Expand Up @@ -81,7 +87,7 @@ def test_openid_listener(self, names, expectation):

@patch("synapse.app.homeserver.KeyResource", new=Mock())
class SynapseHomeserverOpenIDListenerTests(HomeserverTestCase):
def make_homeserver(self, reactor, clock):
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
hs = self.setup_test_homeserver(
federation_http_client=None, homeserver_to_use=SynapseHomeServer
)
Expand All @@ -95,7 +101,7 @@ def make_homeserver(self, reactor, clock):
(["openid"], "auth_fail"),
]
)
def test_openid_listener(self, names, expectation):
def test_openid_listener(self, names: List[str], expectation: str) -> None:
"""
Test different openid listener configurations.

Expand Down
21 changes: 13 additions & 8 deletions tests/app/test_phone_stats_home.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import synapse
from synapse.app.phone_stats_home import start_phone_stats_home
from synapse.rest.client import login, room
from synapse.server import HomeServer
from synapse.util import Clock

from tests import unittest
from tests.server import ThreadedMemoryReactorClock
from tests.unittest import HomeserverTestCase

FIVE_MINUTES_IN_SECONDS = 300
Expand All @@ -19,7 +22,7 @@ class PhoneHomeTestCase(HomeserverTestCase):
# Override the retention time for the user_ips table because otherwise it
# gets pruned too aggressively for our R30 test.
@unittest.override_config({"user_ips_max_age": "365d"})
def test_r30_minimum_usage(self):
def test_r30_minimum_usage(self) -> None:
"""
Tests the minimum amount of interaction necessary for the R30 metric
to consider a user 'retained'.
Expand Down Expand Up @@ -68,7 +71,7 @@ def test_r30_minimum_usage(self):
r30_results = self.get_success(self.hs.get_datastores().main.count_r30_users())
self.assertEqual(r30_results, {"all": 0})

def test_r30_minimum_usage_using_default_config(self):
def test_r30_minimum_usage_using_default_config(self) -> None:
"""
Tests the minimum amount of interaction necessary for the R30 metric
to consider a user 'retained'.
Expand Down Expand Up @@ -122,7 +125,7 @@ def test_r30_minimum_usage_using_default_config(self):
r30_results = self.get_success(self.hs.get_datastores().main.count_r30_users())
self.assertEqual(r30_results, {"all": 0})

def test_r30_user_must_be_retained_for_at_least_a_month(self):
def test_r30_user_must_be_retained_for_at_least_a_month(self) -> None:
"""
Tests that a newly-registered user must be retained for a whole month
before appearing in the R30 statistic, even if they post every day
Expand Down Expand Up @@ -164,12 +167,14 @@ class PhoneHomeR30V2TestCase(HomeserverTestCase):
login.register_servlets,
]

def _advance_to(self, desired_time_secs: float):
def _advance_to(self, desired_time_secs: float) -> None:
now = self.hs.get_clock().time()
assert now < desired_time_secs
self.reactor.advance(desired_time_secs - now)

def make_homeserver(self, reactor, clock):
def make_homeserver(
self, reactor: ThreadedMemoryReactorClock, clock: Clock
) -> HomeServer:
hs = super(PhoneHomeR30V2TestCase, self).make_homeserver(reactor, clock)

# We don't want our tests to actually report statistics, so check
Expand All @@ -181,7 +186,7 @@ def make_homeserver(self, reactor, clock):
start_phone_stats_home(hs)
return hs

def test_r30v2_minimum_usage(self):
def test_r30v2_minimum_usage(self) -> None:
"""
Tests the minimum amount of interaction necessary for the R30v2 metric
to consider a user 'retained'.
Expand Down Expand Up @@ -250,7 +255,7 @@ def test_r30v2_minimum_usage(self):
r30_results, {"all": 0, "android": 0, "electron": 0, "ios": 0, "web": 0}
)

def test_r30v2_user_must_be_retained_for_at_least_a_month(self):
def test_r30v2_user_must_be_retained_for_at_least_a_month(self) -> None:
"""
Tests that a newly-registered user must be retained for a whole month
before appearing in the R30v2 statistic, even if they post every day
Expand Down Expand Up @@ -316,7 +321,7 @@ def test_r30v2_user_must_be_retained_for_at_least_a_month(self):
r30_results, {"all": 1, "android": 1, "electron": 0, "ios": 0, "web": 0}
)

def test_r30v2_returning_dormant_users_not_counted(self):
def test_r30v2_returning_dormant_users_not_counted(self) -> None:
"""
Tests that dormant users (users inactive for a long time) do not
contribute to R30v2 when they return for just a single day.
Expand Down