如何检查 Mypy `# type: ignore` 注释是否仍然有效且是必需的?
How to check if Mypy `# type: ignore` comments are still valid and required?
想象一下,我们有一些巨大的遗留代码库,其中包含许多忽略了 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
是否有现成的工具可以实现这一点?关于自定义脚本有什么想法吗?
更新:
我唯一的想法:
- 编写一个脚本来查找并记住一个文件和一行 Mypy 注释。
- 查找并删除所有 Mypy 评论。
- 运行 Mypy 检查 -> 存储结果。
- 将 Mypy 检查错误行与存储的旧行进行比较。
- 找出不同之处:如果注释被删除,但 Mypy 现在没有抱怨该行,则必须删除注释。
--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.
想象一下,我们有一些巨大的遗留代码库,其中包含许多忽略了 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
是否有现成的工具可以实现这一点?关于自定义脚本有什么想法吗?
更新:
我唯一的想法:
- 编写一个脚本来查找并记住一个文件和一行 Mypy 注释。
- 查找并删除所有 Mypy 评论。
- 运行 Mypy 检查 -> 存储结果。
- 将 Mypy 检查错误行与存储的旧行进行比较。
- 找出不同之处:如果注释被删除,但 Mypy 现在没有抱怨该行,则必须删除注释。
--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.