pylint-dev / pylint

It's not just a linter that annoys you!
https://pylint.readthedocs.io/en/latest/
GNU General Public License v2.0
5.34k stars 1.14k forks source link

False positives around PEP 695 generics #10091

Open jesnie opened 1 day ago

jesnie commented 1 day ago

Bug description

I have a medium-sized code-base I've been updating to PEP 695 typing, and I'm getting a lot of false positives.

Some minimal examples:

class A[T]:

    def __init__(self, x: T) -> None:
        self._x = x

Errors with:

test_pylint_695_1.py:3:26: E0602: Undefined variable 'T' (undefined-variable)

But:

type L[T] = list[T]

class A[T]:

    def __init__(self, x: T) -> None:
        self._x = x

Passes.

Also:

def ident[T](x:T) -> T:
    return x

Passes

but:

type L[T] = list[T]

def ident[T](x:T) -> T:
    return x

Fails with:

test_pylint_695_3.py:3:10: W0621: Redefining name 'T' from outer scope (line 1) (redefined-outer-name)

I suspect:

  1. You're not picking up that class A[T] is defining the variable T.
  2. You're not correctly scoping the T in type L[T].

Configuration

Command used

mypy test_pylint_695* && pylint test_pylint_695*

Pylint output

************* Module test_pylint_695_1
test_pylint_695_1.py:3:26: E0602: Undefined variable 'T' (undefined-variable)
************* Module test_pylint_695_3
test_pylint_695_3.py:3:10: W0621: Redefining name 'T' from outer scope (line 1) (redefined-outer-name)

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

Expected behavior

All of these examples should pass.

Pylint version

pylint==3.3.1
astroid==3.3.5
Python 3.12.3

OS / Environment

Ubuntu 22.04.3 LTS (running under WSL)

Additional dependencies

jesnie commented 1 day ago

Oh, here's another one:

type S = str
print(S.__value__)

errors with:

test_pylint_695.py:2:6: E1101: Class 'str' has no '__value__' member (no-member)

S not of type str, but of type TypeAliasType and does have a __value__.