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

Yet another false positive E0601 used-before-assignment, now for type hints #8110

Closed
zEdS15B3GCwq opened this issue Jan 25, 2023 · 3 comments
Labels
C: used-before-assignment Issues related to 'used-before-assignment' check Question

Comments

@zEdS15B3GCwq
Copy link

Bug description

Issue

Code:

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from numpy import ndarray

a: ndarray # E0601 here
b=ndarray()

Note that only the type hint was flagged by the error, the assignment was not.

Related to

#7574 (may be fixed by #8071)
#7979 (fixed in #7980)
#7368 (fixed by #7360)
However, none of these seem to cover type hints.

Configuration

No response

Command used

pylint --disable=missing-module-docstring .\pylint_test.py

Pylint output

************* Module pylint_test
pylint_test.py:6:3: E0601: Using variable 'ndarray' before assignment (used-before-assignment)

Expected behavior

No issues for the type hint.

Pylint version

pylint 2.15.10
astroid 2.13.3
Python 3.11.1 (tags/v3.11.1:a7a450f, Dec  6 2022, 19:58:39) [MSC v.1934 64 bit (AMD64)]

OS / Environment

Windows 11 x64 21H2

Additional dependencies

No response

@zEdS15B3GCwq zEdS15B3GCwq added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Jan 25, 2023
@nickdrozd nickdrozd added C: used-before-assignment Issues related to 'used-before-assignment' check False Positive 🦟 A message is emitted but nothing is wrong with the code and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Jan 25, 2023
@jacobtylerwalls
Copy link
Member

Isn't this expected? This raises NameError:

$ python3 a.py
Traceback (most recent call last):
  File "/Users/.../a.py", line 6, in <module>
    a: ndarray # E0601 here
NameError: name 'ndarray' is not defined

@jacobtylerwalls jacobtylerwalls added Question and removed False Positive 🦟 A message is emitted but nothing is wrong with the code labels Jan 26, 2023
@zEdS15B3GCwq
Copy link
Author

Eh, you're right. I'm sorry, should've tested that piece of code before posting. Following your response, I tried to write an example that was error-free but still produced E0601, but found that 2.15.10 was indeed working correctly and not reporting E0601.

Thank you for taking the time to reply and sorry for your trouble.

@nhairs
Copy link

nhairs commented Jan 18, 2024

So in case others end up here:

The reason that the snippet is throwing an error is because it uses PEP 563 – Postponed Evaluation of Annotations (3.7+) which requires from __future__ import annotations.

Applying this import will fix the runtime and pylint errors.

(This may change in 3.13 with PEP 649 – Deferred Evaluation Of Annotations Using Descriptors)

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 Question
Projects
None yet
Development

No branches or pull requests

4 participants