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

Discussão em 'Python' iniciado por Stack, Março 12, 2025.

  1. Stack

    Stack Membro Participativo

    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


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

    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.

    Continue reading...

Compartilhe esta Página