Skip to content

'local variable referenced before assignment' false positive #315

Closed

Description

@kgraves

Basic Info:

  • python v3.5.4
  • flake8 v3.5.0
  • pyflakes v1.5.0 (installed as flake8 dependency)

I've vastly simplified this bit of code to make it easier to look at.

I understand this code isn't necessarily best practice, but I ran across this in a project at work, and it took me a while to hunt down what the error message was referring to.

from module import errors


def test_f823():
    try:
        errors.blah('some_value')  # this line should not produce a pyflakes error
    except Exception as error:
        errors = [error]
        return errors

output:
/tmp/tester.py:6: local variable 'errors' (defined in enclosing scope on line 1) referenced before assignment

Let me know if you need anymore info.

Activity

  1. myint commented on Nov 28, 2017

    @myint
    Member

    I don't have time to look into the details right now, but this seems somewhat reminiscent of previous conditional-related issues we've seen this in the past (like #252).

    (Though, in this particular example, there would an error in the code even if weren't for the errors.blah() false positive. Pyflakes would rightfully complain about the redefinition of errors.)

  2. bitglue commented on Nov 28, 2017

    @bitglue
    Member

    But it is an error. Try this variation, which is a program we should expect to print hello world!:

    from pprint import pprint
    
    
    def test_f823():
        try:
            pprint('hello world!')  # this line should not produce a pyflakes error
        except Exception as error:
            pprint = [error]
            return pprint
    
    
    if __name__ == '__main__':
        print(test_f823())
    $ python3 test.py
    [UnboundLocalError("local variable 'pprint' referenced before assignment",)]
    $ python2 test.py
    [UnboundLocalError("local variable 'pprint' referenced before assignment",)]
  3. sigmavirus24 commented on Nov 28, 2017

    @sigmavirus24
    Member

    Right. This isn't a matter of pyflakes being nitpicky, this is totally a language issue.

  4. bitglue commented on Nov 28, 2017

    @bitglue
    Member

    I'm closing this as it appears this is a true positive. Please reopen if there's something I'm overlooking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions