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

False positives around PEP 695 generics #10091

Open
jesnie opened this issue Nov 22, 2024 · 1 comment
Open

False positives around PEP 695 generics #10091

jesnie opened this issue Nov 22, 2024 · 1 comment
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation

Comments

@jesnie
Copy link

jesnie commented Nov 22, 2024

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 jesnie added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Nov 22, 2024
@jesnie
Copy link
Author

jesnie commented Nov 22, 2024

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__.

@Pierre-Sassoulas Pierre-Sassoulas added False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Nov 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Projects
None yet
Development

No branches or pull requests

2 participants