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

Overriding imports gives incorrect error #20

Open
Wilfred opened this issue Aug 8, 2011 · 5 comments
Open

Overriding imports gives incorrect error #20

Wilfred opened this issue Aug 8, 2011 · 5 comments

Comments

@Wilfred
Copy link

Wilfred commented Aug 8, 2011

The following code:

try:
    import cPickle as pickle
except ImportError:
    import pickle

def foo():
    return pickle.dumps

Gives the following error: foo.py:4: redefinition of unused 'pickle' from line 2. However, it is actually used.

Expected error would be "'pickle' imported but unused" or perhaps something like "redefinition of 'pickle' from line 2"

@jehiah
Copy link
Contributor

jehiah commented Sep 6, 2011

I understand why this is misleading, but at the time of parsing line 4, pickle is unused.

The primary error is for the redefinition, and really unrelated to weather or not pickle is unused.

@Wilfred
Copy link
Author

Wilfred commented Sep 6, 2011

I don't quite follow your logic, sorry. Consider this code:

import math

def log(x): return math.log(x)

At time of parsing line 1, math is unused. But usage should be considered after parsing the whole file, and in this example it is.

(I certainly agree that "'pickle' imported but unused" is not an improvement, and it should be "redefinition of 'pickle' from line 2")

@Daenyth
Copy link

Daenyth commented Mar 6, 2012

I can understand why the error would come up, but this is a really common pattern. Isn't there any way you can recognize it and ignore it?

@dbr
Copy link

dbr commented Mar 12, 2012

Hm, pylint sensibly accepts something like:

if some_cond:
    a = 2
else:
    a = 3
print a

Perhaps the same logic could be extended to the try/except, since given:

try:
    something_that_might_raise_an_exception()
    a = 2
except:
    a = 3
print a

a=3 doesn't necessarily redefine a. It's a less well defined situation than the if/else, but as Daenyth says, this is a common pattern, and it'd be nice if it didn't cause the warning!

@saper
Copy link
Contributor

saper commented Jan 6, 2013

This is really duplicate of issue #13 and is fixed with pull request #28.

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