via*_*ixt 8 python type-hinting visual-studio-code 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",
]
Run Code Online (Sandbox Code Playgroud)
然而,我有一些没有类型注释的函数,我没有得到任何关于它们的信息、警告或错误。实际上 Mypy 似乎不起作用。当我删除“无隐式可选”选项时,它实际上只会捕获误报。
我缺少什么?
via*_*ixt 14
我在这里找到了答案,然后意识到我的错误。在 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"
]
}
Run Code Online (Sandbox Code Playgroud)
新方法是简单地安装 Microsoft 提供的扩展,并且您可以根据项目自定义的上述设置现在有所不同,例如,您将提供“mypy-type-checker.args”,而不是“python.linting.mypyArgs” 。
请参阅此处:https ://marketplace.visualstudio.com/items?itemName=ms-python.mypy-type-checker