Skip to content

Commit

Permalink
Overload "allow_none" on DB pool static method (#17616)
Browse files Browse the repository at this point in the history
### Pull Request Checklist

<!-- Please read
https://element-hq.github.io/synapse/latest/development/contributing_guide.html
before submitting your pull request -->

* [x] Pull request is based on the develop branch
* [x] Pull request includes a [changelog
file](https://element-hq.github.io/synapse/latest/development/contributing_guide.html#changelog).
The entry should:
- Be a short description of your change which makes sense to users.
"Fixed a bug that prevented receiving messages from other servers."
instead of "Moved X method from `EventStore` to `EventWorkerStore`.".
  - Use markdown where necessary, mostly for `code blocks`.
  - End with either a period (.) or an exclamation mark (!).
  - Start with a capital letter.
- Feel free to credit yourself, by adding a sentence "Contributed by
@github_username." or "Contributed by [Your Name]." to the end of the
entry.
* [x] [Code
style](https://element-hq.github.io/synapse/latest/code_style.html) is
correct
(run the
[linters](https://element-hq.github.io/synapse/latest/development/contributing_guide.html#run-the-linters))

---------

Co-authored-by: Quentin Gliech <quenting@element.io>
  • Loading branch information
AndrewFerr and sandhose authored Feb 10, 2025
1 parent 8f07ef5 commit e407474
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 33 deletions.
1 change: 1 addition & 0 deletions changelog.d/17616.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Overload DatabasePool.simple_select_one_txn to return non-None when the allow_none parameter is False.
24 changes: 20 additions & 4 deletions synapse/storage/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -2159,10 +2159,26 @@ def simple_update_one_txn(
if rowcount > 1:
raise StoreError(500, "More than one row matched (%s)" % (table,))

# Ideally we could use the overload decorator here to specify that the
# return type is only optional if allow_none is True, but this does not work
# when you call a static method from an instance.
# See https://github.com/python/mypy/issues/7781
@overload
@staticmethod
def simple_select_one_txn(
txn: LoggingTransaction,
table: str,
keyvalues: Dict[str, Any],
retcols: Collection[str],
allow_none: Literal[False] = False,
) -> Tuple[Any, ...]: ...

@overload
@staticmethod
def simple_select_one_txn(
txn: LoggingTransaction,
table: str,
keyvalues: Dict[str, Any],
retcols: Collection[str],
allow_none: Literal[True] = True,
) -> Optional[Tuple[Any, ...]]: ...

@staticmethod
def simple_select_one_txn(
txn: LoggingTransaction,
Expand Down
23 changes: 10 additions & 13 deletions synapse/storage/databases/main/e2e_room_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,19 +510,16 @@ def _get_e2e_room_keys_version_info_txn(txn: LoggingTransaction) -> JsonDict:
# it isn't there.
raise StoreError(404, "No backup with that version exists")

row = cast(
Tuple[int, str, str, Optional[int]],
self.db_pool.simple_select_one_txn(
txn,
table="e2e_room_keys_versions",
keyvalues={
"user_id": user_id,
"version": this_version,
"deleted": 0,
},
retcols=("version", "algorithm", "auth_data", "etag"),
allow_none=False,
),
row = self.db_pool.simple_select_one_txn(
txn,
table="e2e_room_keys_versions",
keyvalues={
"user_id": user_id,
"version": this_version,
"deleted": 0,
},
retcols=("version", "algorithm", "auth_data", "etag"),
allow_none=False,
)
return {
"auth_data": db_to_json(row[2]),
Expand Down
15 changes: 7 additions & 8 deletions synapse/storage/databases/main/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -1510,15 +1510,14 @@ def _use_registration_token_txn(txn: LoggingTransaction) -> None:
# Override type because the return type is only optional if
# allow_none is True, and we don't want mypy throwing errors
# about None not being indexable.
pending, completed = cast(
Tuple[int, int],
self.db_pool.simple_select_one_txn(
txn,
"registration_tokens",
keyvalues={"token": token},
retcols=["pending", "completed"],
),
row = self.db_pool.simple_select_one_txn(
txn,
"registration_tokens",
keyvalues={"token": token},
retcols=("pending", "completed"),
)
pending = int(row[0])
completed = int(row[1])

# Decrement pending and increment completed
self.db_pool.simple_update_one_txn(
Expand Down
15 changes: 7 additions & 8 deletions synapse/storage/databases/main/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -1837,15 +1837,14 @@ def _get_events_around_txn(
dict
"""

stream_ordering, topological_ordering = cast(
Tuple[int, int],
self.db_pool.simple_select_one_txn(
txn,
"events",
keyvalues={"event_id": event_id, "room_id": room_id},
retcols=["stream_ordering", "topological_ordering"],
),
row = self.db_pool.simple_select_one_txn(
txn,
"events",
keyvalues={"event_id": event_id, "room_id": room_id},
retcols=("stream_ordering", "topological_ordering"),
)
stream_ordering = int(row[0])
topological_ordering = int(row[1])

# Paginating backwards includes the event at the token, but paginating
# forward doesn't.
Expand Down

0 comments on commit e407474

Please sign in to comment.