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
Improved push typing #11409
Merged
Merged
Improved push typing #11409
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
56b921e
improve type checking for push
Bubu e9991bf
add changelog
Bubu fb1fbc1
isort
Bubu 08f12e4
lint
Bubu d404a4c
use TypedDict from typing_extensions
Bubu 073bdb8
isort again
Bubu ec27056
properly type fields pulled from event json
Bubu 6f8c5d5
Update synapse/push/httppusher.py
Bubu c8387bd
add docstrings for EmailReason
Bubu 388803d
document all the push types
Bubu 2904f49
lint
Bubu 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 @@ | ||
Improve internal types in push code. |
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Copyright 2021 The Matrix.org Foundation C.I.C. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from typing import List, Optional | ||
|
||
from typing_extensions import TypedDict | ||
|
||
|
||
class EmailReason(TypedDict, total=False): | ||
room_id: str | ||
room_name: Optional[str] | ||
now: int | ||
received_at: int | ||
delay_before_mail_ms: int | ||
last_sent_ts: int | ||
throttle_ms: int | ||
|
||
|
||
class NotifVars(TypedDict): | ||
Bubu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
link: str | ||
ts: Optional[int] | ||
messages: List | ||
|
||
|
||
class RoomVars(TypedDict): | ||
title: Optional[str] | ||
hash: int | ||
notifs: List | ||
invite: bool | ||
link: str | ||
avatar_url: Optional[str] | ||
|
||
|
||
class MessageVars(TypedDict, total=False): | ||
event_type: str | ||
is_historical: bool | ||
id: str | ||
ts: int | ||
sender_name: str | ||
sender_avatar_url: Optional[str] | ||
sender_hash: int | ||
msgtype: Optional[str] | ||
body_text_html: str | ||
body_text_plain: str | ||
image_url: str | ||
format: Optional[str] | ||
|
||
|
||
class TemplateVars(TypedDict, total=False): | ||
app_name: str | ||
server_name: str | ||
link: str | ||
user_display_name: str | ||
unsubscribe_link: str | ||
summary_text: str | ||
rooms: List | ||
reason: EmailReason |
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.
The
total=False
isn't great in my opinion, but it's currently necessary as many code path iteratively construct these dicts. Maybe reworking that could be done as a next step?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.
Can we keep them as normal dicts while we're building them, and then cast? Or just use local variables and then build it at the end? e.g.:
Not sure if that works
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.
That would require changing how these functions iteratively build the struct, while i think that's a good idea in general, i don't know how possible that is, certainly as i remember some of them building these up across multiple functions, passing intermediate results.
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.
Yup, I also believe that's the case. I'll take another look and see if I can resolve at least some easy cases here.
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 looked again and with the current code structure doing anything like this doesn't really make sense as for the reasons @ShadowJonathan mentioned.
The thing we could do is creating None value fields instead of structures with missing fields (so adding a bunch of Optionals instead of
total=False
). I currently have no opinion if this is better or worse.Anything else would require more restructuring of the code, which I think is better done seperately?
Edit: another idea would be segmenting these types into more granular ones like "EmailReason" and EmailReasonWithRoomName. I'm not convinced by this either. really.
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.
Thanks for looking into it. I think restructuring this may be the right answer eventually but you're right that it's not for here and now.