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

used-before-assignment not flagged for import while triggering UnboundLocalError #4624

Closed
chaen opened this issue Jun 29, 2021 · 1 comment · Fixed by #6979
Closed

used-before-assignment not flagged for import while triggering UnboundLocalError #4624

chaen opened this issue Jun 29, 2021 · 1 comment · Fixed by #6979
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
Milestone

Comments

@chaen
Copy link

chaen commented Jun 29, 2021

Steps to reproduce

importExample.py

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

def test():
    print(time.time())
    import time
    print(time.time())

test()

variableExample.py

# pylint: disable=missing-docstring,invalid-name
A = 1

def test():
    print(A)
    A = 2
    print(A)

test()

Current behavior

The execution of these two scripts lead to a similar outcome:

$ python importExample.py 
Traceback (most recent call last):
  File "importExample.py", line 9, in <module>
    test()
  File "importExample.py", line 5, in test
    print(time.time())
UnboundLocalError: local variable 'time' referenced before assignment
$ python variableExample.py 
Traceback (most recent call last):
  File "variableExample.py", line 9, in <module>
    test()
  File "variableExample.py", line 5, in test
    print(A)
UnboundLocalError: local variable 'A' referenced before assignment

pylint detects the problem correctly for the variable, but not for the import

$ pylint variableExample.py 
************* Module variableExample
variableExample.py:6:4: W0621: Redefining name 'A' from outer scope (line 2) (redefined-outer-name)
variableExample.py:5:10: E0601: Using variable 'A' before assignment (used-before-assignment)

-----------------------------------
Your code has been rated at 0.00/10
$ pylint importExample.py 
************* Module importExample
importExample.py:6:4: W0621: Redefining name 'time' from outer scope (line 2) (redefined-outer-name)
importExample.py:6:4: W0404: Reimport 'time' (imported line 2) (reimported)
importExample.py:6:4: C0415: Import outside toplevel (time) (import-outside-toplevel)
importExample.py:2:0: W0611: Unused import time (unused-import)

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

Expected behavior

I'd expect to see E0601 raised in the case of the import as well.

pylint --version output

$ pylint --version
pylint 3.0.0a3
astroid 2.6.0
Python 3.9.5 | packaged by conda-forge | (default, Jun 19 2021, 00:32:32) 
[GCC 9.3.0]
@jacobtylerwalls
Copy link
Member

#6812 reports the same issue for nested functions.

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

Successfully merging a pull request may close this issue.

3 participants