@NotNull
  private static PyFunction createInitMethod(Project project, PyClass cls, @Nullable PyFunction ancestorInit) {
    // found it; copy its param list and make a call to it.
    @NonNls String paramList = ancestorInit != null ? ancestorInit.getParameterList().getText() : "(self)";

    @NonNls String functionText = "def " + PyNames.INIT + paramList + ":\n";
    if (ancestorInit == null) {
      functionText += "    pass";
    }
    else {
      final PyClass ancestorClass = ancestorInit.getContainingClass();
      if (ancestorClass != null && !PyUtil.isObjectClass(ancestorClass)) {
        @NonNls StringBuilder sb = new StringBuilder();
        PyParameter[] params = ancestorInit.getParameterList().getParameters();

        boolean seen = false;
        if (cls.isNewStyleClass(null)) {
          // form the super() call
          sb.append("super(");
          if (!LanguageLevel.forElement(cls).isPy3K()) {
            sb.append(cls.getName());

            // NOTE: assume that we have at least the first param
            String self_name = params[0].getName();
            sb.append(", ").append(self_name);
          }
          sb.append(").").append(PyNames.INIT).append("(");
        }
        else {
          sb.append(ancestorClass.getName());
          sb.append(".__init__(self");
          seen = true;
        }
        for (int i = 1; i < params.length; i += 1) {
          if (seen) {
            sb.append(", ");
          }
          else {
            seen = true;
          }
          sb.append(params[i].getName());
        }
        sb.append(")");
        functionText += "    " + sb;
      }
      else {
        functionText += "    pass";
      }
    }
    return PyElementGenerator.getInstance(project).createFromText(
      LanguageLevel.getDefault(), PyFunction.class, functionText,
      new int[]{0}
    );
  }
@NotNull
  private static PyFunction createInitMethod(Project project, PyClass cls, @Nullable PyFunction ancestorInit)