Skip to content

An example of a try/except where pylint does not detect UnboundLocalError (used-before-assignment) #5811

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
markmark206 opened this issue Feb 15, 2022 · 1 comment
Labels
Duplicate 🐫 Duplicate of an already existing issue
Milestone

Comments

@markmark206
Copy link

Bug description

This is related to (but not exactly duplicated by) what is described in #4913 and #4045 .

Here is an example where a variable is initialized inside a try block, and is used in the except, before it had a chance to be initialized.

$ cat use_before_assignment.py
def compute() -> int:
    raise ValueError("bad chickens")

def count_chickens():
    try:
        chicken_count = compute()
    except ValueError:
        print(chicken_count)

count_chickens()

if i run this code, i get an error -- as i would expect.

$ python use_before_assignment.py
Traceback (most recent call last):
  File "/Users/markgenba/src/genba/use_before_assignment.py", line 6, in count_chickens
    chicken_count = compute()
  File "/Users/markgenba/src/genba/use_before_assignment.py", line 2, in compute
    raise ValueError("bad chickens")
ValueError: bad chickens

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/markgenba/src/genba/use_before_assignment.py", line 10, in <module>
    count_chickens()
  File "/Users/markgenba/src/genba/use_before_assignment.py", line 8, in count_chickens
    print(chicken_count)
UnboundLocalError: local variable 'chicken_count' referenced before assignment

however, pylint doesn't see a problem with the code:

$ pylint -d missing-module-docstring,missing-function-docstring use_before_assignment.py

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

Configuration

No response

Command used

pylint -d missing-module-docstring,missing-function-docstring use_before_assignment.py

Pylint output

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

Expected behavior

I would expect pylint to report used-before-assignment in the line in the except block where we use the variable that was never initialized:

        print(chicken_count)

Pylint version

pylint 2.12.2
astroid 2.9.3
Python 3.9.9 (v3.9.9:ccb0e6a345, Nov 15 2021, 13:06:05)
[Clang 13.0.0 (clang-1300.0.29.3)]

OS / Environment

Mac OS Monteray 12.2 (21D49)

Additional dependencies

No response

@markmark206 markmark206 added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Feb 15, 2022
@markmark206
Copy link
Author

it looks like this issue has already been fixed: #4045 (comment) closing. thank you!!

@DanielNoord DanielNoord added Duplicate 🐫 Duplicate of an already existing issue and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Feb 15, 2022
@DanielNoord DanielNoord added this to the 2.13.0 milestone Feb 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate 🐫 Duplicate of an already existing issue
Projects
None yet
Development

No branches or pull requests

2 participants