pylint icon indicating copy to clipboard operation
pylint copied to clipboard

Spurious 'undefined-variable' warning with postponed annotations enabled

Open SamyCookie opened this issue 1 year ago • 6 comments

Bug description

Hello,

The following raise an unexpected warning.

from __future__ import annotations
class A:
    class B:
        pass
    class C:
        b: B  # undefined-variable

In Python 3.9, Pylint should not raise the undefined-variable warning on B if __future__.annotations has been imported.

Have a good day !

Configuration

No response

Command used

no specific parameters

Pylint output

E0602: Undefined variable 'B' (undefined-variable)

Expected behavior

No warning

Pylint version

pylint 2.16.0
astroid 2.14.1
Python 3.9.2 (default, Feb 28 2021, 17:03:44)

OS / Environment

Debian bullseye 64bit

Additional dependencies

No response

SamyCookie avatar Feb 07 '23 16:02 SamyCookie

@Pierre-Sassoulas were you able to reproduce? I tried with Python 3.9.2 and same pylint & astroid versions reported and could not.

jacobtylerwalls avatar Mar 10 '23 02:03 jacobtylerwalls

Right sorry, I cannot reproduce on pylint main branch, could you upgrade to 2.17.0 @SamyCookie ?

Pierre-Sassoulas avatar Mar 10 '23 22:03 Pierre-Sassoulas

I have reproduced the issue with up to date PyLint version

pylint 2.17.0
astroid 2.15.0
Python 3.9.2 (default, Feb 28 2021, 17:03:44) 
[GCC 10.2.1 20210110]

************* Module foo
foo.py:6:11: E0602: Undefined variable 'B' (undefined-variable)

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

*** Error code 18

Stop.

Maybe you have forgotten to remove the disable comment I had put in my example ?

SamyCookie avatar Mar 13 '23 11:03 SamyCookie

I've also reproduced this issue with the following environment:

pylint 3.0.2
astroid 3.0.1
Python 3.9.2 (default, Feb 28 2021, 17:03:44) 
[GCC 10.2.1 20210110]

I can provide the actual example case if needed, but the issue seems fairly clear. I'm using the same from __future__ import annotations import, and getting the same undefined-variable message.

(as a side note, I have access to an environment with up to Python 3.11 installed; if desired, I can test the same scenario on that environment and report back. Not sure if that would actually be helpful, though.)

Edit: this got marked as spam for some reason? Not sure why. I promise I'm a person.

mishaturnbull avatar Dec 10 '23 22:12 mishaturnbull

I've also reproduced this issue with the following environment:

pylint 3.0.2
astroid 3.0.1
Python 3.9.2 (default, Feb 28 2021, 17:03:44) 
[GCC 10.2.1 20210110]

I can provide the actual example case if needed, but the issue seems fairly clear. I'm using the same from __future__ import annotations import, and getting the same undefined-variable message.

(as a side note, I have access to an environment with up to Python 3.11 installed; if desired, I can test the same scenario on that environment and report back. Not sure if that would actually be helpful, though.)

I also encountered the same issue pylint: 3.0.3 astroid 3.0.2 python 3.11

luras-oil avatar Jan 29 '24 09:01 luras-oil

I've also reproduced this issue with the following environment:

pylint 3.0.2
astroid 3.0.1
Python 3.9.2 (default, Feb 28 2021, 17:03:44) 
[GCC 10.2.1 20210110]

I can provide the actual example case if needed, but the issue seems fairly clear. I'm using the same from __future__ import annotations import, and getting the same undefined-variable message. (as a side note, I have access to an environment with up to Python 3.11 installed; if desired, I can test the same scenario on that environment and report back. Not sure if that would actually be helpful, though.)

I also encountered the same issue pylint: 3.0.3 astroid 3.0.2 python 3.11

I find my case is because of the type is changed from "List" to "list".

luras-oil avatar Feb 01 '24 08:02 luras-oil