Skip to content

Fix ttk.OptionMenu command callback parameter type - #16658

Open
chiliec wants to merge 1 commit into
python:mainfrom
chiliec:fix-ttk-optionmenu-command-type
Open

chiliec wants to merge 1 commit into
python:mainfrom
chiliec:fix-ttk-optionmenu-command-type

Conversation

@chiliec

@chiliec chiliec commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

Fixes #16657

ttk.OptionMenu currently types command as Callable[[tkinter.StringVar], object] | None, but at runtime the callback is invoked with the selected string value, not the StringVar. In CPython's tkinter/ttk.py, set_menu does:

command=(None if self._callback is None else lambda val=val: self._callback(val))

where val comes from *values (strings). So a valid Callable[[str], object] handler is wrongly rejected by the stub. tkinter.OptionMenu already annotates command as Callable[[str], object] | None; this change makes ttk.OptionMenu consistent (both the 3.14+ and the older signature).

Tests

Added an OptionMenu case to stdlib/@tests/test_cases/check_tkinter.py for both tkinter.OptionMenu and tkinter.ttk.OptionMenu.

Validation (mypy 2.4.0, --custom-typeshed-dir .):

  • Before the stub fix, the test case errored:
    error: Argument "command" to "OptionMenu" has incompatible type "Callable[[str], None]"; expected "Callable[[StringVar], object] | None" [arg-type]
  • After the fix, check_tkinter.py passes clean under both --python-version 3.14 and --python-version 3.12.

@github-actions

This comment has been minimized.

@srittau

srittau commented Oct 30, 2026

Copy link
Copy Markdown
Collaborator

Please remove the tests. In typeshed, we only add regression tests for functions and classes which are known to have caused complex problems in the past, or where stubs are difficult to get right. 100% test coverage for typeshed is neither necessary nor desirable, as it would lead to code duplication.

See tests/REGRESSION.md for more information.

ttk.OptionMenu invokes its command callback with the selected string
value (set_menu passes val from *values), not a StringVar. Match the
annotation to tkinter.OptionMenu, which already uses str.
@chiliec
chiliec force-pushed the fix-ttk-optionmenu-command-type branch from e71d7ec to 4ace347 Compare October 31, 2026 11:04
@chiliec

chiliec commented Oct 31, 2026

Copy link
Copy Markdown
Contributor Author

Done — removed the test case, the PR is now just the stub change in ttk.pyi (4ace347). Thanks for the pointer to REGRESSION.md.

@github-actions

Copy link
Copy Markdown
Contributor

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

This branch has not been deployed

No deployments
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 types say requires StringVar but gets str put in at runtime.

2 participants