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

Add final type hint to synapse.server. #15035

Merged
merged 3 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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/15035.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve type hints.
3 changes: 0 additions & 3 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ warn_unused_ignores = False
[mypy-synapse.util.caches.treecache]
disallow_untyped_defs = False

[mypy-synapse.server]
disallow_untyped_defs = False

[mypy-synapse.storage.database]
disallow_untyped_defs = False

Expand Down
2 changes: 1 addition & 1 deletion synapse/handlers/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -1076,7 +1076,7 @@ async def _send_events_for_new_room(
state_map: MutableStateMap[str] = {}
# current_state_group of last event created. Used for computing event context of
# events to be batched
current_state_group = None
current_state_group: Optional[int] = None

def create_event_dict(etype: str, content: JsonDict, **kwargs: Any) -> JsonDict:
e = {"type": etype, "content": content}
Expand Down
10 changes: 5 additions & 5 deletions synapse/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import abc
import functools
import logging
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, TypeVar, cast
from typing import TYPE_CHECKING, Callable, Dict, List, Optional, TypeVar, cast

from twisted.internet.interfaces import IOpenSSLContextFactory
from twisted.internet.tcp import Port
Expand Down Expand Up @@ -144,10 +144,10 @@
from synapse.handlers.saml import SamlHandler


T = TypeVar("T", bound=Callable[..., Any])
T = TypeVar("T")


def cache_in_self(builder: T) -> T:
def cache_in_self(builder: Callable[..., T]) -> Callable[..., T]:
clokep marked this conversation as resolved.
Show resolved Hide resolved
"""Wraps a function called e.g. `get_foo`, checking if `self.foo` exists and
returning if so. If not, calls the given function and sets `self.foo` to it.

Expand All @@ -166,7 +166,7 @@ def cache_in_self(builder: T) -> T:
building = [False]

@functools.wraps(builder)
def _get(self):
def _get(self: "HomeServer") -> T:
try:
return getattr(self, depname)
except AttributeError:
Expand All @@ -187,7 +187,7 @@ def _get(self):

# We cast here as we need to tell mypy that `_get` has the same signature as
# `builder`.
clokep marked this conversation as resolved.
Show resolved Hide resolved
return cast(T, _get)
return _get


class HomeServer(metaclass=abc.ABCMeta):
Expand Down
2 changes: 2 additions & 0 deletions synapse/storage/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class SQLBaseStore(metaclass=ABCMeta):
per data store (and not one per physical database).
"""

db_pool: DatabasePool

def __init__(
self,
database: DatabasePool,
Expand Down
1 change: 1 addition & 0 deletions synapse/storage/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ class DatabasePool:
"""

_TXN_ID = 0
engine: BaseDatabaseEngine

def __init__(
self,
Expand Down
2 changes: 1 addition & 1 deletion synapse/storage/databases/main/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ async def _get_prevs_before_rejected(self, event_ids: Iterable[str]) -> Set[str]

# The set of event_ids to return. This includes all soft-failed events
# and their prev events.
existing_prevs = set()
existing_prevs: Set[str] = set()

def _get_prevs_before_rejected_txn(
txn: LoggingTransaction, batch: Collection[str]
Expand Down