This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add some type hints to datastore #12255
Merged
+61
−42
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6b049d9
Add some type hints to datastore.
dklimpel c31da0c
newsfile
dklimpel cf870f3
change `set` to `Set[str]` in `state.py`
dklimpel a428260
revert changes for `psycopg2`
dklimpel 1e10625
Remove not needed `# type: ignore`, because #12269
dklimpel 8adabd6
Merge branch 'develop' into type_hints
dklimpel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add missing type hints for storage. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,10 +24,9 @@ | |
Optional, | ||
Set, | ||
Tuple, | ||
cast, | ||
) | ||
|
||
from twisted.internet import defer | ||
|
||
from synapse.api.constants import ReceiptTypes | ||
from synapse.replication.slave.storage._slaved_id_tracker import SlavedIdTracker | ||
from synapse.replication.tcp.streams import ReceiptsStream | ||
|
@@ -38,7 +37,11 @@ | |
LoggingTransaction, | ||
) | ||
from synapse.storage.engines import PostgresEngine | ||
from synapse.storage.util.id_generators import MultiWriterIdGenerator, StreamIdGenerator | ||
from synapse.storage.util.id_generators import ( | ||
AbstractStreamIdTracker, | ||
MultiWriterIdGenerator, | ||
StreamIdGenerator, | ||
) | ||
from synapse.types import JsonDict | ||
from synapse.util import json_encoder | ||
from synapse.util.caches.descriptors import cached, cachedList | ||
|
@@ -58,6 +61,7 @@ def __init__( | |
hs: "HomeServer", | ||
): | ||
self._instance_name = hs.get_instance_name() | ||
self._receipts_id_gen: AbstractStreamIdTracker | ||
|
||
if isinstance(database.engine, PostgresEngine): | ||
self._can_write_to_receipts = ( | ||
|
@@ -161,7 +165,7 @@ def f(txn: LoggingTransaction) -> List[Tuple[str, str, int, int]]: | |
" AND user_id = ?" | ||
) | ||
txn.execute(sql, (user_id,)) | ||
return txn.fetchall() | ||
return cast(List[Tuple[str, str, int, int]], txn.fetchall()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at the schema, (It could just be a sloppy schema?) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
rows = await self.db_pool.runInteraction( | ||
"get_receipts_for_user_with_orderings", f | ||
|
@@ -257,7 +261,7 @@ def f(txn: LoggingTransaction) -> List[Dict[str, Any]]: | |
if not rows: | ||
return [] | ||
|
||
content = {} | ||
content: JsonDict = {} | ||
for row in rows: | ||
content.setdefault(row["event_id"], {}).setdefault(row["receipt_type"], {})[ | ||
row["user_id"] | ||
|
@@ -305,7 +309,7 @@ def f(txn: LoggingTransaction) -> List[Dict[str, Any]]: | |
"_get_linearized_receipts_for_rooms", f | ||
) | ||
|
||
results = {} | ||
results: JsonDict = {} | ||
for row in txn_results: | ||
# We want a single event per room, since we want to batch the | ||
# receipts by room, event and type. | ||
|
@@ -370,7 +374,7 @@ def f(txn: LoggingTransaction) -> List[Dict[str, Any]]: | |
"get_linearized_receipts_for_all_rooms", f | ||
) | ||
|
||
results = {} | ||
results: JsonDict = {} | ||
for row in txn_results: | ||
# We want a single event per room, since we want to batch the | ||
# receipts by room, event and type. | ||
|
@@ -399,7 +403,7 @@ async def get_users_sent_receipts_between( | |
""" | ||
|
||
if last_id == current_id: | ||
return defer.succeed([]) | ||
return [] | ||
|
||
def _get_users_sent_receipts_between_txn(txn: LoggingTransaction) -> List[str]: | ||
sql = """ | ||
|
@@ -453,7 +457,10 @@ def get_all_updated_receipts_txn( | |
""" | ||
txn.execute(sql, (last_id, current_id, limit)) | ||
|
||
updates = [(r[0], r[1:5] + (db_to_json(r[5]),)) for r in txn] | ||
updates = cast( | ||
List[Tuple[int, list]], | ||
DMRobertson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[(r[0], r[1:5] + (db_to_json(r[5]),)) for r in txn], | ||
) | ||
|
||
limited = False | ||
upper_bound = current_id | ||
|
@@ -496,7 +503,13 @@ def invalidate_caches_for_receipt( | |
self._invalidate_get_users_with_receipts_in_room(room_id, receipt_type, user_id) | ||
self.get_receipts_for_room.invalidate((room_id, receipt_type)) | ||
|
||
def process_replication_rows(self, stream_name, instance_name, token, rows): | ||
def process_replication_rows( | ||
self, | ||
stream_name: str, | ||
instance_name: str, | ||
token: int, | ||
rows: Iterable[Any], | ||
) -> None: | ||
if stream_name == ReceiptsStream.NAME: | ||
self._receipts_id_gen.advance(instance_name, token) | ||
for row in rows: | ||
|
@@ -584,7 +597,7 @@ def insert_linearized_receipt_txn( | |
) | ||
|
||
if receipt_type == ReceiptTypes.READ and stream_ordering is not None: | ||
self._remove_old_push_actions_before_txn( | ||
self._remove_old_push_actions_before_txn( # type: ignore[attr-defined] | ||
txn, room_id=room_id, user_id=user_id, stream_ordering=stream_ordering | ||
) | ||
|
||
|
@@ -637,7 +650,7 @@ def graph_to_linear(txn: LoggingTransaction) -> str: | |
"insert_receipt_conv", graph_to_linear | ||
) | ||
|
||
async with self._receipts_id_gen.get_next() as stream_id: | ||
async with self._receipts_id_gen.get_next() as stream_id: # type: ignore[attr-defined] | ||
DMRobertson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
event_ts = await self.db_pool.runInteraction( | ||
"insert_linearized_receipt", | ||
self.insert_linearized_receipt_txn, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Somewhat surprised this is necessary. What does mypy think the type of
hs.hostname
is? (E.g. try addingreveal_type(hs.hostname)
and runningmypy
.) Similarly forself.config: HomeServerConfig
below.I'm not actually sure what the best practice is here---restate the annotation or let the type propagate? Maybe @matrix-org/synapse-core has opinions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was a bit surprised by this too -- I'd double check that
server_name
is properly typed onHomeServer
first! 👍There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(And that we're importing the proper
HomeServer
object?)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you do not set this here, you will get:
synapse/storage/databases/main/__init__.py:84: error: Cannot determine type of "server_name" in base class "MediaRepositoryStore" [misc]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a better or other solution? Or
# type: ignore[misc]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's what I think is going on here:
Homeserver.hostname
is astr
self.server_name = hs.hostname
it looks at the superclass to see ifself.server_name
is already exists, and if so checks thatstr
is compatible with whatever type thesuper().server_name
isMediaRepositoryBackgroundUpdateStore
andSQLBaseStore
, neither of which has aserver_name
attribute. Mypy doesn't like this.self.server_name: str
we are explicitly telling mypy that we want to create a new attribute on the child class?All in all, I think what you've done (
self.server_name: str
) is the right choice.