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

Use mypy 1.0 #15052

Merged
merged 19 commits into from Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/15052.misc
@@ -0,0 +1 @@
Improve type hints.
63 changes: 33 additions & 30 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion synapse/__init__.py
Expand Up @@ -64,7 +64,9 @@
try:
from canonicaljson import set_json_library

set_json_library(json)
# type-ignore: I think there has been a regression in mypy 1.0.0 in how it checks
# modules against Protocols.
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved
set_json_library(json) # type: ignore[arg-type]
except ImportError:
pass

Expand Down
1 change: 1 addition & 0 deletions synapse/handlers/ui_auth/checkers.py
Expand Up @@ -39,6 +39,7 @@ def is_enabled(self) -> bool:
Returns:
True if this login type is enabled.
"""
raise NotImplementedError()
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved

async def check_auth(self, authdict: dict, clientip: str) -> Any:
"""Given the authentication dict from the client, attempt to check this step
Expand Down
2 changes: 1 addition & 1 deletion synapse/http/matrixfederationclient.py
Expand Up @@ -1267,7 +1267,7 @@ async def get_file(
def _flatten_response_never_received(e: BaseException) -> str:
if hasattr(e, "reasons"):
reasons = ", ".join(
_flatten_response_never_received(f.value) for f in e.reasons # type: ignore[attr-defined]
_flatten_response_never_received(f.value) for f in e.reasons
)

return "%s:[%s]" % (type(e).__name__, reasons)
Expand Down
14 changes: 9 additions & 5 deletions synapse/logging/opentracing.py
Expand Up @@ -188,7 +188,7 @@ def set_fates(clotho, lachesis, atropos, father="Zues", mother="Themis"):
)

import attr
from typing_extensions import ParamSpec
from typing_extensions import Concatenate, ParamSpec

from twisted.internet import defer
from twisted.web.http import Request
Expand Down Expand Up @@ -445,7 +445,7 @@ def init_tracer(hs: "HomeServer") -> None:
opentracing = None # type: ignore[assignment]
return

if not opentracing or not JaegerConfig:
if opentracing is None or JaegerConfig is None:
raise ConfigError(
"The server has been configured to use opentracing but opentracing is not "
"installed."
Expand Down Expand Up @@ -864,7 +864,7 @@ def extract_text_map(carrier: Dict[str, str]) -> Optional["opentracing.SpanConte

def _custom_sync_async_decorator(
func: Callable[P, R],
wrapping_logic: Callable[[Callable[P, R], Any, Any], ContextManager[None]],
wrapping_logic: Callable[Concatenate[Callable[P, R], P], ContextManager[None]],
) -> Callable[P, R]:
"""
Decorates a function that is sync or async (coroutines), or that returns a Twisted
Expand Down Expand Up @@ -964,7 +964,10 @@ def _decorator(func: Callable[P, R]) -> Callable[P, R]:
if not opentracing:
return func

return _custom_sync_async_decorator(func, _wrapping_logic)
# type-ignore: mypy seems to be confused by the ParamSpecs here.
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved
return _custom_sync_async_decorator(
func, _wrapping_logic # type: ignore[arg-type]
)

return _decorator

Expand Down Expand Up @@ -1010,7 +1013,8 @@ def _wrapping_logic(
set_tag(SynapseTags.FUNC_KWARGS, str(kwargs))
yield

return _custom_sync_async_decorator(func, _wrapping_logic)
# type-ignore: mypy seems to be confused by the ParamSpecs here.
return _custom_sync_async_decorator(func, _wrapping_logic) # type: ignore[arg-type]


@contextlib.contextmanager
Expand Down
1 change: 1 addition & 0 deletions synapse/rest/media/v1/_base.py
Expand Up @@ -300,6 +300,7 @@ def write_to_consumer(self, consumer: IConsumer) -> Awaitable:
Returns:
Resolves once the response has finished being written
"""
raise NotImplementedError()
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved

def __enter__(self) -> None:
pass
Expand Down
4 changes: 2 additions & 2 deletions synapse/storage/engines/__init__.py
Expand Up @@ -25,7 +25,7 @@
except ImportError:

class PostgresEngine(BaseDatabaseEngine): # type: ignore[no-redef]
def __new__(cls, *args: object, **kwargs: object) -> NoReturn: # type: ignore[misc]
def __new__(cls, *args: object, **kwargs: object) -> NoReturn:
raise RuntimeError(
f"Cannot create {cls.__name__} -- psycopg2 module is not installed"
)
Expand All @@ -36,7 +36,7 @@ def __new__(cls, *args: object, **kwargs: object) -> NoReturn: # type: ignore[m
except ImportError:

class Sqlite3Engine(BaseDatabaseEngine): # type: ignore[no-redef]
def __new__(cls, *args: object, **kwargs: object) -> NoReturn: # type: ignore[misc]
def __new__(cls, *args: object, **kwargs: object) -> NoReturn:
raise RuntimeError(
f"Cannot create {cls.__name__} -- sqlite3 module is not installed"
)
Expand Down
6 changes: 4 additions & 2 deletions synapse/storage/engines/postgres.py
Expand Up @@ -22,7 +22,7 @@
IncorrectDatabaseSetup,
IsolationLevel,
)
from synapse.storage.types import Cursor
from synapse.storage.types import Cursor, DBAPI2Module

if TYPE_CHECKING:
from synapse.storage.database import LoggingDatabaseConnection
Expand All @@ -35,7 +35,9 @@ class PostgresEngine(
BaseDatabaseEngine[psycopg2.extensions.connection, psycopg2.extensions.cursor]
):
def __init__(self, database_config: Mapping[str, Any]):
super().__init__(psycopg2, database_config)
# Cast: mypy 1.0.0 doesn't seem to think that the module implements the protocol.
# AFAICS this is a false positive.
super().__init__(cast(DBAPI2Module, psycopg2), database_config)
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)

