Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix false-positive assigning-non-slot with typing.Generic base #4522

Merged
merged 1 commit into from
May 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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