False positive undefined name after del in branch

Bug #1308508 reported by Dima Tisnek
16
This bug affects 3 people
Affects Status Importance Assigned to Milestone
Pyflakes
New
Wishlist
Unassigned

Bug Description

Consider this test code:

def foo():
    bar = 1
    if 0:
        del bar
    else:
        del bar

pyflakes reports test.py:6: undefined name 'bar', that is 2nd `del bar` is considered in error.

understandably it is impossible to analyse all code paths in general case, so how about tagging `bar` as `uncertain if label is present` and allowing to `del` it?

Revision history for this message
Luciano Ramalho (ramalho) wrote :

Here is another false positive, for a not-so-subtle case:

Using flake8, which uses PyFlakes, I get the report:

board.py:14:19: F821 undefined name 'StandardError'

However, StandardError is a built-in exception in Python 2 (but not in Python 3).

Revision history for this message
Florent (florent.x) wrote :

I agree with the initial bug reported : probably we could do better in this case.

The issue reported by @ramalho is not related to the initial report, and it's not an issue at all:

    $ python3 -m pyflakes <<< StandardError
    <stdin>:1: undefined name 'StandardError'
    $ python2 -m pyflakes <<< StandardError
    $

Changed in pyflakes:
importance: Undecided → Wishlist
Revision history for this message
Daniel Hahler (blueyed) wrote :

I came across a similar issue like in comment #1, where flake8/pyflakes on python3 reports "F821: undefined name 'unicode'" , but the code path is not triggered for Python3:

    if sys.version_info < (3, 0):
        def __unicode__(self):
            return self.url

        def __add__(self, other):
            return unicode(self) + other
    else:
        def __str__(self):
            return self.url

        def __add__(self, other):
            return str(self) + other

Revision history for this message
John Vandenberg (jayvdb) wrote :

This bug is about `del` (and the cases in https://bugs.launchpad.net/pyflakes/+bug/1431099 appear to be the same problem)

summary: - False positive F821 undefined name 'xxx'
+ False positive F821 undefined name after del in branch
summary: - False positive F821 undefined name after del in branch
+ False positive undefined name after del in branch
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.