Dealing with untyped packages/libraries #8101
-
Context
The ProblemSince all functions in myEVT_SLIDER_CHANGED = wx.NewEventType() Pyright identifies the type as so I just search the docs of the function and find that the return type is always an myEVT_SLIDER_CHANGED: int = wx.NewEventType() However, the variable’s type is still This behavior differs from mypy: myEVT_SLIDER_CHANGED: int = wx.NewEventType()
reveal_type(myEVT_SLIDER_CHANGED) # mypy: int I can't even change it by hinting the variable before the assignment: From my small search, I found no way whatsoever to convince pyright that the untyped from typing import cast
var1 = int(wx.NewEventType())
reveal_type(var1) # pyright: int
var2 = cast(int, wx.NewEventType())
reveal_type(var2) # pyright: int However, I don't like the first method because it not only changes functionality but also implies that My question is |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
The best way to deal with untyped libraries is to create type stubs for the library in question. For details, refer to this documentation. You can provide type information for the subset of the library that you use and leave the remaining unannotated. If you create a full type stub, it would be great if you could contribute it back to the maintainers of the library so they can include the stub in their distribution. This would save other users of the library the hassle of duplicating the work in creating a type stub. If you don't want to create a type stub file, you can still use the the library with pyright. However, you won't get the full benefits of type checking because pyright will need to assume that any symbols imported from the library have the type I wouldn't recommend using an explicit One other point of clarification... You mentioned that you annotated x: int
x = 3
reveal_type(x) # Literal[3]
x = 2
reveal_type(x) # Literal[2]
x = returns_int()
reveal_type(x) # int If you assign a value whose type is
This behavior allows strict-mode checks to detect when you've assigned an |
Beta Was this translation helpful? Give feedback.
-
Hey! I wanted to ask a follow-up question for writing stubs for 3rd party packages such as wxPython. The thing about wxPython is that it comes with both .py and .pyi stub files which gives basic untyped definitions for autocompletion and shows the interface used (since it is a wrapper around a C library I guess). The stubs are of course untyped which is the problem I am trying to fix. I wanted to write stubs for both mypy and better LSP support (Pylance which uses Pyright) so I want to satisfy two things: Now when I just created a stubs/wx/init.pyi to start type hinting some stuff I lost all of the current LSP support and definitions for the .pyi files shipped with the package itself. This is a problem since I want to just type hint the used part of the package but I still want to have the rest of the package seen by the LSP at least for anyone else going to use wx in the future. The question is Can I make pyright use two stubs for the same package in such a way that types are in one of them but it is incomplete and the other stub is complete but with no types? |
Beta Was this translation helpful? Give feedback.
The best way to deal with untyped libraries is to create type stubs for the library in question. For details, refer to this documentation. You can provide type information for the subset of the library that you use and leave the remaining unannotated. If you create a full type stub, it would be great if you could contribute it back to the maintainers of the library so they can include the stub in their distribution. This would save other users of the library the hassle of duplicating the work in creating a type stub.
If you don't want to create a type stub file, you can still use the the library with pyright. However, you won't get the full benefits of type checking because pyright will n…