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

问题描述

我想在 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 似乎不起作用。当我删除“no-implicit-optional”选项时,它实际上只会捕获误报。

我错过了什么?

解决方法

我找到了答案 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"
    ]

}