private static @Nullable PyTypeParameterType findSameTypeParameterInDefinition(@NotNull PyQualifiedNameOwner owner,
                                                                                 @NotNull String name,
                                                                                 @NotNull Context context) {
    // At this moment, the definition of the TypeVar should be the type alias at the top of the stack.
    // While evaluating type hints of enclosing functions' parameters, resolving to the same TypeVar
    // definition shouldn't trigger the protection against recursive aliases, so we manually remove
    // it from the top for the time being.
    PyQualifiedNameOwner typeVarDeclaration = context.getTypeAliasStack().pop();
    assert typeVarDeclaration instanceof PyTargetExpression;
    try {
      if (owner instanceof PyClass) {
        return StreamEx.of(collectTypeParameters((PyClass)owner, context))
          .findFirst(type -> name.equals(type.getName()))
          .orElse(null);
      }
      else if (owner instanceof PyFunction) {
        return StreamEx.of(((PyFunction)owner).getParameterList().getParameters())
          .select(PyNamedParameter.class)
          .map(parameter -> new PyTypingTypeProvider().getParameterType(parameter, (PyFunction)owner, context))
          .map(Ref::deref)
          .map(paramType -> PyTypeChecker.collectGenerics(paramType, context.getTypeContext()))
          .flatMap(generics -> StreamEx.<PyTypeParameterType>of(generics.getTypeVars())
            .append(generics.getParamSpecs())
            .append(generics.getTypeVarTuples())
          )
          .findFirst(type -> name.equals(type.getName()))
          .orElse(null);
      }
    }
    finally {
      context.getTypeAliasStack().push(typeVarDeclaration);
    }
    return null;
  }
private static @Nullable PyTypeParameterType findSameTypeParameterInDefinition(@NotNull PyQualifiedNameOwner owner,
                                                                                 @NotNull String name,
                                                                                 @NotNull Context context)