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

Incorrect type annotation in get_stream_id_for_event_txn #12437

Closed
DMRobertson opened this issue Apr 11, 2022 · 2 comments
Closed

Incorrect type annotation in get_stream_id_for_event_txn #12437

DMRobertson opened this issue Apr 11, 2022 · 2 comments
Assignees
Labels
good first issue Good for newcomers P4 (OBSOLETE: use S- labels.) Okay backlog: will not schedule, will accept patches T-Task Refactoring, removal, replacement, enabling or disabling functionality, other engineering tasks.

Comments

@DMRobertson
Copy link
Contributor

Originally from this comment thread.

StreamWorkerStore.get_stream_id_for_event_txn is declared to return an int. If you call it with allow_none=True, it returns None, which contravenes the type hints. I think this behaviour is intentional and the type hint is incorrect, judging by how it's used. We only call get_stream_id_for_event_txn in two places, both in this function:

def _get_unread_counts_by_receipt_txn(
self,
txn: LoggingTransaction,
room_id: str,
user_id: str,
last_read_event_id: Optional[str],
) -> NotifCounts:
stream_ordering = None
if last_read_event_id is not None:
stream_ordering = self.get_stream_id_for_event_txn( # type: ignore[attr-defined]
txn,
last_read_event_id,
allow_none=True,
)
if stream_ordering is None:
# Either last_read_event_id is None, or it's an event we don't have (e.g.
# because it's been purged), in which case retrieve the stream ordering for
# the latest membership event from this user in this room (which we assume is
# a join).
event_id = self.db_pool.simple_select_one_onecol_txn(
txn=txn,
table="local_current_membership",
keyvalues={"room_id": room_id, "user_id": user_id},
retcol="event_id",
)
stream_ordering = self.get_stream_id_for_event_txn(txn, event_id) # type: ignore[attr-defined]
return self._get_unread_counts_by_pos_txn(
txn, room_id, user_id, stream_ordering
)

In particular line 194 of the quoted source looks like it's expecting stream_ordering to be possibly None.

Given the above, I think the right thing to do is

  1. change get_stream_id_for_event_txn to return Optional[int] instead of int.
  2. Try changing EventPushActionsWorkerStore to inherit from StreamWorkerStore and removing the type-ignores where we call get_stream_id_for_event_txn. Doing so might fail due with DataStore MRO problems (related: Improve type hints for data store usage #11165 and more generally our storage classes are hella confusing #11733). If so, that's fine and we can leave things as they are.
@DMRobertson DMRobertson added good first issue Good for newcomers T-Task Refactoring, removal, replacement, enabling or disabling functionality, other engineering tasks. P4 (OBSOLETE: use S- labels.) Okay backlog: will not schedule, will accept patches labels Apr 11, 2022
gautamgiri-dev added a commit to gautamgiri-dev/synapse that referenced this issue Apr 13, 2022
StreamWorkerStore.get_stream_id_for_event_txn is not declared to return Optional[int] which is consistent with it's NoneType return when the allow_none = True
gautamgiri-dev pushed a commit to gautamgiri-dev/synapse that referenced this issue Apr 13, 2022
@gautamgiri-dev
Copy link

Hello @DMRobertson I have made some changes as suggested by you followed by some changes based on linting errors thrown by mypy. Could you please review it.. and please assign this issue to me

@clokep
Copy link
Member

clokep commented Aug 5, 2022

This was fixed in #12716.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
good first issue Good for newcomers P4 (OBSOLETE: use S- labels.) Okay backlog: will not schedule, will accept patches T-Task Refactoring, removal, replacement, enabling or disabling functionality, other engineering tasks.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants