Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

W0640 cell-var-from-loop message for variable used in key argument to sorted(). #9386

Closed
RogerMarsh opened this issue Jan 23, 2024 · 1 comment
Labels
Duplicate 🐫 Duplicate of an already existing issue

Comments

@RogerMarsh
Copy link

RogerMarsh commented Jan 23, 2024

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:

        lookup = {}
        for voter in sorted(
            population,
            key=lambda person: sort_key(person, database, lookup),
        ):

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 = {}
    for count, population in enumerate(populations):
        lookup = {}
        # lookup.clear()
        for voter in 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 = {}
    for count, population in enumerate(populations):
        # lookup = {}
        lookup.clear()
        for voter in 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$

Pylint output

************* Module cell_var_from_loop
cell_var_from_loop.py:14:58: W0640: Cell variable lookup defined in loop (cell-var-from-loop)

Expected behavior

I would expect neither version of the code to get the W0640 message.

Pylint version

Script started on Tue Jan 23 14:38:13 2024
opendev$ pylint --version
pylint 2.15.9
astroid 2.12.13
Python 3.10.13 (main, Oct  5 2023, 16:21:31) [Clang 13.0.0 ]
opendev$ exit

Script done on Tue Jan 23 14:38:39 2024

OS / Environment

No response

Additional dependencies

No response

@RogerMarsh RogerMarsh added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Jan 23, 2024
@jacobtylerwalls
Copy link
Member

Thanks for the report! Duplicate of #5508.

@jacobtylerwalls jacobtylerwalls closed this as not planned Won't fix, can't repro, duplicate, stale Jan 24, 2024
@jacobtylerwalls jacobtylerwalls added Duplicate 🐫 Duplicate of an already existing issue and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Jan 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate 🐫 Duplicate of an already existing issue
Projects
None yet
Development

No branches or pull requests

2 participants