[Python-projects] pylint: E0601 false negative

Sylvain Thénault sylvain.thenault at logilab.fr
Wed Aug 6 21:03:55 CEST 2008


On Wed, Aug 06, 2008 at 07:51:48PM +0200, Maarten ter Huurne wrote:
> Hi,

Hi Maarten,
 
> In the following Python fragment, the variable "x" is referenced before it 
> is assigned in two occasions:
> 
> def f(n):
>     if n == 0:
>         if x == 1:
>             pass
>     else:
>         print x
>         x = 3
> 
> There is a rule in the "variables" checker that should catch errors like 
> this: message E0601. But for some reason this message is not issued.
> 
> The reason seems to be the "if x == 1" statement. If this is replaced by 
> "pass", the message will be issued.
> 
> I looked at the code of the variables checker and the implementation of 
> E0601 is in the visit_name() method. This is the final part of the 
> decision whether to issue the message or not:
> 
>                     if (maybee0601
>                         and stmt.source_line() <= defstmt.source_line()
>                         and not is_defined_before(node)
>                         and not are_exclusive(stmt, defstmt)):
>                         self.add_message('E0601', args=name, node=node)
> 
> I added some debug prints and the reason the message is not issued is that 
> "are_exclusive(stmt, defstmt)" returns True. Here "stmt" is the "if x == 
> 1" statement and "defstmt" is the "x = 3" assignment. Indeed those two 
> branches are exclusive. However, that is not a valid reason why the use of 
> "x" should be considered correct. In fact, when the branch in which a 
> variable is assigned is exclusive to a branch in which a variable is used, 
> that would be a reason to consider its use wrong.

I've added a ticket for this, I'll take a look asap:

http://www.logilab.org/ticket/5719 

thank you for your investigation!
-- 
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