package com.jetbrains.python.debugger;

import com.intellij.psi.PsiElement;
import com.jetbrains.python.psi.PyClass;
import com.jetbrains.python.psi.PyFunction;
import com.jetbrains.python.psi.types.PyClassType;
import com.jetbrains.python.psi.types.PyType;
import com.jetbrains.python.psi.types.PyTypeParser;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * @author traff
 */
public class PySignatureUtil {
  private PySignatureUtil() {
  }

  @Nullable
  public static String getShortestImportableName(@Nullable PsiElement anchor, @NotNull String type) {
    final PyType pyType = PyTypeParser.getTypeByName(anchor, type);
    if (pyType instanceof PyClassType) {
      PyClass c = ((PyClassType)pyType).getPyClass();
      return c.getQualifiedName();
    }

    if (pyType != null) {
      return pyType.getName();
    }
    else {
      return type;
    }
  }

  @Nullable
  public static String getArgumentType(@NotNull PyFunction function, @NotNull String name) {
    PySignatureCacheManager cacheManager = PySignatureCacheManager.getInstance(function.getProject());
    PySignature signature = cacheManager.findSignature(function);
    if (signature != null) {
      return getShortestImportableName(function, signature.getArgTypeQualifiedName(name));
    }
    return null;
  }
}

@@ -48,31 +48,217 @@
 com.
-intellij.psi.PsiElement
+google.common.base.Function;%0Aimport com.google.common.collect.Collections2;%0Aimport com.intellij.openapi.util.text.StringUtil;%0Aimport com.intellij.psi.PsiElement;%0Aimport com.jetbrains.appengine.util.StringUtils
 ;%0Aim
@@ -489,16 +489,67 @@
 Parser;%0A
+import com.jetbrains.python.psi.types.PyUnionType;%0A
 import o
@@ -1079,23 +1079,435 @@
 urn 
-pyType.getName(
+getPrintableName(pyType);%0A    %7D%0A    else %7B%0A      return type;%0A    %7D%0A  %7D%0A%0A  private static String getPrintableName(PyType type) %7B%0A    if (type instanceof PyUnionType) %7B%0A      return StringUtil.join(Collections2.transform(((PyUnionType)type).getMembers(), new Function%3CPyType, String%3E() %7B%0A        @Override%0A        public String apply(@Nullable PyType input) %7B%0A          return getPrintableName(input);%0A        %7D%0A      %7D), %22 or %22
 );%0A 
@@ -1531,32 +1531,42 @@
      return type
+.getName()
 ;%0A    %7D%0A  %7D%0A%0A  @

Generate correct string for union type (PY-8896).