void (@NotNull final PyFunction function, @NotNull final PyCodeFragment fragment) { final Set<String> globalWrites = fragment.getGlobalWrites(); final Set<String> newGlobalNames = new LinkedHashSet<>(); final Scope scope = ControlFlowCache.getScope(function); for (String name : globalWrites) { if (!scope.isGlobal(name)) { newGlobalNames.add(name); } } if (!newGlobalNames.isEmpty()) { final PyElementGenerator generator = PyElementGenerator.getInstance(function.getProject()); final PyGlobalStatement globalStatement = generator.createFromText(LanguageLevel.forElement(function), PyGlobalStatement.class, "global " + StringUtil.join(newGlobalNames, ", ")); final PyStatementList statementList = function.getStatementList(); statementList.addBefore(globalStatement, statementList.getFirstChild()); } }