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

Fix up type hints for Twisted 21.7 #10490

Merged
merged 3 commits into from
Jul 28, 2021
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/10490.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix up type annotations to work with Twisted 21.7.
4 changes: 2 additions & 2 deletions synapse/http/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ def connectionLost(self, reason: Failure = connectionDone) -> None:

def read_body_with_max_size(
response: IResponse, stream: ByteWriteable, max_size: Optional[int]
) -> defer.Deferred:
) -> "defer.Deferred[int]":
"""
Read a HTTP response body to a file-object. Optionally enforcing a maximum file size.

Expand All @@ -862,7 +862,7 @@ def read_body_with_max_size(
Returns:
A Deferred which resolves to the length of the read body.
"""
d = defer.Deferred()
d: "defer.Deferred[int]" = defer.Deferred()

# If the Content-Length header gives a size larger than the maximum allowed
# size, do not bother downloading the body.
Expand Down
2 changes: 1 addition & 1 deletion synapse/replication/tcp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ async def wait_for_stream_position(

# Create a new deferred that times out after N seconds, as we don't want
# to wedge here forever.
deferred = Deferred()
deferred: "Deferred[None]" = Deferred()
deferred = timeout_deferred(
deferred, _WAIT_FOR_REPLICATION_TIMEOUT_SECONDS, self._reactor
)
Expand Down
16 changes: 8 additions & 8 deletions synapse/util/async_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@

logger = logging.getLogger(__name__)

_T = TypeVar("_T")


class ObservableDeferred:
"""Wraps a deferred object so that we can add observer deferreds. These
Expand Down Expand Up @@ -121,7 +123,7 @@ def observe(self) -> defer.Deferred:
effect the underlying deferred.
"""
if not self._result:
d = defer.Deferred()
d: "defer.Deferred[Any]" = defer.Deferred()

def remove(r):
self._observers.discard(d)
Expand Down Expand Up @@ -415,7 +417,7 @@ def __init__(self):
self.key_to_current_writer: Dict[str, defer.Deferred] = {}

async def read(self, key: str) -> ContextManager:
new_defer = defer.Deferred()
new_defer: "defer.Deferred[None]" = defer.Deferred()

curr_readers = self.key_to_current_readers.setdefault(key, set())
curr_writer = self.key_to_current_writer.get(key, None)
Expand All @@ -438,7 +440,7 @@ def _ctx_manager():
return _ctx_manager()

async def write(self, key: str) -> ContextManager:
new_defer = defer.Deferred()
new_defer: "defer.Deferred[None]" = defer.Deferred()

curr_readers = self.key_to_current_readers.get(key, set())
curr_writer = self.key_to_current_writer.get(key, None)
Expand Down Expand Up @@ -471,10 +473,8 @@ def _ctx_manager():


def timeout_deferred(
deferred: defer.Deferred,
timeout: float,
reactor: IReactorTime,
) -> defer.Deferred:
deferred: "defer.Deferred[_T]", timeout: float, reactor: IReactorTime
) -> "defer.Deferred[_T]":
"""The in built twisted `Deferred.addTimeout` fails to time out deferreds
that have a canceller that throws exceptions. This method creates a new
deferred that wraps and times out the given deferred, correctly handling
Expand All @@ -497,7 +497,7 @@ def timeout_deferred(
Returns:
A new Deferred, which will errback with defer.TimeoutError on timeout.
"""
new_d = defer.Deferred()
new_d: "defer.Deferred[_T]" = defer.Deferred()

timed_out = [False]

Expand Down
15 changes: 12 additions & 3 deletions synapse/util/caches/deferred_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@

import enum
import threading
from typing import Callable, Generic, Iterable, MutableMapping, Optional, TypeVar, Union
from typing import (
Callable,
Generic,
Iterable,
MutableMapping,
Optional,
TypeVar,
Union,
cast,
)

from prometheus_client import Gauge

Expand Down Expand Up @@ -166,7 +175,7 @@ def get_immediate(
def set(
self,
key: KT,
value: defer.Deferred,
value: "defer.Deferred[VT]",
callback: Optional[Callable[[], None]] = None,
) -> defer.Deferred:
"""Adds a new entry to the cache (or updates an existing one).
Expand Down Expand Up @@ -214,7 +223,7 @@ def set(
if value.called:
result = value.result
if not isinstance(result, failure.Failure):
self.cache.set(key, result, callbacks)
self.cache.set(key, cast(VT, result), callbacks)
return value

# otherwise, we'll add an entry to the _pending_deferred_cache for now,
Expand Down
2 changes: 1 addition & 1 deletion synapse/util/caches/descriptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ def arg_to_cache_key(arg):
# relevant result for that key.
deferreds_map = {}
for arg in missing:
deferred = defer.Deferred()
deferred: "defer.Deferred[Any]" = defer.Deferred()
deferreds_map[arg] = deferred
key = arg_to_cache_key(arg)
cache.set(key, deferred, callback=invalidate_callback)
Expand Down