[Python-projects] pylint: Differentiate between unused 'import X'and unused 'from X import *'

Alexandre Fayolle alexandre.fayolle at logilab.fr
Tue Dec 5 09:36:22 CET 2006


Two more arguments against import *. 

On Thu, Nov 30, 2006 at 06:07:02PM +0100, Maarten ter Huurne wrote:
> I agree that having separate message IDs for the two cases is an 
> improvement.
> 
> However, I would recommend you not to use "*" imports at all, since they 
> import everything, including:
> - private (implementation) definitions
> - the imports done by the module you are importing
 
 - it can lead to symbol overwrite if the new module changes its
implementation (and defines a new function with the same name as a
function imported earlier) (it may sound unlikely, but I got bitten by this
once, and it is quite surprising)

Additionally, importing * makes it harder to find where a function is
defined, especially if import * is used more than once in a given
module, or if the imported module uses import * too. 

These shortcomings can be mitigated by defining __all__ as a list of
exported symbols in the imported module, and naming private methods with
names starting with underscores, since these don't get imported by
import *. 

If this is feasible, I'd appreciate if pylint could tell me if an 
import * statement targets a module with __all__ defined or not. Maybe
another message for this could be required, I'm not sure. I have a gut
feeling that it is less evil to import * in that case (since the module
is obviously meant to be imported like this, otherwise the author would
not have taken the time to list the symbols. Opinions on this? 

-- 
Alexandre Fayolle                              LOGILAB, Paris (France)
Formations Python, Zope, Plone, Debian:  http://www.logilab.fr/formations
Développement logiciel sur mesure:       http://www.logilab.fr/services
Informatique scientifique:               http://www.logilab.fr/science
Reprise et maintenance de sites CPS:     http://www.migration-cms.com/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 481 bytes
Desc: Digital signature
Url : http://lists.logilab.org/pipermail/python-projects/attachments/20061205/bb0a3626/attachment.pgp 


More information about the Python-Projects mailing list