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

Not seeing E0601 where in a pretty obvious code path, where code execution does show UnboundLocalError #9305

Closed
sarvi opened this issue Dec 12, 2023 · 1 comment
Labels
C: used-before-assignment Issues related to 'used-before-assignment' check Duplicate 🐫 Duplicate of an already existing issue

Comments

@sarvi
Copy link

sarvi commented Dec 12, 2023

Bug description

pylint doesnt generate a warning for this code. 
(xrg-382) bash-4.4$ pylint test.py

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

(xrg-382) bash-4.4$ 
If the "result" is being accessed inside the except: block I see the following

(xrg-382) bash-4.4$ cat test.py
'''something'''

def something():
    '''something'''
    return 1/0

def main():
    '''something'''
    try:
        result = something()
    except ZeroDivisionError:
        print('Hello Exception')
    print("Return code is.... ")
    print(result)

if __name__ == '__main__':  # pragma: no cover
    main()
(xrg-382) bash-4.4$ 

but for this code it does

(xrg-382) bash-4.4$ pylint test.py
************* Module test
test.py:13:14: E0601: Using variable 'result' before assignment (used-before-assignment)

-------------------------------------------------------------------
Your code has been rated at 5.83/10 (previous run: 10.00/10, -4.17)

(xrg-382) bash-4.4$ 
(xrg-382) bash-4.4$ cat test.py 
'''something'''

def something():
    '''something'''
    return 1/0

def main():
    '''something'''
    try:
        result = something()
    except ZeroDivisionError:
        print('Hello Exception')
        print(result)
    print("Return code is.... ")
    print(result)

if __name__ == '__main__':  # pragma: no cover
    main()
(xrg-382) bash-4.4$

Configuration

# default

Command used

pylint test.py

Pylint output

(xrg-382) bash-4.4$ pylint test.py

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

(xrg-382) bash-4.4$

Expected behavior

I was expecting to see the following Error
test.py:13:14: E0601: Using variable 'result' before assignment (used-before-assignment)
I was expecting to see this error for the print(result) outside the except block.
(xrg-382) bash-4.4$ pylint test.py
************* Module test
test.py:13:14: E0601: Using variable 'result' before assignment (used-before-assignment)


Your code has been rated at 5.83/10 (previous run: 10.00/10, -4.17)

Pylint version

(xrg-382) bash-4.4$ pylint --version
pylint 3.0.3
astroid 3.0.2
Python 3.8.2 (default, May 20 2020, 22:15:44) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
(xrg-382) bash-4.4$

OS / Environment

(xrg-382) bash-4.4$ uname -a
Linux sjc-ads-5995 4.18.0-305.7.1.el8_4.x86_64 #1 SMP Mon Jun 14 17:25:42 EDT 2021 x86_64 x86_64 x86_64 GNU/Linux

Additional dependencies

No response

@sarvi sarvi added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Dec 12, 2023
@jacobtylerwalls
Copy link
Member

jacobtylerwalls commented Dec 12, 2023

Thanks, this is a known missing feature. I was the one that implemented the first round of try/except checking at all, and the first baby step was to at least complain for assignments only made in the except handlers. #2835 is for variables only defined in the happy path. Some users might find this controversial, so we're considering adding a different flavor of message for it.

Duplicate of #2835

@jacobtylerwalls jacobtylerwalls closed this as not planned Won't fix, can't repro, duplicate, stale Dec 12, 2023
@jacobtylerwalls jacobtylerwalls added Duplicate 🐫 Duplicate of an already existing issue C: used-before-assignment Issues related to 'used-before-assignment' check and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Dec 12, 2023
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 Duplicate 🐫 Duplicate of an already existing issue
Projects
None yet
Development

No branches or pull requests

2 participants