Skip to content

Spurious "undefined name" with "type: ignore" comment for mypy #489

@gward

Description

@gward

mypy has the ability to selectively ignore type errors with special syntax in a type annotation comment. These comments confuse pyflakes and cause it to emit spurious "undefined name" errors.

Sample code using this mypy feature:

def foo(a: str):
    if a == 37:
        return ""
    return a + ""

foo(37)                         # type: ignore[arg-type]

(See https://mypy.readthedocs.io/en/latest/error_codes.html#silencing-errors-based-on-error-codes for more detail.)

pyflakes reports undefined names in that type annotation comment:

$ pyflakes pyflakes-test.py
pyflakes-test.py:6: undefined name 'ignore'
pyflakes-test.py:6: undefined name 'arg'

If I change the type annotation comment to

foo(37)                         # type: ignore

then pyflakes is happy. But I'm not happy, because that will prevent mypy from finding other errors in this line.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions