首页 > 解决方案 > 使 flake8 检测 Python 类型错误

问题描述

通过FastAPI 文档,它看起来像以下函数

def get_name_with_age(name: str, age: int):
    name_with_age = name + " is this old: " + age
    return name_with_age

应该通过 Python linter(mypy,在示例中)在 VSCode 中触发错误消息

在此处输入图像描述

我已经在使用 flake8 但它没有检测到此类错误。有没有办法让 flake8 表现得像那样?

标签: pythonvisual-studio-codefastapiflake8

解决方案


对不起,但这看起来不可能。

在 中mypy,此功能由 提供"check_untyped_defs=true"。我们可以在 settings.json 中使用此配置禁用它:

  "python.linting.mypyArgs": [
    "--follow-imports=silent",
    "--ignore-missing-imports",
    "--show-column-numbers",
    "--no-pretty",
    "check_untyped_defs=false"  //this line
  ],

而从 flake8 的官方文档中,它只禁用了这些功能:

By default, Flake8 ignores E121, E123, E126, E226, E24, and E704.

但是,尽管您同时启用了它们,但 flake8 仍然无法正常工作。

所以,看起来 flake8 没有这个功能。


推荐阅读