Skip to content
This repository has been archived by the owner on Mar 7, 2021. It is now read-only.

redefinition of unused 'somemodule' from line X #13

Open
unode opened this issue Mar 15, 2011 · 6 comments
Open

redefinition of unused 'somemodule' from line X #13

unode opened this issue Mar 15, 2011 · 6 comments

Comments

@unode
Copy link

unode commented Mar 15, 2011

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
Copy link
Author

unode commented Mar 15, 2011

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

@kevinw
Copy link
Owner

kevinw commented Mar 15, 2011

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!

@ervandew
Copy link
Contributor

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.

@unode
Copy link
Author

unode commented Mar 15, 2011

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

@deemoowoor
Copy link

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

@amueller
Copy link

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.

saper added a commit to saper/pyflakes that referenced this issue Jan 6, 2013
saper added a commit to saper/pyflakes that referenced this issue Jan 6, 2013
Do not raise warning on common idioms like

try:
    import something
except ImportError:
    import somethingelse as something

Implements a simple check whether duplicate definition
or import share a common parent AST node that
is either <_ast.If> or <_ast.TryExcept>.
In that case additional verification is performed
whether duplicate definitions/imports were written
together. In case of multiple definitions on
different flow control forks (try/except/else,
if/else) no warning is raised.

No attempt is being made to perform smarter analysis
of the case.

Should fix most obvious cases of:
kevinw#13
kevinw added a commit that referenced this issue Jan 12, 2013
ypwais pushed a commit to ypwais/StarCluster that referenced this issue Dec 7, 2013
Use the work-around fix mentioned in the following pyflakes issue:

kevinw/pyflakes#13

This avoids the need to install a patched version of pyflakes until the
"#pyflakes:ignore" patch is included in a future release. Once the
pyflakes patch is included in a new release this commit should be
reverted to using "#pyflakes:ignore" instead.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants