Skip to content

Top-level assert does not skip 'type: ignore' comments #8234

Closed
@hashstat

Description

@hashstat

When using mypy with the --warn-unused-ignore option, type: ignore comments are still analyzed, and possibly reported, when occurring after assert sys.platform == ... statements.

Take the following module as an example:

# test.py

import sys

assert sys.platform == "win32"

class Foo:
    pass

foo = Foo()
foo.bar = 1  # type: ignore[attr-defined]

Running mypy --warn-unused-ignore --platform win32 test.py finds no errors, as expected. However, mypy --warn-unused-ignore --platform linux test.py reports the following error:

test.py:11: error: unused 'type: ignore' comment
Found 1 error in 1 file (checked 1 source file)

I would expect (by reading the documentation) that the assert should cause the rest of the file to be skipped, including type: ignore comments, since they also are likely platform-specific. The same issue likely occurs for sys.version_info asserts as well.

I am experiencing the issue with Python 3.7.5 and mypy 0.750, 0.760, 0.761, and master.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions