You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The presented code is nonsense, derived from the actual code to demonstrate what pylint makes of it.
I do not think the version without the W0640 message is any more, or less, sound as Python code than the version with it.
Configuration
No response
Command used
opendev$
opendev$ cd tests
opendev$ cat cell_var_from_loop.py
"""cell-var-from-loop warning test."""
def do_stuff(number, item, colleague):
"""Do stuff."""
del number, item, colleague
def generate_report(populations, database):
"""Test function."""# lookup = {}forcount, populationin enumerate(populations):
lookup = {}
# lookup.clear()forvoterin sorted(
population,
key=lambda person: sort_key(person, database, lookup),
):
do_stuff(count, lookup[voter.code], voter)
def sort_key(individual, database, lookup):
"""Sort before doing 'do_stuff'. lookup is provided to avoid doing 'database.stuff' both in the sort and in 'do_stuff' in the loop on 'sorted'. The keys of lookup are unique in population."""
lookup[individual.some_property] = database.lookup_stuff()
return lookup[individual.some_property]
opendev$
opendev$ pylint cell_var_from_loop.py
************* Module cell_var_from_loop
cell_var_from_loop.py:14:58: W0640: Cell variable lookup defined in loop (cell-var-from-loop)
-------------------------------------------------------------------
Your code has been rated at 9.00/10 (previous run: 10.00/10, -1.00)
opendev$
opendev$ cat cell_var_from_loop.py
"""cell-var-from-loop warning test."""
def do_stuff(number, item, colleague):
"""Do stuff."""
del number, item, colleague
def generate_report(populations, database):
"""Test function."""
lookup = {}
forcount, populationin enumerate(populations):
# lookup = {}lookup.clear()
forvoterin sorted(
population,
key=lambda person: sort_key(person, database, lookup),
):
do_stuff(count, lookup[voter.code], voter)
def sort_key(individual, database, lookup):
"""Sort before doing 'do_stuff'. lookup is provided to avoid doing 'database.stuff' both in the sort and in 'do_stuff' in the loop on 'sorted'. The keys of lookup are unique in population."""
lookup[individual.some_property] = database.lookup_stuff()
return lookup[individual.some_property]
opendev$
opendev$ pylint cell_var_from_loop.py
-------------------------------------------------------------------
Your code has been rated at 10.00/10 (previous run: 9.00/10, +1.00)
opendev$
Bug description
The command used section has two versions of module cell_var_from_loop, one with the W0640 message and one without.
It seems to me the 'for voter in sorted' phrase should count as the loop outside of which the problem variable is defined.
Snippet copied here:
The presented code is nonsense, derived from the actual code to demonstrate what pylint makes of it.
I do not think the version without the W0640 message is any more, or less, sound as Python code than the version with it.
Configuration
No response
Command used
Pylint output
Expected behavior
I would expect neither version of the code to get the W0640 message.
Pylint version
OS / Environment
No response
Additional dependencies
No response
The text was updated successfully, but these errors were encountered: