gpt4 book ai didi

python - 如何检查 Mypy `# type: ignore` 评论是否仍然有效且需要?

转载 作者:行者123 更新时间:2023-12-05 01:08:58 27 4
gpt4 key购买 nike

想象一下,我们有一些巨大的遗留代码库,其中包含许多忽略 Mypy 警告的文件:

def foobar():
x = some_external_class.some_method()[0] # type: ignore[ignore-some-mypy-warning]

该走了……

部分代码已更改。部分代码还是一样的。如何检查每个“忽略”评论以了解:如果我删除它会出错吗?

期望的输出:

Checked 100500 files!
You do not need "ignore" comments anymore in the following files:
- spam.py:534
- eggs.py:31
- eggs.py:250

是否有任何现有的工具可以实现这一目标?关于自定义脚本的任何想法?

UPD:

我唯一的想法:

  1. 编写一个脚本来查找并记住每个 Mypy 注释的文件和一行。
  2. 查找并删除所有 Mypy 评论。
  3. 运行 Mypy 检查 -> 存储结果。
  4. 将 Mypy 检查错误行与存储的 OLD 行进行比较。
  5. 找出不同之处:如果删除了评论,但 Mypy 现在没有提示该行,则必须删除该评论。

最佳答案

来自 mypy documentation :

--warn-unused-ignores

This flag will make mypy report an error whenever your code uses a # type: ignore comment on a line that is not actually generating an error message.

关于python - 如何检查 Mypy `# type: ignore` 评论是否仍然有效且需要?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65579151/

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