Reputation: 959
Some of the packages I use don't type hint their code, so when I use them, Pylance keeps telling me that the functions I use have partially unknown types, which is a problem I can't fix. Is there a way to disable such errors?
Upvotes: 20
Views: 1711
Reputation: 66
Pylance uses Pyright for type checking, so you can configure Pyright to ignore type errors from specific third-party packages. To do this, create a pyrightconfig.json
file with the following content (for example, to exclude matplotlib
):
{
"exclude": [
"**/site-packages/matplotlib/**"
]
}
This will prevent Pyright from analyzing matplotlib
, effectively suppressing type-related warnings from that package. You can add other packages to the exclude
list as needed.
Upvotes: 1
Reputation: 634
There are three approaches for achieving such purpose:
You could either ignore a specific error (I.e.: In the import statement), create a stub file to provide the correct typing, or create your own file and import from it, which would use cast from typings.
Mypy has documentation for creating a stub file.
If you want to ignore a specific error, like the Unknown error, you can work it like that (this was tested with pylance):
# type: ignore[reportUnknownVariableType]
You can add this comment either to the end of the line (to ignore one line only), or the top of the file, to ignore all occurrences.
If you'd like, mypy has an automatic stub generator, it can be used as following:
pip3 install mypy
stubgen -p aiohttp_socks
This will generate all the stubs for the package aiohttp_socks, which has multiple files, but you could use it with -m to create stubs for the module too, the difference being: Module will generate only one file, package will generate every file recursively, you generally would want to use -p.
By default vscode picks the stubs in the typings folder, in the root project folder, also vscode has a stub generator that can be used too.
If you want to use cast, it can be done like that:
untyped_connector = ProxyConnector.from_url # type: ignore[reportUnknownVariableType]
ProxyConnectorFromUrl = cast(Callable[[str, Optional[object]], BaseConnector], untyped_connector)
It's also possible to generate stubs using pyright, as vscode does it. But I found it to be slightly more inconvenient and still present this same error in the generated stub files.
To be able to generate stubs in VsCode: add a folder .vscode, and a settings.json file:
.vscode/settings.json
{
"python.analysis.typeCheckingMode": "strict"
}
Install PyLance: ms-python.vscode-pylance
Hover over the offending library:
For more reference on stub generation in vscode: https://micropython-stubs.readthedocs.io/en/main/22_vscode.html
Upvotes: 2
Reputation: 871
If you're absolutely certain of the type you're getting from the external library and you're sure it's not documented through typeshed either, you can always cast
it to signal to the type checker it's to be treated as that type.
from typing import cast
from elsewhere import Ham
spam = some_untyped_return()
ham = cast(Ham, spam)
Upvotes: 2
Reputation: 1178
You can modify the severity level for pyright's diagnostics rules in your settings.json
like this:
"python.analysis.diagnosticSeverityOverrides": {
"reportUnknownVariableType": "none"
},
You can also configure it for your project using the pyproject.toml
tool table:
[tool.pyright]
reportUnknownVariableType = false
Upvotes: 0
Reputation: 1042
# type: ignore
Place this comment at the top of a file to ignore all type-checking errors in the file.
Similarly, place it at the end of a line to just ignore errors on that line.
When I come across a third-party package that doesn't provide typing information, I generally make a wrapper for it, in a file on its own, and use that wrapper from the rest of my codebase - this allows me to just use the per-file ignore, while minimising the amount of code that has to have type-checking disabled.
Upvotes: 2