[code-quality] Warn on list comprehension variables used outside list comprehensions?

code-quality.wting at xoxy.net code-quality.wting at xoxy.net
Wed Nov 26 06:41:53 CET 2014


Python binds list comprehension variables to the local scope which has
caused some subtle bugs. Is it possible to add a warning for this in
pyflakes? I haven't implemented it yet, but here are the example
tests:

def test_listCompVariableUsedOutsideListComp(self):
    """
    Test that a variable defined in a list comprehension is not used
    outside of the list comprehension.
    """
    self.flakes('''
    [x for x in range(3)]
    print x
    ''', m.VariableUsedOutsideListComp)
    self.flakes('''
    [x for x in range(3)]
    [x for _ in range(3)]
    ''', m.VariableUsedOutsideListComp)


def test_listCompVariableAllowReuse(self):
    """
    Test that list comprehension variables are allowed to be reused if
    redefined.
    """
    self.flakes('''
    [x for x in range(3)]
    [x for x in range(3)]''')


- William



More information about the code-quality mailing list