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

False positive E0601: used-before-assignment in try and while block #9689

Open
jc-stu opened this issue Jun 4, 2024 · 0 comments
Open

False positive E0601: used-before-assignment in try and while block #9689

jc-stu opened this issue Jun 4, 2024 · 0 comments
Labels
C: used-before-assignment Issues related to 'used-before-assignment' check 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

@jc-stu
Copy link

jc-stu commented Jun 4, 2024

Bug description

It seems pylint can't process nonlocal statement in try and while block correctly, but no problem in for block.

# pylint: disable=missing-docstring, bare-except

def outer():
    a = 1
    def inner_try():
        try:
            nonlocal a
            print(a)  # E0601
            a = 2
            print(a)
        except:
            pass
    def inner_while():
        i = 0
        while i < 2:
            i += 1
            nonlocal a
            print(a)  # E0601
            a = 2
            print(a)
    def inner_for():
        for _ in range(2):
            nonlocal a
            print(a)  # correct
            a = 2
            print(a)
    inner_try()
    inner_while()
    inner_for()
outer()

Command used

pylint e0601.py

Pylint output

************* Module e0601
e0601.py:8:18: E0601: Using variable 'a' before assignment (used-before-assignment)
e0601.py:18:18: E0601: Using variable 'a' before assignment (used-before-assignment)

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

Expected behavior

There should be no errors.

Pylint version

pylint 3.2.2
astroid 3.2.2
Python 3.12.3 (tags/v3.12.3:f6650f9, Apr  9 2024, 14:05:25) [MSC v.1938 64 bit (AMD64)]

OS / Environment

Windows 10 LTSC 2019
Version 1809 (OS Build 17763.5830)

@jc-stu jc-stu added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Jun 4, 2024
@jacobtylerwalls jacobtylerwalls added False Positive 🦟 A message is emitted but nothing is wrong with the code C: used-before-assignment Issues related to 'used-before-assignment' check 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 Jun 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: used-before-assignment Issues related to 'used-before-assignment' check 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