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 on lambda #9557

Open
holmanb opened this issue Apr 18, 2024 · 0 comments
Open

cell-var-from-loop false positive on lambda #9557

holmanb opened this issue Apr 18, 2024 · 0 comments
Labels
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

@holmanb
Copy link

holmanb commented Apr 18, 2024

Bug description

The cell-var-from-loop warning shows up on conditional use of SystemExit's code property. This appears similar to, but not the same as #5508, which appears specific to keyword arguments.

Given the following code, run pylint on it. Note the comment.

import sys
import pytest


def test_invalid_W0640():
    """
    This test demonstrates a false positive for W0640.

    Message emitted:
    Cell variable %s defined in loop

    Description:
    A variable used in a closure is defined in a loop. This will result in all
    closures using the same value for the closed-over variable.
    """
    def run_closure(closure):
        closure()

    for i in range(5):
        with pytest.raises(SystemExit) as e:
            # pylint incorrectly thinks that the following closure will cause
            # all runs to have to same value
            run_closure(lambda *_: sys.exit(i))
        assert e.value.code == i

Configuration

No response

Command used

pylint test_pylint.py

Pylint output

************* Module test
test.py:23: [W0640(cell-var-from-loop), test_invalid_W0640.<lambda>] Cell variable i defined in loop

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

Expected behavior

No error

Pylint version

pylint 3.0.3
astroid 3.0.2
Python 3.12.2 (main, Apr  2 2024, 18:40:52) [GCC 13.2.0]

OS / Environment

Ubuntu 24.04, pylint installed via pip

Additional dependencies

pytest if you want to demonstrate that the code works as intended

@holmanb holmanb added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Apr 18, 2024
@DanielNoord DanielNoord added 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 and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Apr 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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

2 participants