Skip to content

--warn-unused-ignore inconsistent with removed branches #9652

Closed
@dvarrazzo

Description

@dvarrazzo

Bug Report

I seems to understand that mypy can detect unused branched (ones checked against sys.version_info) and avoid checking them. However --warn-unused-ignore doesn't apply the same filtering.

As a consequence, an # ignore in a branch that goes unchecked is reported as unused, instead of being ignored. So you can have a statement that is reported as error running mypy with a certain Python version, but, if ignored, the same line will be reported as unneeded with a different Python version.

To Reproduce

# test.py
import sys

def f(a: str) -> int:
    return len(a)

f("hello")
if sys.version_info < (3, 7):
    f(b"world")

Running mypy --warn-unused-ignores test.py with Python 3.6, it will report an arg-type error on the last line, whereas a Python 3.8 test run will be clean.

If a type: ignore[arg-type] comment is added to the last line, the Python 3.6 run will be clean, but Python 3.8 will fail with "unused 'type: ignore' comment".

The expectation is that the type: ignore would be ignored by Python 3.8, if such branch is not checked.

Your Environment

Tested with mypy 0.782, 0.790

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions