Add type hints to tkinter standard library module

I am making something that uses the tkinter module and have found that the Widget class has no type hints defined in its methods.
This case is true across files in cpython/Lib/tkinter at main · python/cpython
Sure, I could add # pyright: ignore[reportUnknownMemberType] to the line on VSCode to disable the warning, but it would be great if the module had type hints defined.

Warning:

``Type of “state” is partially unknown
Type of “state” is "(statespec: Unknown | None = None) → Unknown"PylancereportUnknownMemberType

(method) def state(statespec: Unknown | None = None) → Unknown```

2 Likes

Strange. Is Tkinter not in typeshed?

1 Like

Oh, it is!

But then Widget.state has no types defined

typeshed/stdlib/tkinter/ttk.pyi at main · python/typeshed

def state(self, statespec=None): ...

I just learnt about typeshed from your reply, so I had assumed that type definitions were only placed in a module file.

Yes, standard library types are in typeshed. Most types are fairly complete, but tkinter has a lot of functionality and relatively few users, so it’s not always complete. Contributions are welcome!

3 Likes

Oh I see.

Do I go ahead and create an issue on this?

a PR could also work :wink:

1 Like

Generally in typeshed we don’t accept issues that only point out that some items are unannotated. We are aware of that. But we gladly accept PRs that add (or correct) annotations.

2 Likes

I’ll mention as a user who’s used Tk a few times, it was pretty confusing/surprising that anything in the standard library was missing type stubs. For a while, I thought there was something wrong about my venv or Mypy setup because I assumed that the std lib itself would always have its type stubs complete.

1 Like

Alright, I opened a PR Improve type annotations for `ttk.Widget` by DanielOnGitHub17
Thank you.

2 Likes