Skip to content

tkinter Widget do not accept .cget #4889

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

Closed
melassa opened this issue Jan 2, 2021 · 4 comments · Fixed by #4891
Closed

tkinter Widget do not accept .cget #4889

melassa opened this issue Jan 2, 2021 · 4 comments · Fixed by #4891

Comments

@melassa
Copy link
Contributor

melassa commented Jan 2, 2021

With this code:

def func(w: tk.Widget) -> Any:
    return w.cget()

Mypy output is:

error: "Widget" has no attribute "cget"

I think that Misc should have a generic definition of cget() .
Something like this::

def cget(self, key: str) -> Any: ...

I tried this change and got no problem.

This case, indeed, is problematic:


def func(w: Union[tk.Widget,tk.Toplevel,tk.Tk]) -> str:
    return w.cget('cursor')

Mypy output:

error: Cannot determine type of 'cget'

The workaround:

def func(w: Union[tk.Widget, tk.Toplevel, tk.Tk]) -> str:
    w = cast(tk.Widget, w)
    return w.cget('cursor')

.configure has the same problem, but is more hard to fix.

@Akuli
Copy link
Collaborator

Akuli commented Jan 2, 2021

Currently .cget() behaves quite weirdly, and only accepts strings that are known to be valid, so label.cget("lol") will fail but label.cget("text") works. This was my idea, and it was introduced in #4363. Now I think that's dumb and cget() should instead take str and return Any, just like you suggested.

You can already treat the widget as a dict, as in return w['cursor'], and that should work.

@melassa
Copy link
Contributor Author

melassa commented Jan 2, 2021

return w['cursor']

Yes it works and i use it a lot. I changed all my .config .configure and .cget to dict access just to use mypy...

About #4363, it is a good idea, but we need also "generic" access to cget in Misc. Every Widget can override it without problems.

@Akuli
Copy link
Collaborator

Akuli commented Jan 2, 2021

I changed all my .config .configure and .cget to dict access just to use mypy...

Seems like #4363 is not working as intended at all. I changed all of my dict access code to use .cget() and .configure() just to use mypy, and I found some bugs that way.

@melassa
Copy link
Contributor Author

melassa commented Jan 2, 2021

What a change!

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 a pull request may close this issue.

2 participants