# Disables passing `bytes` to txn.execute, c.f. #6186. If you do
Expand Down
8 changes: 5 additions & 3 deletions synapse/storage/engines/sqlite.py
Expand Up @@ -15,18 +15,20 @@
import sqlite3
import struct
import threading
from typing import TYPE_CHECKING, Any, List, Mapping, Optional
from typing import TYPE_CHECKING, Any, List, Mapping, Optional, cast

from synapse.storage.engines import BaseDatabaseEngine
from synapse.storage.types import Cursor
from synapse.storage.types import Cursor, DBAPI2Module

if TYPE_CHECKING:
from synapse.storage.database import LoggingDatabaseConnection


class Sqlite3Engine(BaseDatabaseEngine[sqlite3.Connection, sqlite3.Cursor]):
def __init__(self, database_config: Mapping[str, Any]):
super().__init__(sqlite3, database_config)
# Cast: mypy 1.0.0 doesn't seem to think that the module implements the protocol.
# AFAICS this is a false positive.
super().__init__(cast(DBAPI2Module, sqlite3), database_config)

database = database_config.get("args", {}).get("database")
self._is_in_memory = database in (
Expand Down
2 changes: 1 addition & 1 deletion synapse/streams/__init__.py
Expand Up @@ -32,4 +32,4 @@ async def get_new_events(
is_guest: bool,
explicit_room_id: Optional[str] = None,
) -> Tuple[List[R], K]:
...
raise NotImplementedError()
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 2 additions & 2 deletions tests/handlers/test_register.py
Expand Up @@ -62,7 +62,7 @@ async def check_registration_for_spam(
request_info: Collection[Tuple[str, str]],
auth_provider_id: Optional[str],
) -> RegistrationBehaviour:
pass
return RegistrationBehaviour.ALLOW


class DenyAll(TestSpamChecker):
Expand Down Expand Up @@ -111,7 +111,7 @@ async def check_registration_for_spam(
username: Optional[str],
request_info: Collection[Tuple[str, str]],
) -> RegistrationBehaviour:
pass
return RegistrationBehaviour.ALLOW


class LegacyAllowAll(TestLegacyRegistrationSpamChecker):
Expand Down
11 changes: 6 additions & 5 deletions tests/http/federation/test_matrix_federation_agent.py
Expand Up @@ -63,7 +63,7 @@
get_test_ca_cert_file,
)
from tests.server import FakeTransport, ThreadedMemoryReactorClock
from tests.utils import default_config
from tests.utils import checked_cast, default_config

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -146,8 +146,10 @@ def _make_connection(
#
# Normally this would be done by the TCP socket code in Twisted, but we are
# stubbing that out here.
client_protocol = client_factory.buildProtocol(dummy_address)
assert isinstance(client_protocol, _WrappingProtocol)
# NB: we use a checked_cast here to workaround https://github.com/Shoobx/mypy-zope/issues/91)
client_protocol = checked_cast(
_WrappingProtocol, client_factory.buildProtocol(dummy_address)
)
client_protocol.makeConnection(
FakeTransport(server_protocol, self.reactor, client_protocol)
)
Expand Down Expand Up @@ -446,7 +448,6 @@ def _do_get_via_proxy(
server_ssl_protocol = _wrap_server_factory_for_tls(
_get_test_protocol_factory()
).buildProtocol(dummy_address)
assert isinstance(server_ssl_protocol, TLSMemoryBIOProtocol)

# Tell the HTTP server to send outgoing traffic back via the proxy's transport.
proxy_server_transport = proxy_server.transport
Expand Down Expand Up @@ -1529,7 +1530,7 @@ def _check_logcontext(context: LoggingContextOrSentinel) -> None:

def _wrap_server_factory_for_tls(
factory: IProtocolFactory, sanlist: Optional[List[bytes]] = None
) -> IProtocolFactory:
) -> TLSMemoryBIOFactory:
"""Wrap an existing Protocol Factory with a test TLSMemoryBIOFactory
The resultant factory will create a TLS server which presents a certificate
signed by our test CA, valid for the domains in `sanlist`
Expand Down