pyflakes icon indicating copy to clipboard operation
pyflakes copied to clipboard

redefinition of unused 'somemodule' from line X

Open unode opened this issue 13 years ago • 6 comments

try:
    import simplejson as json
except ImportError:
    import json

with open('file.json') as fh:
    json.load(fh)

The previous block of code will generate the error "redefinition of unused 'json' from line 4 ". But in this case I don't see why it should be an error.

unode avatar Mar 15 '11 13:03 unode

Although different in context, it might be related to this issue.

unode avatar Mar 15 '11 13:03 unode

This annoys me too, but I'm not 100% sure how to generalize it into something that doesn't miss actual errors.

But yeah: imports that are inside a try/except ImportError: block could be marked as such, and then if redefined in the except block, not raise a message. I'll give it a shot for the next release!

kevinw avatar Mar 15 '11 14:03 kevinw

Although different in context, it might be related to this issue.

That issue is separate and has been resolved. The case reported here still occurs in the latest version.

ervandew avatar Mar 15 '11 14:03 ervandew

I'll be waiting for the fix. Thanks for the quick reply.

unode avatar Mar 15 '11 14:03 unode

While the fix is still coming, this is how it can be worked around, if you're wondering:

try:
      from unittest.runner import _WritelnDecorator
     _WritelnDecorator; # workaround for pyflakes issue #13
except ImportError:
     from unittest import _WritelnDecorator

Substitude _unittest and _WritelnDecorator with the entities (modules, functions, classes) you need

deemoowoor avatar Sep 20 '11 11:09 deemoowoor

Any progress on this? A related issue is code like this:


if i==1:
    def g():
        return 1
else:
    def g():
        return 2

which gives a "redefinition of function" warning.

amueller avatar Jul 23 '12 21:07 amueller