How to check if Mypy `# type: ignore` comments are still valid and required?

Viewed 3840

Imagine we have some giant legacy code base with a lot of files with ignored Mypy warnings:

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

Time to go...

Some parts of code were changed. Some parts of code is still the same. How to check every "ignore" comment to know: will I get an error if I remove it?

Desired output:

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

Is there any existing tools to achieve this? Any ideas about custom scripts?

UPD:

The only idea that I have:

  1. Write a script that will find and remember a file and a line of every Mypy comment.
  2. Find and remove ALL Mypy comments.
  3. Run Mypy check -> store results.
  4. Compare the Mypy check errors lines with a stored OLD lines.
  5. Find a difference: if a comment was removed, but Mypy does not complain now about that line, then the comment must be removed.
1 Answers

From 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.

Related