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

Commit e6acd3c

Browse files
authored
Upgrade mypy to version 0.931 (#12030)
Upgrade mypy to 0.931, mypy-zope to 0.3.5 and fix new complaints.
1 parent eb609c6 commit e6acd3c

File tree

9 files changed

+33
-19
lines changed

9 files changed

+33
-19
lines changed

Diff for: changelog.d/12030.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Upgrade mypy to version 0.931.

Diff for: setup.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ def exec_file(path_segments):
103103
]
104104

105105
CONDITIONAL_REQUIREMENTS["mypy"] = [
106-
"mypy==0.910",
107-
"mypy-zope==0.3.2",
106+
"mypy==0.931",
107+
"mypy-zope==0.3.5",
108108
"types-bleach>=4.1.0",
109109
"types-jsonschema>=3.2.0",
110110
"types-opentracing>=2.4.2",

Diff for: stubs/sortedcontainers/sorteddict.pyi

+9-4
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,18 @@ class SortedDict(Dict[_KT, _VT]):
6666
def __copy__(self: _SD) -> _SD: ...
6767
@classmethod
6868
@overload
69-
def fromkeys(cls, seq: Iterable[_T_h]) -> SortedDict[_T_h, None]: ...
69+
def fromkeys(
70+
cls, seq: Iterable[_T_h], value: None = ...
71+
) -> SortedDict[_T_h, None]: ...
7072
@classmethod
7173
@overload
7274
def fromkeys(cls, seq: Iterable[_T_h], value: _S) -> SortedDict[_T_h, _S]: ...
73-
def keys(self) -> SortedKeysView[_KT]: ...
74-
def items(self) -> SortedItemsView[_KT, _VT]: ...
75-
def values(self) -> SortedValuesView[_VT]: ...
75+
# As of Python 3.10, `dict_{keys,items,values}` have an extra `mapping` attribute and so
76+
# `Sorted{Keys,Items,Values}View` are no longer compatible with them.
77+
# See https://github.com/python/typeshed/issues/6837
78+
def keys(self) -> SortedKeysView[_KT]: ... # type: ignore[override]
79+
def items(self) -> SortedItemsView[_KT, _VT]: ... # type: ignore[override]
80+
def values(self) -> SortedValuesView[_VT]: ... # type: ignore[override]
7681
@overload
7782
def pop(self, key: _KT) -> _VT: ...
7883
@overload

Diff for: synapse/handlers/search.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ async def _calculate_event_contexts(
654654
self.storage, user.to_string(), res.events_after
655655
)
656656

657-
context = {
657+
context: JsonDict = {
658658
"events_before": events_before,
659659
"events_after": events_after,
660660
"start": await now_token.copy_and_replace(

Diff for: synapse/push/baserules.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ def make_base_prepend_rules(
130130
return rules
131131

132132

133-
BASE_APPEND_CONTENT_RULES = [
133+
# We have to annotate these types, otherwise mypy infers them as
134+
# `List[Dict[str, Sequence[Collection[str]]]]`.
135+
BASE_APPEND_CONTENT_RULES: List[Dict[str, Any]] = [
134136
{
135137
"rule_id": "global/content/.m.rule.contains_user_name",
136138
"conditions": [
@@ -149,7 +151,7 @@ def make_base_prepend_rules(
149151
]
150152

151153

152-
BASE_PREPEND_OVERRIDE_RULES = [
154+
BASE_PREPEND_OVERRIDE_RULES: List[Dict[str, Any]] = [
153155
{
154156
"rule_id": "global/override/.m.rule.master",
155157
"enabled": False,
@@ -159,7 +161,7 @@ def make_base_prepend_rules(
159161
]
160162

161163

162-
BASE_APPEND_OVERRIDE_RULES = [
164+
BASE_APPEND_OVERRIDE_RULES: List[Dict[str, Any]] = [
163165
{
164166
"rule_id": "global/override/.m.rule.suppress_notices",
165167
"conditions": [
@@ -278,7 +280,7 @@ def make_base_prepend_rules(
278280
]
279281

280282

281-
BASE_APPEND_UNDERRIDE_RULES = [
283+
BASE_APPEND_UNDERRIDE_RULES: List[Dict[str, Any]] = [
282284
{
283285
"rule_id": "global/underride/.m.rule.call",
284286
"conditions": [

Diff for: synapse/push/httppusher.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ async def _build_notification_dict(
325325
# This was checked in the __init__, but mypy doesn't seem to know that.
326326
assert self.data is not None
327327
if self.data.get("format") == "event_id_only":
328-
d = {
328+
d: Dict[str, Any] = {
329329
"notification": {
330330
"event_id": event.event_id,
331331
"room_id": event.room_id,

Diff for: synapse/streams/events.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,16 @@ class _EventSourcesInner:
3737
account_data: AccountDataEventSource
3838

3939
def get_sources(self) -> Iterator[Tuple[str, EventSource]]:
40-
for attribute in _EventSourcesInner.__attrs_attrs__: # type: ignore[attr-defined]
40+
for attribute in attr.fields(_EventSourcesInner):
4141
yield attribute.name, getattr(self, attribute.name)
4242

4343

4444
class EventSources:
4545
def __init__(self, hs: "HomeServer"):
4646
self.sources = _EventSourcesInner(
47-
*(attribute.type(hs) for attribute in _EventSourcesInner.__attrs_attrs__) # type: ignore[attr-defined]
47+
# mypy thinks attribute.type is `Optional`, but we know it's never `None` here since
48+
# all the attributes of `_EventSourcesInner` are annotated.
49+
*(attribute.type(hs) for attribute in attr.fields(_EventSourcesInner)) # type: ignore[misc]
4850
)
4951
self.store = hs.get_datastore()
5052

Diff for: synapse/util/daemonize.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import signal
2121
import sys
2222
from types import FrameType, TracebackType
23-
from typing import NoReturn, Type
23+
from typing import NoReturn, Optional, Type
2424

2525

2626
def daemonize_process(pid_file: str, logger: logging.Logger, chdir: str = "/") -> None:
@@ -100,7 +100,9 @@ def daemonize_process(pid_file: str, logger: logging.Logger, chdir: str = "/") -
100100
# also catch any other uncaught exceptions before we get that far.)
101101

102102
def excepthook(
103-
type_: Type[BaseException], value: BaseException, traceback: TracebackType
103+
type_: Type[BaseException],
104+
value: BaseException,
105+
traceback: Optional[TracebackType],
104106
) -> None:
105107
logger.critical("Unhanded exception", exc_info=(type_, value, traceback))
106108

@@ -123,7 +125,7 @@ def excepthook(
123125
sys.exit(1)
124126

125127
# write a log line on SIGTERM.
126-
def sigterm(signum: signal.Signals, frame: FrameType) -> NoReturn:
128+
def sigterm(signum: int, frame: Optional[FrameType]) -> NoReturn:
127129
logger.warning("Caught signal %s. Stopping daemon." % signum)
128130
sys.exit(0)
129131

Diff for: synapse/util/patch_inline_callbacks.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import functools
1616
import sys
17-
from typing import Any, Callable, Generator, List, TypeVar
17+
from typing import Any, Callable, Generator, List, TypeVar, cast
1818

1919
from twisted.internet import defer
2020
from twisted.internet.defer import Deferred
@@ -174,7 +174,9 @@ def check_yield_points_inner(
174174
)
175175
)
176176
changes.append(err)
177-
return getattr(e, "value", None)
177+
# The `StopIteration` or `_DefGen_Return` contains the return value from the
178+
# generator.
179+
return cast(T, e.value)
178180

179181
frame = gen.gi_frame
180182

0 commit comments

Comments
 (0)