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

[cell-var-from-loop] false positive for lambda used as keyword arg #5508

Open
kowalcj0 opened this issue Dec 12, 2021 · 1 comment
Open

[cell-var-from-loop] false positive for lambda used as keyword arg #5508

kowalcj0 opened this issue Dec 12, 2021 · 1 comment
Labels
direct parentage assumption False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation

Comments

@kowalcj0
Copy link

Bug description

Hi,

I'm little confused by this warning which pylint 2.12.2 found in the following example.py:

"""cell-var-from-loop example."""

grouped_messages = {
    "category_a": [
        {"key_a": 3},
        {"key_a": 1},
        {"key_a": 2},
    ],
    "category_b": [
        {"key_b": 5},
        {"key_b": 3},
        {"key_b": 8},
    ],
}
sorting_keys = {
    "category_a": "key_a",
    "category_b": "key_b",
}

sorted_messages = {}

for category, messages in grouped_messages.items():
    sorted_messages[category] = sorted(
        (message for message in messages),
        key=lambda msg: msg[sorting_keys[category]]
    )

print(sorted_messages)

I got rid of this warning by replacing key=lambda msg: msg[sorting_keys[category]] with key=operator.itemgetter(sorting_keys[category]).
TBH I'm not sure why pylint complains about category being defined in loop.
Especially, because both versions of the code, give exactly the same result:

python3 example.py 
{'category_a': [{'key_a': 1}, {'key_a': 2}, {'key_a': 3}], 'category_b': [{'key_b': 3}, {'key_b': 5}, {'key_b': 8}]}

This might be a continuation of: #4827

Thanks

Configuration

No response

Command used

pylint example.py

Pylint output

************* Module example
example.py:25:41: W0640: Cell variable category defined in loop (cell-var-from-loop)

------------------------------------------------------------------
Your code has been rated at 8.33/10 (previous run: 8.33/10, +0.00)

Expected behavior

No warnings

Pylint version

pylint 2.12.2
astroid 2.9.0
Python 3.10.0 (default, Oct  4 2021, 00:00:00) [GCC 11.2.1 20210728 (Red Hat 11.2.1-1)

OS / Environment

Fedora 35

Additional dependencies

astroid==2.9.0
isort==5.10.1
lazy-object-proxy==1.6.0
mccabe==0.6.1
platformdirs==2.4.0
pylint==2.12.2
toml==0.10.2
wrapt==1.13.3

@kowalcj0 kowalcj0 added Bug 🪲 Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Dec 12, 2021
@Pierre-Sassoulas Pierre-Sassoulas added False Positive 🦟 A message is emitted but nothing is wrong with the code and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Dec 12, 2021
@Pierre-Sassoulas Pierre-Sassoulas added Needs investigation 🔬 A bug or crash where it's not immediately obvious what is happenning and removed Bug 🪲 labels Jul 5, 2022
@jacobtylerwalls jacobtylerwalls changed the title Possibly a false positive warning W0640: Cell variable category defined in loop (cell-var-from-loop) [cell-var-from-loop] false positive for lambda used as keyword arg Jan 24, 2024
@jacobtylerwalls jacobtylerwalls added Needs PR This issue is accepted, sufficiently specified and now needs an implementation and removed Needs investigation 🔬 A bug or crash where it's not immediately obvious what is happenning labels Jan 24, 2024
@jacobtylerwalls
Copy link
Member

jacobtylerwalls commented Jan 24, 2024

Reproduced @ 27e7272.

We try to exclude lambdas that are called immediately, but we're fooled when an assumption of direct parentage is violated here:

def is_being_called(node: nodes.NodeNG) -> bool:
"""Return True if node is the function being called in a Call node."""
return isinstance(node.parent, nodes.Call) and node.parent.func is node

The lambda's parent is a Keyword node, and that has a parent of Call.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
direct parentage assumption False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Projects
None yet
Development

No branches or pull requests

3 participants