Skip to content

Fix tkinter.OptionMenu callback type - #16113

Merged
srittau merged 2 commits into
python:mainfrom
subotac:fix/tkinter-optionmenu-callback
Jul 31, 2026
Merged

Fix tkinter.OptionMenu callback type#16113
srittau merged 2 commits into
python:mainfrom
subotac:fix/tkinter-optionmenu-callback

Conversation

@subotac

@subotac subotac commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

OptionMenu passes the selected menu value to _setit, which invokes the callback with that value rather than the StringVar. Update both version-specific signatures to type the callback argument
as str, and add a regression test for callback inference.

CPython implementation: https://github.com/python/cpython/blob/main/Lib/tkinter/__init__.py#L4573-L4615

Fixes #16111

@github-actions

This comment has been minimized.

@srittau srittau left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks, just a note about tests.

root.after(1000, foo, 10, "lol")
root.after(1000, foo, 10, 10) # type: ignore

tkinter.OptionMenu(root, tkinter.StringVar(), "option", command=lambda value: assert_type(value, str))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Since our tests are never run, they are only checked against type checkers, this test doesn't do much. We try to keep our tests minimal, so this isn't needed.

@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@srittau
srittau merged commit 1b11667 into python:main Jul 31, 2026
73 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tkinter.OptionMenu(command=...) type hint wrong

2 participants