Skip to content

widget: Define TypedDicts for todo/poll widget outputs and JSON payloads#1614

Open
NexionisJake wants to merge 1 commit intozulip:mainfrom
NexionisJake:widget-typeddict-fix
Open

widget: Define TypedDicts for todo/poll widget outputs and JSON payloads#1614
NexionisJake wants to merge 1 commit intozulip:mainfrom
NexionisJake:widget-typeddict-fix

Conversation

@NexionisJake
Copy link
Copy Markdown

What does this PR do, and why?

This PR improves type safety for widget handling by defining TypedDicts for todo/poll widget outputs and JSON payloads.

Changes include:

  • Adding TypedDicts in api_types.py for all widget event JSON structures:

    • TodoInitialTask, TodoWidgetExtraData, TodoWidgetInit
    • TodoNewTaskEvent, TodoStrikeEvent, TodoNewTitleEvent
    • PollWidgetExtraData, PollWidgetInit, PollQuestionEvent
    • PollVoteEvent, PollNewOptionEvent
    • Union types: TodoWidgetEvent, PollWidgetEvent, WidgetEvent
  • Updating widget.py to use typed structures with casts for JSON parsing, providing better type checking and IDE support

  • Adding factory fixtures for Submessage content in tests:

    • submessage_factory() for constructing Submessage dicts
    • Helper functions for todo and poll widget JSON content
  • Submessage is now properly defined in api_types.py and used to improve the Message TypedDict definition

External discussion & connections

How did you test this?

  • Adapting existing automated tests
  • Existing automated tests should already cover this (only a refactor of tested code)

Self-review checklist for each commit

  • It is a minimal coherent idea
  • It has a commit summary following the documented style (title & body)
  • It has a commit summary describing the motivation and reasoning for the change
  • It individually passes linting and tests
  • It contains test additions for any new behavior
  • It flows clearly from a previous branch commit, and/or prepares for the next commit

@zulipbot zulipbot added size: XL [Automatic label added by zulipbot] area: refactoring area: widgets GSoC Possible GSoC project component TRACKING labels Feb 7, 2026
@zulipbot
Copy link
Copy Markdown
Member

zulipbot commented Feb 7, 2026

Hello @zulip/server-refactoring members, this pull request was labeled with the "area: refactoring" label, so you may want to check it out!

@NexionisJake NexionisJake force-pushed the widget-typeddict-fix branch 4 times, most recently from faac8f3 to 52209d9 Compare February 7, 2026 19:39
This commit improves type safety for widget handling by:

- Adding TypedDicts in api_types.py for all widget event JSON structures:
  - TodoInitialTask, TodoWidgetExtraData, TodoWidgetInit
  - TodoNewTaskEvent, TodoStrikeEvent, TodoNewTitleEvent
  - PollWidgetExtraData, PollWidgetInit, PollQuestionEvent
  - PollVoteEvent, PollNewOptionEvent
  - Union types: TodoWidgetEvent, PollWidgetEvent, WidgetEvent

- Updating widget.py to use typed structures with casts for JSON parsing,
  providing better type checking and IDE support

- Adding factory fixtures for Submessage content in tests:
  - submessage_factory() for constructing Submessage dicts
  - Helper functions for todo and poll widget JSON content

- Submessage is now properly defined in api_types.py and used to
  improve the Message TypedDict definition

- Fixed model.py to use correct Submessage field names (id, msg_type)

- Updated test_model.py expected submessage formats to match new schema

- Fixed model.py mypy error by casting msg_type to Literal["widget"]

Fixes: zulip#1576
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: refactoring area: widgets GSoC Possible GSoC project component size: XL [Automatic label added by zulipbot] TRACKING

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve typing & test approach for widgets

2 participants