@ApiStatus.Internal
  @NotNull
  public static <T extends PyAstElement> List<T> collectChildren(@NotNull PyAstFile pyFile, @NotNull Class<T> elementType) {
    final List<T> result = new ArrayList<>();
    pyFile.acceptChildren(new TopLevelVisitor() {
      @Override
      protected void checkAddElement(PsiElement node) {
        if (elementType.isInstance(node)) {
          result.add(elementType.cast(node));
        }
      }

      @Override
      public void visitPyStatement(@NotNull PyAstStatement node) {
        if (PyAstStatement.class.isAssignableFrom(elementType) && !(node instanceof PyAstCompoundStatement)) {
          checkAddElement(node);
          return;
        }
        super.visitPyStatement(node);
      }
    });
    return result;
  }
@ApiStatus.Internal
  @NotNull
  public static <T extends PyAstElement> List<T> collectChildren(@NotNull PyAstFile pyFile, @NotNull Class<T> elementType)