Search code examples
python-3.xmypyflake8

Mypy + flake8: Is there any way to surpress warning of `F821 undefined name`


In the following code, flake8 say F821 undefined name 'B'. But for mypy, type hint for f is neccesary. How to ignore such warnings by flake8?

def f(b: B) -> None:
    pass


class B():
    pass

This example can be solved trivially: change the order of declarations. But sometimes I cannot change the orders in realistic cases . Such misleading warnings are noisy for me.

My env: Python 3.6 + flake8 3.6.0 + mypy 0.641 + flake8-mypy 17.8.0


Solution

  • You can use # noqa: to silence some errors. Example:

    test.py

    def f(b: B) -> None:  # noqa: F821
        pass
    
    
    class B():
        pass
    

    bash

    $ flake8 test.py
    (return no error)
    

    See also: http://flake8.pycqa.org/en/3.1.1/user/ignoring-errors.html#in-line-ignoring-errors