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
Fix /initialSync error due to unhashable RoomStreamToken
#10827
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 @@ | ||
Fix error in deprecated `/initialSync` endpoint when using the undocumented `from` and `to` parameters. |
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 |
---|---|---|
|
@@ -30,6 +30,7 @@ | |
) | ||
|
||
import attr | ||
from frozendict import frozendict | ||
from signedjson.key import decode_verify_key_bytes | ||
from unpaddedbase64 import decode_base64 | ||
from zope.interface import Interface | ||
|
@@ -466,12 +467,12 @@ class RoomStreamToken: | |
stream = attr.ib(type=int, validator=attr.validators.instance_of(int)) | ||
|
||
instance_map = attr.ib( | ||
type=Dict[str, int], | ||
factory=dict, | ||
type=Mapping[str, int], | ||
factory=frozendict, | ||
validator=attr.validators.deep_mapping( | ||
key_validator=attr.validators.instance_of(str), | ||
value_validator=attr.validators.instance_of(int), | ||
mapping_validator=attr.validators.instance_of(dict), | ||
mapping_validator=attr.validators.instance_of(frozendict), | ||
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. oh but you have this validator here ... well. Now I am wondering if this is better or worse. I'm interested to see what others of the team think (or is this something we already do elsewhere?) 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. I guess this is a runtime check rather than something Mypy can check, though |
||
), | ||
) | ||
|
||
|
@@ -507,7 +508,7 @@ async def parse(cls, store: "DataStore", string: str) -> "RoomStreamToken": | |
return cls( | ||
topological=None, | ||
stream=stream, | ||
instance_map=instance_map, | ||
instance_map=frozendict(instance_map), | ||
) | ||
except Exception: | ||
pass | ||
|
@@ -540,7 +541,7 @@ def copy_and_advance(self, other: "RoomStreamToken") -> "RoomStreamToken": | |
for instance in set(self.instance_map).union(other.instance_map) | ||
} | ||
|
||
return RoomStreamToken(None, max_stream, instance_map) | ||
return RoomStreamToken(None, max_stream, frozendict(instance_map)) | ||
|
||
def as_historical_tuple(self) -> Tuple[int, int]: | ||
"""Returns a tuple of `(topological, stream)` for historical tokens. | ||
|
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.
I was about to say 'huh, I never thought of just calling frozendict a Mapping!', but I'm not sure this type annotation tells the whole story.
dict
is a subclass ofMapping
, yet this field very clearly needs theMapping
to be hashable, so the type annotation is too loose here.I think this really does need
frozendict[str, int]
— which I think you have to put in double quotes (:sob: — there is a fork of frozendict which came in after generics emerged and fixes that, but we don't use it in Synapse).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.
We have a stub for this in our repo.
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've updated the type to be
"frozendict[str, int]"
with the quotes. It does feel slightly off since thefrozendict
-ness is really an implementation detail. The type required isMapping[str, int] & Hashable
except there's no way to express that in Python that I know of.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 don't think stubs are used at runtime, so you still have to put in the quotes otherwise you get the type subscripting error. (am I missing something? I hope so :))
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.
Yep, without the quotes I get a type subscripting error.