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-up type hints for tests.push #14816
Merged
Merged
Changes from all commits
Commits
Show all changes
5 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 @@ | ||
Add missing type hints. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,11 +12,11 @@ | |
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from typing import Iterable, Optional, Tuple | ||
from typing import Iterable, List, Optional, Tuple, cast | ||
|
||
from synapse.api.constants import EventTypes, Membership | ||
from synapse.api.room_versions import RoomVersions | ||
from synapse.events import FrozenEvent | ||
from synapse.events import EventBase, FrozenEvent | ||
from synapse.push.presentable_names import calculate_room_name | ||
from synapse.types import StateKey, StateMap | ||
|
||
|
@@ -51,13 +51,15 @@ def __init__(self, events: Iterable[Tuple[StateKey, dict]]): | |
) | ||
|
||
async def get_event( | ||
self, event_id: StateKey, allow_none: bool = False | ||
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. Aside: I'd've guessed that |
||
self, event_id: str, allow_none: bool = False | ||
) -> Optional[FrozenEvent]: | ||
assert allow_none, "Mock not configured for allow_none = False" | ||
|
||
return self._events.get(event_id) | ||
# Decode the state key from the event ID. | ||
state_key = cast(Tuple[str, str], tuple(event_id.split("|", 1))) | ||
return self._events.get(state_key) | ||
|
||
async def get_events(self, event_ids: Iterable[StateKey]): | ||
async def get_events(self, event_ids: Iterable[StateKey]) -> StateMap[EventBase]: | ||
# This is cheating since it just returns all events. | ||
return self._events | ||
|
||
|
@@ -68,27 +70,27 @@ class PresentableNamesTestCase(unittest.HomeserverTestCase): | |
|
||
def _calculate_room_name( | ||
self, | ||
events: StateMap[dict], | ||
events: Iterable[Tuple[Tuple[str, str], dict]], | ||
user_id: str = "", | ||
fallback_to_members: bool = True, | ||
fallback_to_single_member: bool = True, | ||
): | ||
# This isn't 100% accurate, but works with MockDataStore. | ||
room_state_ids = {k[0]: k[0] for k in events} | ||
) -> Optional[str]: | ||
# Encode the state key into the event ID. | ||
room_state_ids = {k[0]: "|".join(k[0]) for k in events} | ||
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. To check: this is now a map from 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. Yeah. We could do more tracking and generate integer event IDs are something but that seemed like a faff. |
||
|
||
return self.get_success( | ||
calculate_room_name( | ||
MockDataStore(events), | ||
MockDataStore(events), # type: ignore[arg-type] | ||
room_state_ids, | ||
user_id or self.USER_ID, | ||
fallback_to_members, | ||
fallback_to_single_member, | ||
) | ||
) | ||
|
||
def test_name(self): | ||
def test_name(self) -> None: | ||
"""A room name event should be used.""" | ||
events = [ | ||
events: List[Tuple[Tuple[str, str], dict]] = [ | ||
((EventTypes.Name, ""), {"name": "test-name"}), | ||
] | ||
self.assertEqual("test-name", self._calculate_room_name(events)) | ||
|
@@ -100,9 +102,9 @@ def test_name(self): | |
events = [((EventTypes.Name, ""), {"name": 1})] | ||
self.assertEqual(1, self._calculate_room_name(events)) | ||
|
||
def test_canonical_alias(self): | ||
def test_canonical_alias(self) -> None: | ||
"""An canonical alias should be used.""" | ||
events = [ | ||
events: List[Tuple[Tuple[str, str], dict]] = [ | ||
((EventTypes.CanonicalAlias, ""), {"alias": "#test-name:test"}), | ||
] | ||
self.assertEqual("#test-name:test", self._calculate_room_name(events)) | ||
|
@@ -114,9 +116,9 @@ def test_canonical_alias(self): | |
events = [((EventTypes.CanonicalAlias, ""), {"alias": "test-name"})] | ||
self.assertEqual("Empty Room", self._calculate_room_name(events)) | ||
|
||
def test_invite(self): | ||
def test_invite(self) -> None: | ||
"""An invite has special behaviour.""" | ||
events = [ | ||
events: List[Tuple[Tuple[str, str], dict]] = [ | ||
((EventTypes.Member, self.USER_ID), {"membership": Membership.INVITE}), | ||
((EventTypes.Member, self.OTHER_USER_ID), {"displayname": "Other User"}), | ||
] | ||
|
@@ -140,9 +142,9 @@ def test_invite(self): | |
] | ||
self.assertEqual("Room Invite", self._calculate_room_name(events)) | ||
|
||
def test_no_members(self): | ||
def test_no_members(self) -> None: | ||
"""Behaviour of an empty room.""" | ||
events = [] | ||
events: List[Tuple[Tuple[str, str], dict]] = [] | ||
self.assertEqual("Empty Room", self._calculate_room_name(events)) | ||
|
||
# Note that events with invalid (or missing) membership are ignored. | ||
|
@@ -152,7 +154,7 @@ def test_no_members(self): | |
] | ||
self.assertEqual("Empty Room", self._calculate_room_name(events)) | ||
|
||
def test_no_other_members(self): | ||
def test_no_other_members(self) -> None: | ||
"""Behaviour of a room with no other members in it.""" | ||
events = [ | ||
( | ||
|
@@ -185,7 +187,7 @@ def test_no_other_members(self): | |
self._calculate_room_name(events, user_id=self.OTHER_USER_ID), | ||
) | ||
|
||
def test_one_other_member(self): | ||
def test_one_other_member(self) -> None: | ||
"""Behaviour of a room with a single other member.""" | ||
events = [ | ||
((EventTypes.Member, self.USER_ID), {"membership": Membership.JOIN}), | ||
|
@@ -209,7 +211,7 @@ def test_one_other_member(self): | |
] | ||
self.assertEqual("@user:test", self._calculate_room_name(events)) | ||
|
||
def test_other_members(self): | ||
def test_other_members(self) -> None: | ||
"""Behaviour of a room with multiple other members.""" | ||
# Two other members. | ||
events = [ | ||
|
Oops, something went wrong.
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.
Unfortunate that this got out of sync. How did you notice this was missing?
(Typeshed has some scripting in their CI which checks that every (public) stub corresponds to a runtime-importable name. In principle we could probably do something similar---though it'd be overkill; mypy's
disallow-untyped-calls
would probably suffice.)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 attempting to write some tests and was trying to double check the signature, Cmd+clicked in PyCharm, which brought me to the autogenerated Python code. Then I tried to find the stub and couldn't.
I think unignoring mypy from the test files might have picked it up? Not 100% sure on that though since I assume we use this in production code?
I do know adding an incorrect type here is used, so the stubs are being used somehow.
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.
This generated the following:
It seems this is a test-only method,
BulkPushRuleEvaluator
callsrun
instead.