Skip to content

gh-140481: avoid misleading exception in tk.Tk.__getitem__#140483

Closed
TheLizzard wants to merge 1 commit into
python:mainfrom
TheLizzard:fix-issue-140481
Closed

gh-140481: avoid misleading exception in tk.Tk.__getitem__#140483
TheLizzard wants to merge 1 commit into
python:mainfrom
TheLizzard:fix-issue-140481

Conversation

@TheLizzard

@TheLizzard TheLizzard commented Oct 22, 2025

Copy link
Copy Markdown

Before

Code like:

import tkinter as tk
root = tk.Tk()
print("background" in root)

Would throw a TypeError: can only concatenate str (not "int") to str

Summary of changes

  • Fixed the error message when calling "option" in <tk.Misc>
  • Added __contain__ method to tk.Misc since it already has __getitem__ and __setitem__
  • Added tests to prevent regression

Testing

  • Ran ./python -m test test_tkinter -u=gui locally to make sure the old tests as well as the new ones pass

@picnixz picnixz changed the title gh-140481 gh-140481: avoid misleading exception in tk.Tk.__getitem__ Oct 22, 2025

@picnixz picnixz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure this is the correct way to fix this. We should instead check if option is a string in cget and if not raise a TypeError here.

cc @serhiy-storchaka

@TheLizzard

TheLizzard commented Oct 22, 2025

Copy link
Copy Markdown
Author

That might be better but then for consistency sake, we would have to add a lot of type checks to functions like Misc.config since right now they raise TclError and not TypeError (root.config(background=frozenset()).

I don't know what the best course of action is.

@serhiy-storchaka

Copy link
Copy Markdown
Member

See #140501.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants