Skip to content

tkinter Widget do not accept .cget #4889

@melassa

Description

@melassa

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions