private static @Nullable Ref<PyType> getEnumType(@NotNull PsiElement referenceTarget, @NotNull TypeEvalContext context,
                                                   @Nullable PsiElement anchor) {
    if (referenceTarget instanceof PyTargetExpression target) {
      final ScopeOwner owner = ScopeUtil.getScopeOwner(target);
      if (owner instanceof PyClass cls) {
        final List<PyClassLikeType> types = cls.getAncestorTypes(context);
        for (PyClassLikeType type : types) {
          if (type != null && PyNames.TYPE_ENUM.equals(type.getClassQName())) {
            final PyType classType = context.getType(cls);
            if (classType instanceof PyClassType) {
              return Ref.create(((PyClassType)classType).toInstance());
            }
          }
        }
      }
    }
    if (referenceTarget instanceof PyQualifiedNameOwner qualifiedNameOwner) {
      final String name = qualifiedNameOwner.getQualifiedName();
      if ((PyNames.TYPE_ENUM + ".name").equals(name)) {
        return Ref.create(PyBuiltinCache.getInstance(referenceTarget).getStrType());
      }
      else if ("enum.IntEnum.value".equals(name) && anchor instanceof PyReferenceExpression) {
        return Ref.create(PyBuiltinCache.getInstance(referenceTarget).getIntType());
      }
      else if ((PyNames.TYPE_ENUM + ".value").equals(name) &&
               anchor instanceof PyReferenceExpression anchorExpr && context.maySwitchToAST(anchor)) {
        final PyExpression qualifier = anchorExpr.getQualifier();
        // An enum value is retrieved programmatically, e.g. MyEnum[name].value, or just type-hinted
        if (qualifier != null) {
          PyClassType enumType = as(context.getType(qualifier), PyClassType.class);
          if (enumType != null) {
            PyClass enumClass = enumType.getPyClass();
            PyTargetExpression firstEnumItem = ContainerUtil.getFirstItem(enumClass.getClassAttributes());
            if (firstEnumItem != null && context.maySwitchToAST(firstEnumItem)) {
              final PyExpression value = firstEnumItem.findAssignedValue();
              if (value != null) {
                return Ref.create(context.getType(value));
              }
            }
            else {
              return Ref.create();
            }
          }
        }
      }
      else if ("enum.EnumMeta.__members__".equals(name)) {
        return Ref.create(PyTypeParser.getTypeByName(referenceTarget, "dict[str, unknown]", context));
      }
    }
    @Nullable PyType enumAutoType = getEnumAutoConstructorType(referenceTarget, context, anchor);
    if (enumAutoType != null) {
      return Ref.create(enumAutoType);
    }
    return null;
  }
private static @Nullable Ref<PyType> getEnumType(@NotNull PsiElement referenceTarget, @NotNull TypeEvalContext context,
                                                   @Nullable PsiElement anchor)