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

Commit f82d38e

Browse files
authored
Improve type hints in storage classes. (#11652)
By using cast and making ignores more specific.
1 parent f58b300 commit f82d38e

10 files changed

+44
-34
lines changed

changelog.d/11652.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add missing type hints to storage classes.

synapse/storage/databases/main/deviceinbox.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# limitations under the License.
1515

1616
import logging
17-
from typing import TYPE_CHECKING, List, Optional, Tuple
17+
from typing import TYPE_CHECKING, List, Optional, Tuple, cast
1818

1919
from synapse.logging import issue9533_logger
2020
from synapse.logging.opentracing import log_kv, set_tag, trace
@@ -673,7 +673,7 @@ def _remove_dead_devices_from_device_inbox_txn(
673673
# There's a type mismatch here between how we want to type the row and
674674
# what fetchone says it returns, but we silence it because we know that
675675
# res can't be None.
676-
res: Tuple[Optional[int]] = txn.fetchone() # type: ignore[assignment]
676+
res = cast(Tuple[Optional[int]], txn.fetchone())
677677
if res[0] is None:
678678
# this can only happen if the `device_inbox` table is empty, in which
679679
# case we have no work to do.

synapse/storage/databases/main/event_federation.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ def _get_auth_chain_ids_txn(
288288
new_front = set()
289289
for chunk in batch_iter(front, 100):
290290
# Pull the auth events either from the cache or DB.
291-
to_fetch = [] # Event IDs to fetch from DB # type: List[str]
291+
to_fetch: List[str] = [] # Event IDs to fetch from DB
292292
for event_id in chunk:
293293
res = self._event_auth_cache.get(event_id)
294294
if res is None:
@@ -615,8 +615,8 @@ def _get_auth_chain_difference_txn(
615615
# currently walking, either from cache or DB.
616616
search, chunk = search[:-100], search[-100:]
617617

618-
found = [] # Results found # type: List[Tuple[str, str, int]]
619-
to_fetch = [] # Event IDs to fetch from DB # type: List[str]
618+
found: List[Tuple[str, str, int]] = [] # Results found
619+
to_fetch: List[str] = [] # Event IDs to fetch from DB
620620
for _, event_id in chunk:
621621
res = self._event_auth_cache.get(event_id)
622622
if res is None:

synapse/storage/databases/main/event_push_actions.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
import logging
16-
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union
16+
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union, cast
1717

1818
import attr
1919

@@ -326,7 +326,7 @@ def get_after_receipt(
326326
)
327327
args = [user_id, user_id, min_stream_ordering, max_stream_ordering, limit]
328328
txn.execute(sql, args)
329-
return txn.fetchall() # type: ignore[return-value]
329+
return cast(List[Tuple[str, str, int, str, bool]], txn.fetchall())
330330

331331
after_read_receipt = await self.db_pool.runInteraction(
332332
"get_unread_push_actions_for_user_in_range_http_arr", get_after_receipt
@@ -357,7 +357,7 @@ def get_no_receipt(
357357
)
358358
args = [user_id, user_id, min_stream_ordering, max_stream_ordering, limit]
359359
txn.execute(sql, args)
360-
return txn.fetchall() # type: ignore[return-value]
360+
return cast(List[Tuple[str, str, int, str, bool]], txn.fetchall())
361361

362362
no_read_receipt = await self.db_pool.runInteraction(
363363
"get_unread_push_actions_for_user_in_range_http_nrr", get_no_receipt
@@ -434,7 +434,7 @@ def get_after_receipt(
434434
)
435435
args = [user_id, user_id, min_stream_ordering, max_stream_ordering, limit]
436436
txn.execute(sql, args)
437-
return txn.fetchall() # type: ignore[return-value]
437+
return cast(List[Tuple[str, str, int, str, bool, int]], txn.fetchall())
438438

439439
after_read_receipt = await self.db_pool.runInteraction(
440440
"get_unread_push_actions_for_user_in_range_email_arr", get_after_receipt
@@ -465,7 +465,7 @@ def get_no_receipt(
465465
)
466466
args = [user_id, user_id, min_stream_ordering, max_stream_ordering, limit]
467467
txn.execute(sql, args)
468-
return txn.fetchall() # type: ignore[return-value]
468+
return cast(List[Tuple[str, str, int, str, bool, int]], txn.fetchall())
469469

470470
no_read_receipt = await self.db_pool.runInteraction(
471471
"get_unread_push_actions_for_user_in_range_email_nrr", get_no_receipt
@@ -662,7 +662,7 @@ def _find_first_stream_ordering_after_ts_txn(
662662
The stream ordering
663663
"""
664664
txn.execute("SELECT MAX(stream_ordering) FROM events")
665-
max_stream_ordering = txn.fetchone()[0] # type: ignore[index]
665+
max_stream_ordering = cast(Tuple[Optional[int]], txn.fetchone())[0]
666666

667667
if max_stream_ordering is None:
668668
return 0
@@ -731,7 +731,7 @@ def f(txn: LoggingTransaction) -> Optional[Tuple[int]]:
731731
" LIMIT 1"
732732
)
733733
txn.execute(sql, (stream_ordering,))
734-
return txn.fetchone() # type: ignore[return-value]
734+
return cast(Optional[Tuple[int]], txn.fetchone())
735735

736736
result = await self.db_pool.runInteraction(
737737
"get_time_of_last_push_action_before", f
@@ -1029,7 +1029,9 @@ def f(
10291029
" LIMIT ?" % (before_clause,)
10301030
)
10311031
txn.execute(sql, args)
1032-
return txn.fetchall() # type: ignore[return-value]
1032+
return cast(
1033+
List[Tuple[str, str, int, int, str, bool, str, int]], txn.fetchall()
1034+
)
10331035

10341036
push_actions = await self.db_pool.runInteraction("get_push_actions_for_user", f)
10351037
return [

synapse/storage/databases/main/filtering.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16-
from typing import Union
16+
from typing import Optional, Tuple, Union, cast
1717

1818
from canonicaljson import encode_canonical_json
1919

@@ -63,7 +63,7 @@ def _do_txn(txn: LoggingTransaction) -> int:
6363

6464
sql = "SELECT MAX(filter_id) FROM user_filters WHERE user_id = ?"
6565
txn.execute(sql, (user_localpart,))
66-
max_id = txn.fetchone()[0] # type: ignore[index]
66+
max_id = cast(Tuple[Optional[int]], txn.fetchone())[0]
6767
if max_id is None:
6868
filter_id = 0
6969
else:

synapse/storage/databases/main/media_repository.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
Optional,
2424
Tuple,
2525
Union,
26+
cast,
2627
)
2728

2829
from synapse.storage._base import SQLBaseStore
@@ -220,7 +221,7 @@ def get_local_media_by_user_paginate_txn(
220221
WHERE user_id = ?
221222
"""
222223
txn.execute(sql, args)
223-
count = txn.fetchone()[0] # type: ignore[index]
224+
count = cast(Tuple[int], txn.fetchone())[0]
224225

225226
sql = """
226227
SELECT

synapse/storage/databases/main/pusher.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ async def add_pusher(
494494
# invalidate, since we the user might not have had a pusher before
495495
await self.db_pool.runInteraction(
496496
"add_pusher",
497-
self._invalidate_cache_and_stream, # type: ignore
497+
self._invalidate_cache_and_stream, # type: ignore[attr-defined]
498498
self.get_if_user_has_pusher,
499499
(user_id,),
500500
)
@@ -503,7 +503,7 @@ async def delete_pusher_by_app_id_pushkey_user_id(
503503
self, app_id: str, pushkey: str, user_id: str
504504
) -> None:
505505
def delete_pusher_txn(txn, stream_id):
506-
self._invalidate_cache_and_stream( # type: ignore
506+
self._invalidate_cache_and_stream( # type: ignore[attr-defined]
507507
txn, self.get_if_user_has_pusher, (user_id,)
508508
)
509509

@@ -548,7 +548,7 @@ async def delete_all_pushers_for_user(self, user_id: str) -> None:
548548
pushers = list(await self.get_pushers_by_user_id(user_id))
549549

550550
def delete_pushers_txn(txn, stream_ids):
551-
self._invalidate_cache_and_stream( # type: ignore
551+
self._invalidate_cache_and_stream( # type: ignore[attr-defined]
552552
txn, self.get_if_user_has_pusher, (user_id,)
553553
)
554554

synapse/storage/databases/main/registration.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import logging
1717
import random
1818
import re
19-
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union
19+
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union, cast
2020

2121
import attr
2222

@@ -1357,12 +1357,15 @@ def _use_registration_token_txn(txn):
13571357
# Override type because the return type is only optional if
13581358
# allow_none is True, and we don't want mypy throwing errors
13591359
# about None not being indexable.
1360-
res: Dict[str, Any] = self.db_pool.simple_select_one_txn(
1361-
txn,
1362-
"registration_tokens",
1363-
keyvalues={"token": token},
1364-
retcols=["pending", "completed"],
1365-
) # type: ignore
1360+
res = cast(
1361+
Dict[str, Any],
1362+
self.db_pool.simple_select_one_txn(
1363+
txn,
1364+
"registration_tokens",
1365+
keyvalues={"token": token},
1366+
retcols=["pending", "completed"],
1367+
),
1368+
)
13661369

13671370
# Decrement pending and increment completed
13681371
self.db_pool.simple_update_one_txn(

synapse/storage/databases/main/relations.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515
import logging
16-
from typing import List, Optional, Tuple, Union
16+
from typing import List, Optional, Tuple, Union, cast
1717

1818
import attr
1919

@@ -399,7 +399,7 @@ def _get_thread_summary_txn(
399399
AND relation_type = ?
400400
"""
401401
txn.execute(sql, (event_id, room_id, RelationTypes.THREAD))
402-
count = txn.fetchone()[0] # type: ignore[index]
402+
count = cast(Tuple[int], txn.fetchone())[0]
403403

404404
return count, latest_event_id
405405

synapse/storage/databases/main/ui_auth.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
from typing import Any, Dict, List, Optional, Tuple, Union
14+
from typing import Any, Dict, List, Optional, Tuple, Union, cast
1515

1616
import attr
1717

@@ -225,11 +225,14 @@ def _set_ui_auth_session_data_txn(
225225
self, txn: LoggingTransaction, session_id: str, key: str, value: Any
226226
):
227227
# Get the current value.
228-
result: Dict[str, Any] = self.db_pool.simple_select_one_txn( # type: ignore
229-
txn,
230-
table="ui_auth_sessions",
231-
keyvalues={"session_id": session_id},
232-
retcols=("serverdict",),
228+
result = cast(
229+
Dict[str, Any],
230+
self.db_pool.simple_select_one_txn(
231+
txn,
232+
table="ui_auth_sessions",
233+
keyvalues={"session_id": session_id},
234+
retcols=("serverdict",),
235+
),
233236
)
234237

235238
# Update it and add it back to the database.

0 commit comments

Comments
 (0)