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

possibly-used-before-assignment doesn't understand never-returning-functions #9627

Closed
chrisburr opened this issue May 15, 2024 · 4 comments · Fixed by #9634
Closed

possibly-used-before-assignment doesn't understand never-returning-functions #9627

chrisburr opened this issue May 15, 2024 · 4 comments · Fixed by #9634
Assignees
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
Milestone

Comments

@chrisburr
Copy link

Bug description

# pylint: disable=missing-module-docstring,invalid-name
import sys

if input("Enter something or nothing: "):
    sys.exit()
else:
    a = 1
print(a)

Configuration

No response

Command used

pylint test.py

Pylint output

************* Module test
test.py:8:6: E0606: Possibly using variable 'a' before assignment (possibly-used-before-assignment)

Expected behavior

Branches which end in a function which never returns shouldn't be considered as possible causes of never defined variables.

Pylint version

pylint 3.2.0
astroid 3.2.0
Python 3.11.7 | packaged by conda-forge | (main, Dec 23 2023, 14:43:09) [GCC 12.3.0]

OS / Environment

No response

Additional dependencies

No response

@chrisburr chrisburr added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label May 15, 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 May 15, 2024
@jacobtylerwalls jacobtylerwalls self-assigned this May 15, 2024
@jacobtylerwalls jacobtylerwalls added this to the 3.2.1 milestone May 15, 2024
@nickdrozd
Copy link
Contributor

I don't think is this a false positive. Variable a really is possibly undefined, in the sense that there are branches in which it is used but not defined. For example, if sys.exit is tampered with, a NameError can be raised:

import sys

sys.exit = lambda: None
if input("Enter something or nothing: "):
    sys.exit()
else:
    a = 1
print(a)

There is an easy way to silence this warning, namely to define a unconditionally:

import sys

if input("Enter something or nothing: "):
    sys.exit()

a = 1
print(a)

@jacobtylerwalls
Copy link
Member

I think the proposal is to use the existing option never-returning-functions, which I think is a good idea and simple fix. The risk of tampering is almost nil, and it's under the user's control, if they want to test more rigorously they can supply an empty array to that setting.

@jacobtylerwalls
Copy link
Member

Looks like never-returning-functions is documented as only dealing with one option, and there's already a TODO statement in the code to reuse it in pylint 4 for other purposes, so that's maybe better deferred.

In the meantime, we can solve the reporter's issue with using the existing util is_terminating_func.

@chrisburr
Copy link
Author

Thanks a lot for the quick fix!

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

Successfully merging a pull request may close this issue.

3 participants