How to avoid PEP8 'imported but unused'

3491 views
Skip to first unread message

Adam Jiang

unread,
May 5, 2013, 12:00:54 PM5/5/13
to pytho...@python.org
I am new to python. Now, I am woring on an application within Django
framework. When I checked my code with pep8 and pyflakes, some warning
messages show up-'Foobar imported but unused'. Obviously, it indicates
that some modules are imprted to current module but never get
references. However, it seems the message is wrong in this case:

# file: urls.py
urlpattens = patterns(
'',
url('^signup/$', 'signup')
}

# file: register.py
def signup(request):
return ...

# file: views.py
import signup from register

The warning message is shown in file views.py. It seems to me that the
code is okay because Django requires all functions serve as 'view' is
typically go into views.py. 'import' is about get 'signup' function
into module 'views.py'. Or, I am totally wrong? Is there a proper way
to avoid this warnning?

Best regards,
/Adam

Fábio Santos

unread,
May 5, 2013, 12:18:40 PM5/5/13
to Adam Jiang, pytho...@python.org

I usually do this on pyflakes:

import whatever
assert whatever  # silence pyflakes

Pyflakes and pep8 have no way of knowing django will import and use your module, or whether you are just importing a module for the side effects, so they issue a warning anyway. Assert'ing counts as using the module, so it counts as an used import.

> --
> http://mail.python.org/mailman/listinfo/python-list

MRAB

unread,
May 5, 2013, 12:21:36 PM5/5/13
to pytho...@python.org
On 05/05/2013 17:00, Adam Jiang wrote:
> I am new to python. Now, I am woring on an application within Django
> framework. When I checked my code with pep8 and pyflakes, some warning
> messages show up-'Foobar imported but unused'. Obviously, it indicates
> that some modules are imprted to current module but never get
> references. However, it seems the message is wrong in this case:
>
> # file: urls.py
> urlpattens = patterns(
> '',
> url('^signup/$', 'signup')
> }
>
> # file: register.py
> def signup(request):
> return ...
>
> # file: views.py
> import signup from register
>
> The warning message is shown in file views.py. It seems to me that the
> code is okay because Django requires all functions serve as 'view' is
> typically go into views.py. 'import' is about get 'signup' function
> into module 'views.py'. Or, I am totally wrong? Is there a proper way
> to avoid this warnning?
>
It's not:

import signup from register

(that's an error) but:

from register import signup

After fixing that, does it still show the warning?

Peter Otten

unread,
May 5, 2013, 12:38:46 PM5/5/13
to pytho...@python.org
Adam Jiang wrote:

> I am new to python. Now, I am woring on an application within Django
> framework. When I checked my code with pep8 and pyflakes, some warning
> messages show up-'Foobar imported but unused'. Obviously, it indicates
> that some modules are imprted to current module but never get
> references. However, it seems the message is wrong in this case:
>
> # file: urls.py
> urlpattens = patterns(
> '',
> url('^signup/$', 'signup')
> }
>
> # file: register.py
> def signup(request):
> return ...
>
> # file: views.py
> import signup from register
>
> The warning message is shown in file views.py. It seems to me that the
> code is okay because Django requires all functions serve as 'view' is
> typically go into views.py. 'import' is about get 'signup' function
> into module 'views.py'. Or, I am totally wrong? Is there a proper way
> to avoid this warnning?

pylint has a way to suppress such warnings with a comment like

from signup import register # pylint:disable=W0611

but personally I find the magic comment more annoying than the false
warning...

Adam Jiang

unread,
May 5, 2013, 12:40:31 PM5/5/13
to pytho...@python.org
Thanks. It works very well.

One more question. In this particular case it seems 'assert' should be
safe as a workaround, doesn't it? 'assert' will check if the symbol
is imported and not NULL. Is there side effect if I just applied this
rule as a generic one.

/Adam
On Sun, May 05, 2013 at 05:18:40PM +0100, F�bio Santos wrote:
> I usually do this on pyflakes:
>
> import whatever
> assert whatever� # silence pyflakes
>
> Pyflakes and pep8 have no way of knowing django will import and use your
> module, or whether you are just importing a module for the side effects, so
> they issue a warning anyway. Assert'ing counts as using the module, so it
> counts as an used import.
>
> On 5 May 2013 17:05, "Adam Jiang" <jiang...@gmail.com> wrote:
> >
> > I am new to python. Now, I am woring on an application within Django
> > framework. When I checked my code with pep8 and pyflakes, some warning
> > messages show up-'Foobar imported but unused'. Obviously, it indicates
> > that some modules are imprted to current module but never get
> > references. However, it seems the message is wrong in this case:
> >
> > # file: urls.py
> > urlpattens = patterns(
> > � � '',
> > � � url('^signup/$', 'signup')
> > }
> >
> > # file: register.py
> > def signup(request):
> > � � return ...
> >
> > # file: views.py
> > import signup from register
> >
> > The warning message is shown in file views.py. It seems to me that the
> > code is okay because Django requires all functions serve as 'view' is
> > typically go into views.py. 'import' is about get 'signup' function
> > into module 'views.py'. Or, I am totally wrong? Is there a proper way
> > to avoid this warnning?
> >

Fábio Santos

unread,
May 5, 2013, 1:27:44 PM5/5/13
to Adam Jiang, pytho...@python.org
That assert will never fail. If the symbol is not imported, the import
statement raises ImportError. And actually "assert" makes sure that
the value is not false-ish, not None/Null. And AFAIK a module object
is *always* true.

> One more question. In this particular case it seems 'assert' should be
> safe as a workaround, doesn't it? 'assert' will check if the symbol
> is imported and not NULL. Is there side effect if I just applied this
> rule as a generic one.
>

--
Fábio Santos

Adam Jiang

unread,
May 5, 2013, 10:47:57 PM5/5/13
to Fábio Santos, pytho...@python.org
Thank you. Problem solved.

/Adam
> F�bio Santos
Reply all
Reply to author
Forward
0 new messages