Can I disable type errors from third-party packages in Pylance?
Asked Answered
D

3

15

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?

Disclimax answered 25/8, 2021 at 3:30 Comment(0)
S
2

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)
Sidewheel answered 1/3 at 15:9 Comment(0)
T
1
# 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.

Thermionics answered 30/3, 2023 at 7:5 Comment(0)
H
0

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
Heptameter answered 20/10, 2023 at 5:59 Comment(1)
But I think that will also hide the reportUnknownVariableType error when I forgot to add the type annotation in my own code, right?Generalissimo

© 2022 - 2024 — McMap. All rights reserved.