Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api: Add dataclass for update_message events. #18411

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

MSurfer20
Copy link
Member

Different variants of update_message events made it difficult
to keep api changes uniform across them. Created a dataclass for
update_message variant events and refactored to replace Dict with
TypedDict. Fixes #16022.

Testing plan:

GIFs or screenshots:

@MSurfer20 MSurfer20 force-pushed the fix_message_embeds branch from 69f4f26 to f5c479e Compare May 8, 2021 17:17
@@ -5485,6 +5485,38 @@ class DeleteMessagesEvent(TypedDict, total=False):
stream_id: int


class UpdateMessagesEvent(TypedDict, total=False):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why total=False here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is something I used as a temporary solution and need to fix(hence the current WIP status). Removing it causes error since message_ids, which is a compulary field is calculated much later in the code and the event TypedDict is created much before.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@timabbott Something that I now remembered -- the total=False is necessary because we need to omit the Optional parameters completely rather than including them with None, since the API expects those keys to be absent rather than present with value None, and gives error in such cases.
A downside of doing this is that we won't be able to check for missing parameters in the event.(I followed the example of DeleteMessagesEvent to use total=False)

new_stream_id: Optional[int]
orig_subject: Optional[str]
subject: Optional[str]
topic_links: Optional[List[Dict[str, str]]]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is kinda giant; I wonder if there's a good way to define simpler classes; e.g. have the common elements in a base class, and then subclasses for "has topic edit"? Not sure; in any case, it's an improvement to just have a defined type even if it is giant.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, ideally we'd use the class in zerver/tornado_event_queue.py if possible.

Copy link
Member Author

@MSurfer20 MSurfer20 May 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is kinda giant; I wonder if there's a good way to define simpler classes; e.g. have the common elements in a base class, and then subclasses for "has topic edit"? Not sure; in any case, it's an improvement to just have a defined type even if it is giant.

I initially tried to do it this way -- but there are total 4 branching ifs - if there is content, if it is a stream message, if the topic is changed or stream is moved, if the topic is changed - giving rise to 16 possible classes. That might make the class structure, as well as the code to use the appropriate class for the TypedDict a little cluttered in my opinion.
Also, in TypedDict, it is not possible to change type of an inherited parameter in the child class, so an approach where we have Optional elements made non-optional in child classes won't work out as well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@timabbott Would this large number of different classes be desirable? Since this is the only way I think we could remove the total=False if that is desired.

@MSurfer20 MSurfer20 force-pushed the fix_message_embeds branch 3 times, most recently from 6230034 to 5ad6d66 Compare May 13, 2021 17:55
Different variants of update_message events made it difficult
to keep api changes uniform across them. Created a dataclass for
update_message variant events and refactored to replace Dict with
TypedDict. Fixes zulip#16022.
@MSurfer20 MSurfer20 force-pushed the fix_message_embeds branch from 5ad6d66 to d7cddad Compare May 13, 2021 18:03
@MSurfer20 MSurfer20 changed the title [WIP]api: Add dataclass for update_message events. api: Add dataclass for update_message events. May 13, 2021
@zulipbot
Copy link
Member

Heads up @MSurfer20, we just merged some commits that conflict with the changes your made in this pull request! You can review this repository's recent commits to see where the conflicts occur. Please rebase your feature branch against the upstream/main branch and resolve your pull request's merge conflicts accordingly.

@timabbott timabbott force-pushed the main branch 2 times, most recently from 4ec3636 to 88b200c Compare August 18, 2023 23:52
@timabbott timabbott added the completion candidate PRs with reviews that may unblock merging label Jan 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
completion candidate PRs with reviews that may unblock merging has conflicts size: M
Projects
None yet
Development

Successfully merging this pull request may close these issues.

message embeds: Fix nonstandard update_message event for message embeds
3 participants