Skip to content

Commit

Permalink
Fix issue with slots caching
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p committed Apr 7, 2021
1 parent 05022b1 commit 6c355ca
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
10 changes: 10 additions & 0 deletions astroid/brain/brain_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,16 @@ def infer_typing_attr(
# infer the native methods, replace them for an easy inference tip
func_to_add = astroid.extract_node(CLASS_GETITEM_TEMPLATE)
value.locals["__class_getitem__"] = [func_to_add]
if (
isinstance(node.parent, nodes.ClassDef)
and node in node.parent.bases
and getattr(node.parent, "__cache", None)
):
# node.parent.slots is evaluated and cached before the inference tip
# is first applied. Remove the last result to allow a recalculation of slots
cache = getattr(node.parent, "__cache")
if cache and cache.get(node.parent.slots) is not None:
del cache[node.parent.slots]
return iter([value])

node = extract_node(TYPING_TYPE_TEMPLATE.format(value.qname().split(".")[-1]))
Expand Down
23 changes: 23 additions & 0 deletions tests/unittest_brain.py
Original file line number Diff line number Diff line change
Expand Up @@ -1388,6 +1388,29 @@ def test_typing_annotated_subscriptable(self):
assert isinstance(inferred, nodes.ClassDef)
assert isinstance(inferred.getattr("__class_getitem__")[0], nodes.FunctionDef)

@test_utils.require_version(minver="3.7")
def test_typing_generic_slots(self):
"""Test cache reset for slots if Generic subscript is inferred."""
node = builder.extract_node(
"""
from typing import Generic, TypeVar
T = TypeVar('T')
class A(Generic[T]):
__slots__ = ['value']
def __init__(self, value):
self.value = value
"""
)
inferred = next(node.infer())
assert len(inferred.slots()) == 0
# Only after the subscript base is inferred and the inference tip applied,
# will slots contain the correct value
next(node.bases[0].infer())
slots = inferred.slots()
assert len(slots) == 1
assert isinstance(slots[0], nodes.Const)
assert slots[0].value == "value"

def test_has_dunder_args(self):
ast_node = builder.extract_node(
"""
Expand Down

0 comments on commit 6c355ca

Please sign in to comment.