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

Commit 11df4ec

Browse files
authored
Add more type hints to the main state store. (#12267)
1 parent 5e88143 commit 11df4ec

File tree

4 files changed

+16
-10
lines changed

4 files changed

+16
-10
lines changed

changelog.d/12267.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add missing type hints for storage.

mypy.ini

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ exclude = (?x)
4343
|synapse/storage/databases/main/event_federation.py
4444
|synapse/storage/databases/main/push_rule.py
4545
|synapse/storage/databases/main/roommember.py
46-
|synapse/storage/databases/main/state.py
4746
|synapse/storage/schema/
4847

4948
|tests/api/test_auth.py

synapse/storage/databases/main/state.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
15-
import collections.abc
1615
import logging
17-
from typing import TYPE_CHECKING, Collection, Iterable, Optional, Set, Tuple
16+
from typing import TYPE_CHECKING, Dict, Iterable, Optional, Set, Tuple
17+
18+
from frozendict import frozendict
1819

1920
from synapse.api.constants import EventTypes, Membership
2021
from synapse.api.errors import NotFoundError, UnsupportedRoomVersionError
@@ -29,7 +30,7 @@
2930
from synapse.storage.databases.main.events_worker import EventsWorkerStore
3031
from synapse.storage.databases.main.roommember import RoomMemberWorkerStore
3132
from synapse.storage.state import StateFilter
32-
from synapse.types import JsonDict, StateMap
33+
from synapse.types import JsonDict, JsonMapping, StateMap
3334
from synapse.util.caches import intern_string
3435
from synapse.util.caches.descriptors import cached, cachedList
3536

@@ -132,7 +133,7 @@ def get_room_version_id_txn(self, txn: LoggingTransaction, room_id: str) -> str:
132133

133134
return room_version
134135

135-
async def get_room_predecessor(self, room_id: str) -> Optional[dict]:
136+
async def get_room_predecessor(self, room_id: str) -> Optional[JsonMapping]:
136137
"""Get the predecessor of an upgraded room if it exists.
137138
Otherwise return None.
138139
@@ -158,9 +159,10 @@ async def get_room_predecessor(self, room_id: str) -> Optional[dict]:
158159
predecessor = create_event.content.get("predecessor", None)
159160

160161
# Ensure the key is a dictionary
161-
if not isinstance(predecessor, collections.abc.Mapping):
162+
if not isinstance(predecessor, (dict, frozendict)):
162163
return None
163164

165+
# The keys must be strings since the data is JSON.
164166
return predecessor
165167

166168
async def get_create_event_for_room(self, room_id: str) -> EventBase:
@@ -306,7 +308,9 @@ async def _get_state_group_for_event(self, event_id: str) -> Optional[int]:
306308
list_name="event_ids",
307309
num_args=1,
308310
)
309-
async def _get_state_group_for_events(self, event_ids: Collection[str]) -> JsonDict:
311+
async def _get_state_group_for_events(
312+
self, event_ids: Iterable[str]
313+
) -> Dict[str, int]:
310314
"""Returns mapping event_id -> state_group"""
311315
rows = await self.db_pool.simple_select_many_batch(
312316
table="event_to_state_groups",
@@ -521,7 +525,7 @@ def _background_remove_left_rooms_txn(
521525
)
522526

523527
for user_id in potentially_left_users - joined_users:
524-
await self.mark_remote_user_device_list_as_unsubscribed(user_id)
528+
await self.mark_remote_user_device_list_as_unsubscribed(user_id) # type: ignore[attr-defined]
525529

526530
return batch_size
527531

synapse/util/caches/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import typing
1818
from enum import Enum, auto
1919
from sys import intern
20-
from typing import Any, Callable, Dict, List, Optional, Sized
20+
from typing import Any, Callable, Dict, List, Optional, Sized, TypeVar
2121

2222
import attr
2323
from prometheus_client.core import Gauge
@@ -195,8 +195,10 @@ def register_cache(
195195
)
196196
}
197197

198+
T = TypeVar("T", Optional[str], str)
198199

199-
def intern_string(string: Optional[str]) -> Optional[str]:
200+
201+
def intern_string(string: T) -> T:
200202
"""Takes a (potentially) unicode string and interns it if it's ascii"""
201203
if string is None:
202204
return None

0 commit comments

Comments
 (0)