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 negative used-before-assignment when the variable is used again in try/except #9941

Open
octaviancorlade opened this issue Sep 20, 2024 · 1 comment
Labels
C: used-before-assignment Issues related to 'used-before-assignment' check False Negative 🦋 No message is emitted but something is wrong with the code

Comments

@octaviancorlade
Copy link

octaviancorlade commented Sep 20, 2024

Bug description

This is highlighted as expected

import requests

try:
    response = requests.get("https://google.com", timeout=1)
except Exception:
    print(response) # correctly raises used-before-assignment

This is not

import requests

try:
    response = requests.get("https://google.com", timeout=1) 
    response2 = response
except Exception:
    print(response) # should still raise used-before-assignment

Command used

pylint script1.py

Pylint output

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

Expected behavior

************* Module script1
script1.py:8:10: E0601: Using variable 'response' before assignment (used-before-assignment)

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

Pylint version

pylint 3.3.0
astroid 3.3.3
Python 3.12.2 (main, Feb  6 2024, 20:19:44) [Clang 15.0.0 (clang-1500.1.0.2.5)]

Additional dependencies

requests==2.32.3
pylint==3.3.0
@octaviancorlade octaviancorlade added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Sep 20, 2024
@nickdrozd nickdrozd added False Negative 🦋 No message is emitted but something is wrong with the code C: used-before-assignment Issues related to 'used-before-assignment' check and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Sep 21, 2024
@nickdrozd
Copy link
Collaborator

The name consumption logic is a little wonky. If the variable which would be undefined in the except block is used at all in the try block, the warning is not raised.

A self-contained example:

try:
    x = 1 / 0
except ZeroDivisionError:
    print(x)  # triggers used-before-assignment

try:
    y = 1 / 0
    print(y)
except ZeroDivisionError:
    print(y)  # no warning -- false negative

nickdrozd added a commit to nickdrozd/pylint that referenced this issue Sep 28, 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 Negative 🦋 No message is emitted but something is wrong with the code
Projects
None yet
Development

No branches or pull requests

2 participants