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?
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)
# 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.
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
© 2022 - 2024 — McMap. All rights reserved.
reportUnknownVariableType
error when I forgot to add the type annotation in my own code, right? – Generalissimo