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

undefined-variable or used-before-assignment should be reported when variable is declared with a type annotation but not assigned to before use #5140

Closed
waleedmebane opened this issue Oct 10, 2021 · 0 comments · Fixed by #5158
Assignees
Labels
False Negative 🦋 No message is emitted but something is wrong with the code
Milestone

Comments

@waleedmebane
Copy link

Bug description

x: int
print(x)

Pylint does not report an error for the code above.

This code:

# x: int
print(x)

produces:

uninitialized_variable_test.py:2:6: E0602: Undefined variable 'x' (undefined-variable)

This code:

x
print(x)

produces:

uninitialized_variable_test.py:1:0: W0104: Statement seems to have no effect (pointless-statement)
uninitialized_variable_test.py:1:0: E0602: Undefined variable 'x' (undefined-variable)
uninitialized_variable_test.py:2:6: E0602: Undefined variable 'x' (undefined-variable)

This code:

x: int
print(x)
x = 0

produces no error.

This code:

# x: int
print(x)
x = 0

produces:
uninitialized_variable_test.py:2:6: E0601: Using variable 'x' before assignment (used-before-assignment)

Python produces a runtime error for all three cases:
NameError: name 'x' is not defined

(FYI, Mypy and pytype also do not catch this error.)



### Configuration

_No response_

### Command used

```shell
pylint uninitialized_variable_test.py

Pylint output

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

Expected behavior

Should get an undefined variable error in the same cases in which one would get the error without the variable having been declared but not assigned, OR it should produce a used-before-assignment error.

Pylint version

pylint 2.11.1
astroid 2.8.2
Python 3.9.5 (default, Jul 19 2021, 01:21:38) 
[GCC 5.4.0 20160609]

OS / Environment

Ubuntu 16.04

Additional dependencies

No response

@waleedmebane waleedmebane added Bug 🪲 Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Oct 10, 2021
@DanielNoord DanielNoord added False Negative 🦋 No message is emitted but something is wrong with the code and removed Bug 🪲 Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Oct 15, 2021
@DanielNoord DanielNoord added this to the 2.12.0 milestone Oct 15, 2021
@DanielNoord DanielNoord self-assigned this Oct 15, 2021
DanielNoord added a commit to DanielNoord/pylint that referenced this issue Oct 15, 2021
DanielNoord added a commit to DanielNoord/pylint that referenced this issue Oct 15, 2021
DanielNoord added a commit that referenced this issue Oct 15, 2021
…ment (#5158)

Closes #5140

Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

2 participants