Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Ignore specific libraries when useLibraryCodeForTypes = true #945

Closed
tekumara opened this issue Aug 16, 2020 · 5 comments
Closed

Ignore specific libraries when useLibraryCodeForTypes = true #945

tekumara opened this issue Aug 16, 2020 · 5 comments
Labels
blocked Waiting on external fix enhancement request New feature or request

Comments

@tekumara
Copy link

tekumara commented Aug 16, 2020

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.

@tekumara tekumara added the enhancement request New feature or request label Aug 16, 2020
@erictraut
Copy link
Collaborator

erictraut commented Aug 16, 2020

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.

@erictraut erictraut added the blocked Waiting on external fix label Aug 16, 2020
@erictraut
Copy link
Collaborator

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.

@erictraut
Copy link
Collaborator

@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.

@erictraut
Copy link
Collaborator

Please refer to #1003. I'm going to close this issue in favor of that one.

@turtlebasket
Copy link

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.

This breaks coloring on imports from the sqlalchemy root module in VSCode.

Before (no custom typings/sqlalchemy/__init__.py):

Screenshot 2023-11-08 at 6 23 52 PM

After (custom typings/sqlalchemy/__init__.py):

Screenshot 2023-11-08 at 6 20 25 PM

This fix does get rid of other (most?) error detection issues with pyright, but it is a pretty annoying tradeoff to have to make. Might just go with sqlalchemy-stubs and make the jump to mypy for now :\

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
blocked Waiting on external fix enhancement request New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants