Fix a surprisingly tricky mypy error
Previous commit makes `QueryList`'s values be Optional[str]` instead of str. Before the change - `device_id` is deduced to be an `Optional[str]` as it comes from iterating over `query_list`. - when we use `device_id` to mark deleted devices as holding `None` in `result`, mypy complaints that we are using an `Optional[str]` to lookup something in a Dict whose keys are `str`. Fix this in two steps. 1. Avoid name reuse. 2. Don't store `None` in the initial version of `deleted_devices`.
This commit is contained in:
@@ -295,15 +295,19 @@ class EndToEndKeyWorkerStore(EndToEndKeyBackgroundStore, CacheInvalidationWorker
|
||||
include_deleted_devices = False
|
||||
|
||||
if include_deleted_devices:
|
||||
deleted_devices = set(query_list)
|
||||
deleted_devices = {
|
||||
(user_id, device_id)
|
||||
for user_id, device_id in query_list
|
||||
if device_id is not None
|
||||
}
|
||||
|
||||
for (user_id, device_id) in query_list:
|
||||
for (queried_user_id, queried_device_id) in query_list:
|
||||
query_clause = "user_id = ?"
|
||||
query_params.append(user_id)
|
||||
query_params.append(queried_user_id)
|
||||
|
||||
if device_id is not None:
|
||||
if queried_device_id is not None:
|
||||
query_clause += " AND device_id = ?"
|
||||
query_params.append(device_id)
|
||||
query_params.append(queried_device_id)
|
||||
|
||||
query_clauses.append(query_clause)
|
||||
|
||||
@@ -322,10 +326,16 @@ class EndToEndKeyWorkerStore(EndToEndKeyBackgroundStore, CacheInvalidationWorker
|
||||
txn.execute(sql, query_params)
|
||||
|
||||
result: Dict[str, Dict[str, Optional[DeviceKeyLookupResult]]] = {}
|
||||
for (user_id, device_id, display_name, key_json) in txn:
|
||||
fetched_user_id: str
|
||||
fetched_device_id: str
|
||||
display_name: Optional[str]
|
||||
key_json: Optional[str]
|
||||
for (fetched_user_id, fetched_device_id, display_name, key_json) in txn:
|
||||
if include_deleted_devices:
|
||||
deleted_devices.remove((user_id, device_id))
|
||||
result.setdefault(user_id, {})[device_id] = DeviceKeyLookupResult(
|
||||
deleted_devices.remove((fetched_user_id, fetched_device_id))
|
||||
result.setdefault(fetched_user_id, {})[
|
||||
fetched_device_id
|
||||
] = DeviceKeyLookupResult(
|
||||
display_name, db_to_json(key_json) if key_json else None
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user