Skip to content

Commit

Permalink
Fix false-positive assigning-non-slot with typing.Generic base (#4522)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p authored May 30, 2021
1 parent a5be476 commit 9d1acfc
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 0 deletions.
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ modules are added.

Closes PyCQA/astroid#942

* Fix ``assigning-non-slot`` false-positive with base that inherits from ``typing.Generic``

Closes #4509
Closes PyCQA/astroid#999


What's New in Pylint 2.8.2?
===========================
Expand Down
7 changes: 7 additions & 0 deletions pylint/checkers/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1417,6 +1417,13 @@ def _check_in_slots(self, node):
if "__slots__" not in klass.locals or not klass.newstyle:
return

# If 'typing.Generic' is a base of bases of klass, the cached version
# of 'slots()' might have been evaluated incorrectly, thus deleted cache entry.
if any(base.qname() == "typing.Generic" for base in klass.mro()):
cache = getattr(klass, "__cache", None)
if cache and cache.get(klass.slots) is not None:
del cache[klass.slots]

slots = klass.slots()
if slots is None:
return
Expand Down
18 changes: 18 additions & 0 deletions tests/functional/a/assign/assigning_non_slot_4509.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# pylint: disable=invalid-name,missing-docstring,too-few-public-methods

# Slots with base that inherits from 'Generic'
# https://github.com/PyCQA/pylint/issues/4509
# https://github.com/PyCQA/astroid/issues/999

from typing import Generic, TypeVar
T = TypeVar("T")

class Base(Generic[T]):
__slots__ = ()

class Foo(Base[T]):
__slots__ = ['_value']

def __init__(self, value: T):
self._value = value
self._bar = value # [assigning-non-slot]
2 changes: 2 additions & 0 deletions tests/functional/a/assign/assigning_non_slot_4509.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[testoptions]
min_pyver=3.7
1 change: 1 addition & 0 deletions tests/functional/a/assign/assigning_non_slot_4509.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
assigning-non-slot:18:8:Foo.__init__:Assigning to attribute '_bar' not defined in class slots

0 comments on commit 9d1acfc

Please # to comment.