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

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

Closed
grahamcracker1234 opened this issue Jan 31, 2024 · 2 comments · Fixed by #9655
Closed

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

grahamcracker1234 opened this issue Jan 31, 2024 · 2 comments · Fixed by #9655
Assignees
Labels
C: unsubscriptable-object Issues related to 'unsubscriptable-object' check 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 python 3.12 typing

Comments

@grahamcracker1234
Copy link

grahamcracker1234 commented Jan 31, 2024

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

grahamcracker1234 commented Jan 31, 2024

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.

@jacobtylerwalls
Copy link
Member

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

@jacobtylerwalls jacobtylerwalls changed the title Incorrect linting with type parameter declarations for classes PEP 695 [unsubscriptable-object] FP for generic classes (PEP 695) Feb 3, 2024
@jacobtylerwalls jacobtylerwalls added typing 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 python 3.12 C: unsubscriptable-object Issues related to 'unsubscriptable-object' check and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Feb 3, 2024
@cdce8p cdce8p self-assigned this May 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: unsubscriptable-object Issues related to 'unsubscriptable-object' check 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 python 3.12 typing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants