Skip to content

False positive used-before-assignment when else of try/else calls sys.exit() #7563

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

Closed
kayhayen opened this issue Oct 3, 2022 · 2 comments · Fixed by #7784
Closed

False positive used-before-assignment when else of try/else calls sys.exit() #7563

kayhayen opened this issue Oct 3, 2022 · 2 comments · Fixed by #7784
Labels
C: used-before-assignment Issues related to 'used-before-assignment' check Control flow Requires control flow understanding False Positive 🦟 A message is emitted but nothing is wrong with the code Good first issue Friendly and approachable by new contributors Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Milestone

Comments

@kayhayen
Copy link

kayhayen commented Oct 3, 2022

Bug description

Given this code snippet:

    try:
        yamllint.cli.run([document])
    except SystemExit as e:
        lint_result = e.code
    else:
        sys.exit("Error, yamllint didn't raise expected SystemExit exception.")
    if lint_result != 0:
        sys.exit("Error, no lint clean yaml.")

    my_print("OK.", style="blue")

Since sys.exit() in the else branch is aborting, there is no doubt that lint_result must be assigned.

Configuration

No configuration

Command used

pylint m.py

Pylint output

Warning used-before-assignment for the `lint_result` usage.

Expected behavior

Warning should not be given.

Pylint version

pylint 2.15.3
astroid 2.12.10
Python 3.9.2 (default, Feb 28 2021, 17:03:44)

OS / Environment

Debian Buster with PyPI packages

Additional dependencies

No response

@kayhayen kayhayen added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Oct 3, 2022
@Pierre-Sassoulas
Copy link
Member

The issue look similar to #7520.

@Pierre-Sassoulas Pierre-Sassoulas added Control flow Requires control flow understanding 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 and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Oct 3, 2022
@jacobtylerwalls jacobtylerwalls added the C: used-before-assignment Issues related to 'used-before-assignment' check label Oct 4, 2022
@jacobtylerwalls jacobtylerwalls changed the title False alarm for unused-before-assigment False positive used-before-assignment when else of try/else calls sys.exit() Oct 4, 2022
@jacobtylerwalls
Copy link
Member

Similar issue as #6790, because the patch (#6791) only handled return, not sys.exit.

Should be an easy patch for a contributor to extend #6791. Could consider moving BUILTIN_EXIT_FUNCS to a common constant file and then use that.

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 Control flow Requires control flow understanding False Positive 🦟 A message is emitted but nothing is wrong with the code Good first issue Friendly and approachable by new contributors 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