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 after try-except block #5524

Closed
cdce8p opened this issue Dec 13, 2021 · 4 comments · Fixed by #5764
Closed

False-negative used-before-assignment after try-except block #5524

cdce8p opened this issue Dec 13, 2021 · 4 comments · Fixed by #5764
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

@cdce8p
Copy link
Member

cdce8p commented Dec 13, 2021

Followup to #5500 (comment)

def func(var):
    try:
        return 1 / var.some_other_func()
    except AttributeError:
        pass
    except ZeroDivisionError:
        msg = "Devision by 0"

    print(msg)  # should emit 'used-before-assignment'

The code above should raise a used-before-assignment error since msg is not assigned in the AttributeError ExceptHandler.

@cdce8p cdce8p 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 labels Dec 13, 2021
@jacobtylerwalls
Copy link
Member

jacobtylerwalls commented Jan 3, 2022

Extending this example, if the try block actually defines msg instead of returning, since there is not exhaustive error handling (by assigning the name, or raising or exiting), then we could emit a message that the use of the name needs to be guarded by else. Maybe that's also used-before-assignment, or maybe it's not. 🤔

Maybe consider-using-try-else (or if that's too weasel-worded, maybe nonexhaustive-error-handling?)

try:
    a = 1 / n
except LibraryError:
    pass
print(a)  # [nonexhaustive-error-handling]
try:
    a = 1 / n
except LibraryError:
    pass
else:
    print(a)  # no message

From discussion in #5582 (comment).

@jacobtylerwalls
Copy link
Member

We can close #2835 as a duplicate.

@cdce8p
Copy link
Member Author

cdce8p commented Jan 30, 2022

Another idea. I like how pyright names it. Maybe possibly-unbound?

"a" is possibly unbound

Maybe consider-using-try-else

We shouldn't really "force" users to add an else case. There are just to many alternatives. You could for example assign a default value before you enter the try block.

@DanielNoord
Copy link
Collaborator

DanielNoord commented Feb 8, 2022

Some of the discussion in this issue is being moved to #2835. The rest is being closed with #5764.

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
3 participants