void (@Nullable PyType type, @NotNull PyCallable callable, @NotNull TypeEvalContext context) { if (!(callable instanceof PyFunction pyFunction)) return; PyTypeChecker.Generics typeParameters = PyTypeChecker.collectGenerics(type, context); if (typeParameters.isEmpty()) return; PyClass pyClass = pyFunction.getContainingClass(); Function<PyGenericType, PyQualifiedNameOwner> findScopeOwner = typeVar -> pyFunction; if (PyUtil.isInitOrNewMethod(callable)) { findScopeOwner = typeVar -> pyClass; } else if (pyClass != null) { PyType classGenericType = getGenericType(pyClass, context); if (classGenericType != null) { PyTypeChecker.Generics classTypeParameters = PyTypeChecker.collectGenerics(classGenericType, context); Set<String> classTypeVarNames = ContainerUtil.map2Set(classTypeParameters.getTypeVars(), PyGenericType::getName); findScopeOwner = typeVar -> classTypeVarNames.contains(typeVar.getName()) ? pyClass : pyFunction; } } for (PyTypeParameterType typeParam : typeParameters.getAllTypeParameters()) { if (typeParam instanceof PyTypeVarTypeImpl typeVar) { typeVar.setScopeOwner(findScopeOwner.apply(typeVar)); } } }