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

Add missing type hints to synapse.app. #11287

Merged
merged 13 commits into from
Nov 10, 2021
24 changes: 13 additions & 11 deletions synapse/app/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@
List,
NoReturn,
Tuple,
cast,
)

from cryptography.utils import CryptographyDeprecationWarning

import twisted
from twisted.internet import defer, error, reactor
from twisted.internet import defer, error, reactor as _reactor
from twisted.internet.interfaces import IOpenSSLContextFactory, IReactorSSL, IReactorTCP
from twisted.internet.protocol import ServerFactory
from twisted.internet.tcp import Port
Expand All @@ -61,6 +62,7 @@
from synapse.metrics import register_threadpool
from synapse.metrics.background_process_metrics import wrap_as_background_process
from synapse.metrics.jemalloc import setup_jemalloc_stats
from synapse.types import ISynapseReactor
from synapse.util.caches.lrucache import setup_expire_lru_cache_entries
from synapse.util.daemonize import daemonize_process
from synapse.util.gai_resolver import GAIResolver
Expand All @@ -70,6 +72,11 @@
if TYPE_CHECKING:
from synapse.server import HomeServer

# Twisted injects the global reactor to make it easier to import, this confuses
# mypy which thinks it is a module. Tell it that it a more proper type.
reactor = cast(ISynapseReactor, _reactor)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunate, but hopefully worth it to get the extra checking!

(If we end up making use of this elsewhere, perhaps we can do this import-cast dance in synapse.types or somewhere similar?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had that thought briefly, but decided it wasn't worth tying things more tightly together at the moment!



logger = logging.getLogger(__name__)

# list of tuples of function, args list, kwargs dict
Expand All @@ -92,8 +99,7 @@ def register_sighup(func: Callable[..., None], *args: Any, **kwargs: Any) -> Non
def start_worker_reactor(
appname: str,
config: HomeServerConfig,
# mypy can't figure out what the default reactor is.
run_command: Callable[[], None] = reactor.run, # type: ignore[attr-defined]
run_command: Callable[[], None] = reactor.run,
) -> None:
"""Run the reactor in the main process

Expand Down Expand Up @@ -128,8 +134,7 @@ def start_reactor(
daemonize: bool,
print_pidfile: bool,
logger: logging.Logger,
# mypy can't figure out what the default reactor is.
run_command: Callable[[], None] = reactor.run, # type: ignore[attr-defined]
run_command: Callable[[], None] = reactor.run,
) -> None:
"""Run the reactor in the main process

Expand Down Expand Up @@ -242,8 +247,7 @@ async def wrapper() -> None:
# on as normal.
os._exit(1)

# mypy can't figure out what the default reactor is.
reactor.callWhenRunning(lambda: defer.ensureDeferred(wrapper())) # type: ignore[attr-defined]
reactor.callWhenRunning(lambda: defer.ensureDeferred(wrapper()))


def listen_metrics(bind_addresses: Iterable[str], port: int) -> None:
Expand Down Expand Up @@ -285,8 +289,7 @@ def listen_tcp(
bind_addresses: Collection[str],
port: int,
factory: ServerFactory,
# mypy can't figure out what the default reactor is.
reactor: IReactorTCP = reactor, # type: ignore[assignment]
reactor: IReactorTCP = reactor,
backlog: int = 50,
) -> List[Port]:
"""
Expand All @@ -312,8 +315,7 @@ def listen_ssl(
port: int,
factory: ServerFactory,
context_factory: IOpenSSLContextFactory,
# mypy can't figure out what the default reactor is.
reactor: IReactorSSL = reactor, # type: ignore[assignment]
reactor: IReactorSSL = reactor,
backlog: int = 50,
) -> List[Port]:
"""
Expand Down
4 changes: 2 additions & 2 deletions synapse/logging/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from logging import Handler, LogRecord
from logging.handlers import MemoryHandler
from threading import Thread
from typing import Optional
from typing import Optional, cast

from twisted.internet.interfaces import IReactorCore

Expand Down Expand Up @@ -56,7 +56,7 @@ def on_reactor_running():
if reactor is None:
from twisted.internet import reactor as global_reactor

reactor_to_use = global_reactor # type: ignore[assignment]
reactor_to_use = cast(IReactorCore, global_reactor)
else:
reactor_to_use = reactor

Expand Down
5 changes: 2 additions & 3 deletions synapse/util/async_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@
Set,
TypeVar,
Union,
cast,
)

import attr
from typing_extensions import ContextManager

from twisted.internet import defer
from twisted.internet.base import ReactorBase
from twisted.internet.defer import CancelledError
from twisted.internet.interfaces import IReactorTime
from twisted.python import failure
Expand Down Expand Up @@ -271,8 +271,7 @@ def __init__(
if not clock:
from twisted.internet import reactor

assert isinstance(reactor, ReactorBase)
clock = Clock(reactor)
clock = Clock(cast(IReactorTime, reactor))
self._clock = clock
self.max_count = max_count

Expand Down