Ignoring a specific undefined variable with pyright

Question:

When writing custom SaltStack modules/states using VScode and linting with pyright, I get the following error all over the place:

"__salt__" is not defined

It’s not a killer, because I can put the following on the end of every line that references it:

# pyright: ignore[reportUndefinedVariable]

But what I would rather do is tell my project that __salt__ is a known variable, and don’t report on that variable.

Is this possible?

Asked By: Xaraxia

||

Answers:

I eventually worked out the correct solution which works with python newer than 3.5 and all the variables I’m tripping over.

from typing import Any, TYPE_CHECKING

if TYPE_CHECKING:
    __salt__: Any = None
    __states__: Any = None
    __opts__: Any = None
Answered By: Xaraxia

You can do so by declaring the variable in the __builtins__.pyi file. See this.

Answered By: Diego Palacios