忽略带有Pyright的特定未定义变量

发布于 2025-02-12 01:28:16 字数 281 浏览 4 评论 0原文

当使用VSCODE和PYRIGHT编写自定义Saltstack模块/状态时,我到处都会遇到以下错误:

"__salt__" is not defined

这不是杀手,因为我可以将以下内容放在引用它的每一行的末尾

# pyright: ignore[reportUndefinedVariable]

:做是告诉我的项目,__盐__是已知变量,并且不要在该变量上报告。

这可能吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔一刀 2025-02-19 01:28:16

您可以通过在__内置__。pyi文件中声明变量来做到这一点。参见 this

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

一刻暧昧 2025-02-19 01:28:16

我最终制定了正确的解决方案,该解决方案可与3.5更新的Python一起使用,并且我绊倒了所有变量。

from typing import Any, TYPE_CHECKING

if TYPE_CHECKING:
    __salt__: Any = None
    __states__: Any = None
    __opts__: Any = None

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文