在 VSCode 中配置 Mypy 以强制类型提示

问题描述 投票:0回答:2

我想在 Visual Studio Code 上配置 Mypy 以在我的项目中强制执行类型提示。

我使用以下配置

"python.linting.pylintArgs": [
        "--disable=W0611"
    ],
    "python.linting.mypyEnabled":true,
    "python.linting.mypyCategorySeverity.note":"Warning",
    "python.linting.mypyArgs": [

        "--ignore-missing-imports",
        "--follow-imports=silent",
        "--show-column-numbers",
        "--disallow-untyped-defs=True",
        "--disallow-untyped-calls=True",
        "--check-untyped-defs=True",
        "--no-implicit-optional=True",

    ]

但是,我有一些没有类型注释的函数,并且我没有得到有关它们的任何信息、警告或错误。 实际上 Mypy 似乎不起作用。当我删除“无隐式可选”选项时,它实际上只会捕获误报。

我错过了什么?

python visual-studio-code type-hinting mypy
2个回答
12
投票

我在here找到了答案,然后意识到我的错误。在 mypyArgs 中添加“=True”是错误的。

这是我的新设置:

{
    "python.pythonPath": "C:\\Program Files\\Python37\\python.exe",
    "python.linting.enabled": true,
    "python.linting.pylintEnabled": true,
    "python.linting.pylintArgs": [
 
        "--disable=W0611"
    ],
    "python.linting.mypyEnabled": true,
    "python.linting.mypyCategorySeverity.error":"Warning",
    "python.linting.mypyArgs": [
        "--ignore-missing-imports",
        "--follow-imports=silent",
        "--show-column-numbers",
        "--disallow-untyped-defs",
        "--disallow-untyped-calls"
    ]

}

0
投票

新方法是简单地安装微软提供的扩展,并且您可以根据项目自定义的上述设置现在有所不同,例如您将提供“mypy-type-checker.args”,而不是“python.linting.mypyArgs”。

请参阅此处:https://marketplace.visualstudio.com/items?itemName=ms-python.mypy-type-checker

© www.soinside.com 2019 - 2024. All rights reserved.