Enabling "useLibraryCodeForTypes": true will cause pyright to use/infer type information from .py files.
This can be helpful for third-party libraries that lack stubs. In general "useLibraryCodeForTypes": true is useful, and is the default for Pylance.
However, the precision and completeness of inferred types varies between third-party libraries that lack stubs. For example, pytoml uses context managers and exceptions to control flow, and so inferred signatures have the return type NoReturn. Ideally pytoml would have stubs with the correct return types. Given it doesn't, the options are to disable useLibraryCodeForTypes (and lose its benefits for other libraries) or add # type: ignore everywhere its functions are used.
There are other libraries that have similar problems. Given the lack of stubs, and the varying quality of inference across libraries, would it be possible instead to selectively ignore specific libraries? Then useLibraryCodeForTypes and its benefits could still be had for most libraries.
My recommendation is that if you are using Pylance/Pyright for type checking, useLibraryCodeForTypes should be disabled. This feature was added primarily for users who are uninterested in type checking but are interested in completion suggestions and other language convenience features. For that reason, it is off by default in Pyright but on by default in Pylance.
I'm going to leave this request open for now but mark it "blocked". It relates to some discussions we're having about support for type annotations within library "py" files, and I need to wait for those discussions to conclude before recommending a solution to the problem you've described.
We're unlikely to implement the feature that you have described here, but there are other ways we could address the problem.
@tekumara, I thought of a workaround that effectively provides the feature you asked for. You can define a local stub file for the libraries whose types you don't want to be used. Create a "typings" or "stubs" folder within your project. For each package you want to exclude from "useLibraryCodeForTypes", create a subfolder with the name of the package, add a __init__.pyi file, and add the following line to that file:
def __getattr__(name) -> Any: ...
Pyright will then use the stub instead of the library code, and it will treat all symbols in that module as "Any" for the purpose of type checking.
Please refer to #1003. I'm going to close this issue in favor of that one.
Most helpful comment
@tekumara, I thought of a workaround that effectively provides the feature you asked for. You can define a local stub file for the libraries whose types you don't want to be used. Create a "typings" or "stubs" folder within your project. For each package you want to exclude from "useLibraryCodeForTypes", create a subfolder with the name of the package, add a
__init__.pyifile, and add the following line to that file:Pyright will then use the stub instead of the library code, and it will treat all symbols in that module as "Any" for the purpose of type checking.