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.32k stars 1.14k forks source link

False positive `declare-non-slot` on classvar #9950

Open mthuurne opened 1 month ago

mthuurne commented 1 month ago

Bug description

I'm checking code that simplifies to this testcase:

from typing import ClassVar

class C:
    __slots__ = ("value",)

    def __init__(self, value: int):
        self.value = value

    x: ClassVar[int]
    y: ClassVar[int] = 2
    z = 3

For some reason, pylint considers x to be a declaration that conflicts with __slots__, but not y and z.

Curiously, this error flagged for x goes away if __slots__ is empty.

Configuration

No response

Command used

pylint non_slot_classvar.py

Pylint output

************* Module non_slot_classvar
non_slot_classvar.py:10:4: E0245: No such name 'x' in __slots__ (declare-non-slot)

Expected behavior

No error should be flagged.

At runtime, there is no error, as __slots__ only applies to instances of C, not to the class.

Pylint version

pylint 3.3.0
astroid 3.3.3
Python 3.11.9 (main, Apr 08 2024, 06:18:15) [GCC]

OS / Environment

Linux (openSUSE Tumbleweed)

Additional dependencies

No response

mthuurne commented 1 month ago

This problem is new since pylint 3.3.0, which makes sense as E0245 was added recently, in #9564.

jacobtylerwalls commented 1 month ago

cc/ @adamtuft, could you lend us your view of this bug report? Thanks!

adamtuft commented 1 month ago

Thanks for tagging me, I'll have a look at this 🔍