diff --git a/astroid/brain/brain_typing.py b/astroid/brain/brain_typing.py index b82699c248..e1ce3ee5cf 100644 --- a/astroid/brain/brain_typing.py +++ b/astroid/brain/brain_typing.py @@ -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])) diff --git a/tests/unittest_brain.py b/tests/unittest_brain.py index f7ca2bf77c..a217f223f7 100644 --- a/tests/unittest_brain.py +++ b/tests/unittest_brain.py @@ -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( """