Fix up type hints for Twisted 21.7 (#10490)

Mostly this involves decorating a few Deferred declarations with extra type hints. We wrap the types in quotes to avoid runtime errors when running against older versions of Twisted that don't have generics on Deferred.
This commit is contained in:
Richard van der Hoff 2021-07-28 13:04:11 +01:00 committed by GitHub
parent 9643dfde6a
commit d9cb658c78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 25 additions and 15 deletions

1
changelog.d/10490.misc Normal file
View File

@ -0,0 +1 @@
Fix up type annotations to work with Twisted 21.7.

View File

@ -847,7 +847,7 @@ class _ReadBodyWithMaxSizeProtocol(protocol.Protocol):
def read_body_with_max_size( def read_body_with_max_size(
response: IResponse, stream: ByteWriteable, max_size: Optional[int] 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. Read a HTTP response body to a file-object. Optionally enforcing a maximum file size.
@ -862,7 +862,7 @@ def read_body_with_max_size(
Returns: Returns:
A Deferred which resolves to the length of the read body. 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 # If the Content-Length header gives a size larger than the maximum allowed
# size, do not bother downloading the body. # size, do not bother downloading the body.

View File

@ -285,7 +285,7 @@ class ReplicationDataHandler:
# Create a new deferred that times out after N seconds, as we don't want # Create a new deferred that times out after N seconds, as we don't want
# to wedge here forever. # to wedge here forever.
deferred = Deferred() deferred: "Deferred[None]" = Deferred()
deferred = timeout_deferred( deferred = timeout_deferred(
deferred, _WAIT_FOR_REPLICATION_TIMEOUT_SECONDS, self._reactor deferred, _WAIT_FOR_REPLICATION_TIMEOUT_SECONDS, self._reactor
) )

View File

@ -49,6 +49,8 @@ from synapse.util import Clock, unwrapFirstError
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
_T = TypeVar("_T")
class ObservableDeferred: class ObservableDeferred:
"""Wraps a deferred object so that we can add observer deferreds. These """Wraps a deferred object so that we can add observer deferreds. These
@ -121,7 +123,7 @@ class ObservableDeferred:
effect the underlying deferred. effect the underlying deferred.
""" """
if not self._result: if not self._result:
d = defer.Deferred() d: "defer.Deferred[Any]" = defer.Deferred()
def remove(r): def remove(r):
self._observers.discard(d) self._observers.discard(d)
@ -415,7 +417,7 @@ class ReadWriteLock:
self.key_to_current_writer: Dict[str, defer.Deferred] = {} self.key_to_current_writer: Dict[str, defer.Deferred] = {}
async def read(self, key: str) -> ContextManager: 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_readers = self.key_to_current_readers.setdefault(key, set())
curr_writer = self.key_to_current_writer.get(key, None) curr_writer = self.key_to_current_writer.get(key, None)
@ -438,7 +440,7 @@ class ReadWriteLock:
return _ctx_manager() return _ctx_manager()
async def write(self, key: str) -> ContextManager: 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_readers = self.key_to_current_readers.get(key, set())
curr_writer = self.key_to_current_writer.get(key, None) curr_writer = self.key_to_current_writer.get(key, None)
@ -471,10 +473,8 @@ R = TypeVar("R")
def timeout_deferred( def timeout_deferred(
deferred: defer.Deferred, deferred: "defer.Deferred[_T]", timeout: float, reactor: IReactorTime
timeout: float, ) -> "defer.Deferred[_T]":
reactor: IReactorTime,
) -> defer.Deferred:
"""The in built twisted `Deferred.addTimeout` fails to time out deferreds """The in built twisted `Deferred.addTimeout` fails to time out deferreds
that have a canceller that throws exceptions. This method creates a new that have a canceller that throws exceptions. This method creates a new
deferred that wraps and times out the given deferred, correctly handling deferred that wraps and times out the given deferred, correctly handling
@ -497,7 +497,7 @@ def timeout_deferred(
Returns: Returns:
A new Deferred, which will errback with defer.TimeoutError on timeout. 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] timed_out = [False]

View File

@ -16,7 +16,16 @@
import enum import enum
import threading 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 from prometheus_client import Gauge
@ -166,7 +175,7 @@ class DeferredCache(Generic[KT, VT]):
def set( def set(
self, self,
key: KT, key: KT,
value: defer.Deferred, value: "defer.Deferred[VT]",
callback: Optional[Callable[[], None]] = None, callback: Optional[Callable[[], None]] = None,
) -> defer.Deferred: ) -> defer.Deferred:
"""Adds a new entry to the cache (or updates an existing one). """Adds a new entry to the cache (or updates an existing one).
@ -214,7 +223,7 @@ class DeferredCache(Generic[KT, VT]):
if value.called: if value.called:
result = value.result result = value.result
if not isinstance(result, failure.Failure): if not isinstance(result, failure.Failure):
self.cache.set(key, result, callbacks) self.cache.set(key, cast(VT, result), callbacks)
return value return value
# otherwise, we'll add an entry to the _pending_deferred_cache for now, # otherwise, we'll add an entry to the _pending_deferred_cache for now,

View File

@ -413,7 +413,7 @@ class DeferredCacheListDescriptor(_CacheDescriptorBase):
# relevant result for that key. # relevant result for that key.
deferreds_map = {} deferreds_map = {}
for arg in missing: for arg in missing:
deferred = defer.Deferred() deferred: "defer.Deferred[Any]" = defer.Deferred()
deferreds_map[arg] = deferred deferreds_map[arg] = deferred
key = arg_to_cache_key(arg) key = arg_to_cache_key(arg)
cache.set(key, deferred, callback=invalidate_callback) cache.set(key, deferred, callback=invalidate_callback)