pylint icon indicating copy to clipboard operation
pylint copied to clipboard

[unsubscriptable-object] FP for generic classes (PEP 695)

Open grahamcracker1234 opened this issue 1 year ago • 2 comments

Bug description

The behavior described in #9193 is still observable with pylint 3.0.3 for classes.

class Foo[T]:
    def __init__(self, value: T):
        self.value = value

x = Foo[int](1)

Works fine but T is marked as an undefined variable, and Foo is marked unsubscriptable.

Configuration

No response

Command used

pylint foo.py

Pylint output

************* Module foo
foo.py:1:0: C0114: Missing module docstring (missing-module-docstring)
foo.py:1:0: C0104: Disallowed name "foo" (disallowed-name)
foo.py:1:0: C0115: Missing class docstring (missing-class-docstring)
foo.py:2:30: E0602: Undefined variable 'T' (undefined-variable)
foo.py:1:0: R0903: Too few public methods (0/2) (too-few-public-methods)
foo.py:6:4: E1136: Value 'Foo' is unsubscriptable (unsubscriptable-object)

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

Expected behavior

There should be no E0602 or E1136.

Pylint version

pylint 3.0.3
astroid 3.0.2
Python 3.12.0 (main, Dec  4 2023, 20:32:31) [Clang 15.0.0 (clang-1500.0.40.1)]

OS / Environment

macOS 14.2.1 / Apple M2 Max

Additional dependencies

No response

grahamcracker1234 avatar Jan 31 '24 22:01 grahamcracker1234

Including

from __future__ import annotations

fixes the E0602 linting.

Using

from typing import Generic, TypeVar

T = TypeVar("T")

class Foo(Generic[T]):
    def __init__(self, value: T):
        self.value = value

x = Foo[int](1)

instead of the new syntax of PEP 695, removes the E1136 linting.

grahamcracker1234 avatar Jan 31 '24 22:01 grahamcracker1234

Thanks for the report. The undefined-variable is reported in #9335, so I'll refocus this report on the unsubscriptable-object.

jacobtylerwalls avatar Feb 03 '24 16:02 jacobtylerwalls