gpt4 book ai didi

python - 有没有办法让 flake8 检查源代码中的类型提示

转载 作者:太空狗 更新时间:2023-10-29 21:41:51 27 4
gpt4 key购买 nike

我正在开始一个新的 python 项目(python 3.5+),我想在整个代码库中强制执行类型提示。有没有办法用 flake8 或任何其他工具来做到这一点?

最佳答案

看看mypy .

来自网站:

Mypy is an experimental optional static type checker for Python that aims to combine the benefits of dynamic (or "duck") typing and static typing. Mypy combines the expressive power and convenience of Python with a powerful type system and compile-time type checking. Mypy type checks standard Python programs; run them using any Python VM with basically no runtime overhead.

编辑

实际上 mypy 是一个类型检查器,所以默认情况下它只检查是否存在与提示或推断的类型相关的错误。为了也让它报告非提示类型,您可以使用额外的命令行开关,如记录 here .

--disallow-untyped-defs reports an error whenever it encounters a function definition without type annotations.

--check-untyped-defs is less severe than the previous option – it type checks the body of every function, regardless of whether it has type annotations. (By default the bodies of functions without annotations are not type checked.) It will assume all arguments have type Any and always infer Any as the return type.

--disallow-untyped-calls reports an error whenever a function with type annotations calls a function defined without annotations.

...

关于python - 有没有办法让 flake8 检查源代码中的类型提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43187829/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com