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

Sylvain Thénault sylvain.thenault at logilab.fr
Tue Dec 5 10:06:25 CET 2006


On Tuesday 05 December à 09:36, Alexandre Fayolle wrote:
> 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? 

Sound good to me. File a feature request :)
-- 
Sylvain Thénault                               LOGILAB, Paris (France)
Formations Python, Zope, Plone, Debian:  http://www.logilab.fr/formations
Développement logiciel sur mesure:       http://www.logilab.fr/services
Python et calcul scientifique:           http://www.logilab.fr/science



More information about the Python-Projects mailing list