Search code examples
pythonpython-2.7python-importflake8

#noqa works only on one line import but not on import ()


I have a list of imports I used in a file:

import (
a,
b,
c 
)

However, this imports are @pytest.fixture and thus, they are not called explicitly in the code and so I got "imported but unused" Flake8 error, as expected.

I tried to do the following:

  1. use as, e.g.:

    import (
        a as a,
        b as b,
        ...
    )
    
  2. add #noqa at each line end, e.g.:

    import (
        a, #noqa
        b, #noqa
        ...
    )
    
  3. add #noqa at the end of the import, i.e.:

    import (
        ...
    ) #noqa
    

But none have worked. Only splitting the import into separate lines did the trick, i.e.

import a #noqa
import b #noqa
...

Why is that and am I missing a simpler way to do so?


Solution

  • your original code is a syntax error so I'm assuming you mean something like this:

    from mod import (
        a,
        b,
        c,
    )
    

    if you run this through flake8 you get:

    $ flake8 t.py 
    t.py:1:1: F401 'mod.a' imported but unused
    t.py:1:1: F401 'mod.b' imported but unused
    t.py:1:1: F401 'mod.c' imported but unused
    

    to inline-disable lint codes, use a # noqa comment on the line where the error occurs (or if it is a triple quoted string or backslash-escaped string then use the last line) -- the former case applies here

    from mod import (  # noqa: F401
        a,
        b,
        c,
    )
    

    and:

    $ flake8 t.py
    $ echo $?
    0
    

    disclaimer: I'm the current flake8 maintainer