如何在 vscode 和 mypy 之间同步类型提示

发布于 2025-01-20 16:22:24 字数 539 浏览 1 评论 0原文

例如,以下(愚蠢)示例将在vscode中引起错误:

class Something:
    def __init__(self, x):
        self.x = x

    def something_else(self, y) -> Something:
        return Something(self.x + y)

在此处看到:

“在此处输入图像说明”

其中aS mypy script.py将不会返回任何错误。

我不确定这里有什么区别,但是我宁愿将其优先考虑,因为这是我用来捕获类型错误的原因。

For example the following (silly) example will raise an error within vscode:

class Something:
    def __init__(self, x):
        self.x = x

    def something_else(self, y) -> Something:
        return Something(self.x + y)

Seen here:

enter image description here

Where as mypy script.py will return no errors.

I'm not sure what the difference is here, but I'd rather mypy took priority as that's what I'm using to catch type errors.

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

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

发布评论

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

评论(1

初与友歌 2025-01-27 16:22:24

某物未定义,因为在类定义完成之前,它将无法定义。您会注意到,运行此代码会导致nameError,因此在这种情况下,Pylance和Pylint是正确的,您可能不应该使他们的警告保持沉默。

请参阅这个问题如何解决该问题。

Something isn't defined as it won't be defined until the class definition is complete. You'll notice that running this code will cause a NameError, so in this case Pylance and Pylint are correct and you probably shouldn't silence their warnings.

Refer to this question for how to fix that.

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