void (@NotNull final Project project, @NotNull final PyClass clazz, @Nullable final Collection<String> paramExpressions, @Nullable final Collection<Pair<String, String>> keywordArguments) { final PyElementGenerator generator = PyElementGenerator.getInstance(project); final LanguageLevel languageLevel = LanguageLevel.forElement(clazz); PyArgumentList superClassExpressionList = clazz.getSuperClassExpressionList(); boolean addExpression = false; if (superClassExpressionList == null) { superClassExpressionList = generator.createFromText(languageLevel, PyClass.class, "class foo():pass").getSuperClassExpressionList(); assert superClassExpressionList != null : "expression not created"; addExpression = true; } generator.createFromText(LanguageLevel.PYTHON34, PyClass.class, "class foo(object, metaclass=Foo): pass").getSuperClassExpressionList(); if (paramExpressions != null) { for (final String paramExpression : paramExpressions) { superClassExpressionList.addArgument(generator.createParameter(paramExpression)); } } if (keywordArguments != null) { for (final Pair<String, String> keywordArgument : keywordArguments) { superClassExpressionList.addArgument(generator.createKeywordArgument(languageLevel, keywordArgument.first, keywordArgument.second)); } } // If class has no expression list, then we need to add it manually. if (addExpression) { final ASTNode classNameNode = clazz.getNameNode(); // For nameless classes we simply add expression list directly to them final PsiElement elementToAddAfter = (classNameNode == null) ? clazz.getFirstChild() : classNameNode.getPsi(); clazz.addAfter(superClassExpressionList, elementToAddAfter); } }