diff --git a/R/centrality.R b/R/centrality.R index 1a21d8597f..fc7c4e8888 100644 --- a/R/centrality.R +++ b/R/centrality.R @@ -26,6 +26,8 @@ subgraph.centrality <- function(graph, diag = FALSE) { #' `page.rank()` was renamed to [page_rank()] to create a more #' consistent API. #' @inheritParams page_rank +#' @param algo `r lifecycle::badge("deprecated")` Use `algorithm` in +#' [page_rank()] instead. #' @keywords internal #' @export page.rank <- function( @@ -42,7 +44,7 @@ page.rank <- function( lifecycle::deprecate_warn("2.0.0", "page.rank()", "page_rank()") page_rank( graph = graph, - algo = algo, + algorithm = algo, vids = vids, directed = directed, damping = damping, @@ -1914,7 +1916,7 @@ hub_score <- function( #' #' @param graph The graph object. #' @inheritParams rlang::args_dots_empty -#' @param algo Character scalar, which implementation to use to carry out the +#' @param algorithm Character scalar, which implementation to use to carry out the #' calculation. The default is `"prpack"`, which uses the PRPACK library #' () to calculate PageRank scores #' by solving a set of linear equations. This is a new implementation in igraph @@ -1983,7 +1985,7 @@ hub_score <- function( page_rank <- function( graph, ..., - algo = c("prpack", "arpack"), + algorithm = c("prpack", "arpack"), vids = NULL, directed = TRUE, damping = 0.85, @@ -2005,7 +2007,7 @@ page_rank <- function( cli::cli_abort(base::c("Unexpected argument passed to {.fn page_rank}: {.arg {(.arg_extra)}}.", i = "Arguments after {.arg ...} must be spelled out in full."), call = base::parent.frame()) } base::list(env = base::environment(), recovered = base::c( - if (!base::missing(algo)) base::c(algo = "algo"), + if (!base::missing(algo)) base::c(algorithm = "algo"), if (!base::missing(vids)) base::c(vids = "vids"), if (!base::missing(directed)) base::c(directed = "directed"), if (!base::missing(damping)) base::c(damping = "damping"), @@ -2015,7 +2017,6 @@ page_rank <- function( )) } .arg_given <- base::c( - if (!base::missing(algo)) "algo", if (!base::missing(vids)) "vids", if (!base::missing(directed)) "directed", if (!base::missing(damping)) "damping", @@ -2027,6 +2028,10 @@ page_rank <- function( .arg_recovered <- .arg_handle$recovered[!base::is.element(.arg_handle$recovered, .arg_given)] if (base::length(.arg_recovered) > 0L) { .arg_names <- base::names(.arg_recovered) + .arg_conflict <- base::intersect(.arg_names, base::c( + if (!base::missing(algorithm)) "algorithm" + )) + if (base::length(.arg_conflict) > 0L) cli::cli_abort(base::c("Argument {.arg {(.arg_conflict)}} of {.fn page_rank} was supplied more than once.", i = "Pass it exactly once, by its new name {.arg {(.arg_conflict)}}.")) for (.arg_i in base::seq_along(.arg_recovered)) base::do.call(base::delayedAssign, base::list(.arg_names[[.arg_i]], base::call("[[", .arg_handle$env, .arg_recovered[[.arg_i]]), base::baseenv(), base::environment())) .arg_dots <- base::substitute(...()) .arg_tags <- base::as.character(base::names(.arg_dots)) @@ -2035,7 +2040,7 @@ page_rank <- function( .arg_shown <- base::ifelse(base::is.na(.arg_tag_of), .arg_recovered, base::paste0(.arg_tag_of, " = ")) lifecycle::deprecate_soft( "3.0.0", - what = base::I("Calling `page_rank()` with positional or abbreviated arguments"), + what = base::I("Calling `page_rank()` with positional, abbreviated or renamed arguments"), details = base::c( i = base::paste0("Detected call: page_rank(", base::paste(base::c("graph", .arg_shown), collapse = ", "), ")"), i = base::paste0("Use instead: page_rank(", base::paste(base::c("graph", base::paste0(.arg_names, " = ")), collapse = ", "), ")") @@ -2051,7 +2056,7 @@ page_rank <- function( personalized_pagerank_impl( graph = graph, - algo = algo, + algo = algorithm, vids = vids, directed = directed, damping = damping, diff --git a/R/community.R b/R/community.R index d0a7c7b4a9..999aca0f92 100644 --- a/R/community.R +++ b/R/community.R @@ -3714,8 +3714,6 @@ contract <- function( # BEGIN GENERATED ARG_HANDLE: contract, do not edit, see tools/generate-migrations.R # fmt: skip if (...length() > 0L) { - .arg_ambiguous <- base::intersect(base::names(base::substitute(...())), base::c("v", "ve", "ver", "vert", "verte", "vertex")) - if (base::length(.arg_ambiguous) > 0L) cli::cli_abort("Argument {.arg {(.arg_ambiguous[[1L]])}} matches multiple arguments of {.fn contract}.") # Pre-3.0.0 signature: contract(graph, mapping, vertex.attr.comb) .old_signature <- function(vertex.attr.comb, ...) { if (...length() > 0L) { diff --git a/R/conversion.R b/R/conversion.R index a16c2744a9..b41fddbe75 100644 --- a/R/conversion.R +++ b/R/conversion.R @@ -824,8 +824,6 @@ as_undirected <- function( # BEGIN GENERATED ARG_HANDLE: as_undirected, do not edit, see tools/generate-migrations.R # fmt: skip if (...length() > 0L) { - .arg_ambiguous <- base::intersect(base::names(base::substitute(...())), base::c("e", "ed", "edg", "edge")) - if (base::length(.arg_ambiguous) > 0L) cli::cli_abort("Argument {.arg {(.arg_ambiguous[[1L]])}} matches multiple arguments of {.fn as_undirected}.") # Pre-3.0.0 signature: as_undirected(graph, mode, edge.attr.comb) .old_signature <- function(edge.attr.comb, ...) { if (...length() > 0L) { diff --git a/R/migration-fixture.R b/R/migration-fixture.R index 84298b73a1..3bcc374c39 100644 --- a/R/migration-fixture.R +++ b/R/migration-fixture.R @@ -21,8 +21,6 @@ migration_fixture <- function( # BEGIN GENERATED ARG_HANDLE: migration_fixture, do not edit, see tools/generate-migrations.R # fmt: skip if (...length() > 0L) { - .arg_ambiguous <- base::intersect(base::names(base::substitute(...())), base::c("w", "we", "wei", "weig", "weigh")) - if (base::length(.arg_ambiguous) > 0L) cli::cli_abort("Argument {.arg {(.arg_ambiguous[[1L]])}} matches multiple arguments of {.fn migration_fixture}.") # Pre-3.0.0 signature: migration_fixture(graph, n, weight, kind, directed) .old_signature <- function(weight, kind, directed, ...) { if (...length() > 0L) { @@ -209,3 +207,250 @@ migration_fixture_shadow <- function( list(graph = graph, weights = weights, names = names, c = c, attr = attr) } + +# `migration_fixture_rename()`: pre-3.0.0 signature f(graph, from, to, kind, +# directed), now f(graph, vertices, to, ..., type, directed) with `from` renamed +# to `vertices` in its head slot and kept as a deprecated formal. +#' @noRd +migration_fixture_rename <- function( + graph, + vertices = NULL, + to = NULL, + ..., + type = "out", + directed = FALSE, + from = deprecated() +) { + # BEGIN GENERATED ARG_HANDLE: migration_fixture_rename, do not edit, see tools/generate-migrations.R + # fmt: skip + if (!base::missing(from)) { + # Pre-3.0.0 signature: migration_fixture_rename(graph, from, to, kind, directed) + .legacy_signature <- function(graph, from, to, kind, directed, ...) { + base::list(env = base::environment(), n_extra = ...length(), extra = base::as.character(base::names(base::substitute(...()))), recovered = base::c( + if (!base::missing(graph)) base::c(graph = "graph"), + if (!base::missing(from)) base::c(vertices = "from"), + if (!base::missing(to)) base::c(to = "to"), + if (!base::missing(kind)) base::c(type = "kind"), + if (!base::missing(directed)) base::c(directed = "directed") + )) + } + .arg_call <- base::sys.call() + .arg_call[[1L]] <- .legacy_signature + .arg_handle <- base::eval(.arg_call, base::parent.frame()) + .arg_extra <- .arg_handle$extra + if (.arg_handle$n_extra > base::sum(base::nzchar(.arg_extra))) cli::cli_abort("Too many arguments passed to {.fn migration_fixture_rename}.") + .arg_explicit <- base::c("graph", "vertices", "to", "type", "directed", "from")[base::pmatch(.arg_extra, base::c("graph", "vertices", "to", "type", "directed", "from"), duplicates.ok = TRUE)] + if (base::anyNA(.arg_explicit)) cli::cli_abort("Unexpected argument passed to {.fn migration_fixture_rename}: {.arg {(.arg_extra[base::is.na(.arg_explicit)])}}.") + .arg_recovered <- .arg_handle$recovered + .arg_names <- base::names(.arg_recovered) + .arg_conflict <- base::intersect(.arg_names, .arg_explicit) + if (base::length(.arg_conflict) > 0L) { + .arg_conflict <- .arg_conflict[[1L]] + .arg_via <- .arg_recovered[[.arg_conflict]] + cli::cli_abort(base::c("Argument {.arg {(.arg_conflict)}} of {.fn migration_fixture_rename} was supplied more than once.", i = if (base::is.element(.arg_via, base::names(.arg_call))) "It was also supplied via its legacy name {.arg {(.arg_via)}}." else "It was also passed by position, as {.arg {(.arg_via)}} of the old signature.")) + } + for (.arg_i in base::seq_along(.arg_recovered)) base::do.call(base::delayedAssign, base::list(.arg_names[[.arg_i]], base::call("[[", .arg_handle$env, .arg_recovered[[.arg_i]]), base::baseenv(), base::environment())) + if (!base::missing(from)) lifecycle::deprecate_soft("3.0.0", "migration_fixture_rename(from = )", "migration_fixture_rename(vertices = )") + .arg_tail <- .arg_recovered[base::is.element(.arg_recovered, base::c("kind", "directed"))] + .arg_tail <- .arg_tail[!base::is.element(.arg_tail, base::names(.arg_call)) | base::names(.arg_tail) != .arg_tail] + if (base::length(.arg_tail) > 0L) { + .arg_shown <- base::ifelse(base::is.element(.arg_tail, base::names(.arg_call)), base::paste0(.arg_tail, " = "), .arg_tail) + lifecycle::deprecate_soft( + "3.0.0", + what = base::I("Calling `migration_fixture_rename()` with positional, abbreviated or renamed arguments"), + details = base::c( + i = base::paste0("Detected call: migration_fixture_rename(", base::paste(base::c("graph", "vertices", "to", .arg_shown), collapse = ", "), ")"), + i = base::paste0("Use instead: migration_fixture_rename(", base::paste(base::c("graph", "vertices", "to", base::paste0(base::names(.arg_tail), " = ")), collapse = ", "), ")") + ) + ) + } + } else if (...length() > 0L) { + .arg_forbidden <- base::intersect(base::names(base::sys.call()), base::c("t")) + if (base::length(.arg_forbidden) > 0L) cli::cli_abort(base::c("Argument {.arg {(.arg_forbidden)}} matches multiple formal arguments of {.fn migration_fixture_rename}.", i = "Spell out the full argument name.")) + # Pre-3.0.0 signature: migration_fixture_rename(graph, from, to, kind, directed) + .old_signature <- function(kind, directed, ...) { + if (...length() > 0L) { + .arg_extra <- base::names(base::substitute(...())) + .arg_extra <- .arg_extra[base::nzchar(.arg_extra)] + if (base::length(.arg_extra) == 0L) cli::cli_abort("Too many arguments passed to {.fn migration_fixture_rename}.", call = base::parent.frame()) + cli::cli_abort(base::c("Unexpected argument passed to {.fn migration_fixture_rename}: {.arg {(.arg_extra)}}.", i = "Arguments after {.arg ...} must be spelled out in full."), call = base::parent.frame()) + } + base::list(env = base::environment(), recovered = base::c( + if (!base::missing(kind)) base::c(type = "kind"), + if (!base::missing(directed)) base::c(directed = "directed") + )) + } + .arg_given <- base::c( + if (!base::missing(directed)) "directed" + ) + .arg_handle <- base::eval(base::as.call(base::c(base::quote(.old_signature), base::structure(base::rep(base::list(NULL), base::length(.arg_given)), names = .arg_given), base::quote(...)))) + .arg_recovered <- .arg_handle$recovered[!base::is.element(.arg_handle$recovered, .arg_given)] + if (base::length(.arg_recovered) > 0L) { + .arg_names <- base::names(.arg_recovered) + .arg_conflict <- base::intersect(.arg_names, base::c( + if (!base::missing(type)) "type" + )) + if (base::length(.arg_conflict) > 0L) cli::cli_abort(base::c("Argument {.arg {(.arg_conflict)}} of {.fn migration_fixture_rename} was supplied more than once.", i = "Pass it exactly once, by its new name {.arg {(.arg_conflict)}}.")) + for (.arg_i in base::seq_along(.arg_recovered)) base::do.call(base::delayedAssign, base::list(.arg_names[[.arg_i]], base::call("[[", .arg_handle$env, .arg_recovered[[.arg_i]]), base::baseenv(), base::environment())) + .arg_dots <- base::substitute(...()) + .arg_tags <- base::as.character(base::names(.arg_dots)) + .arg_tags <- .arg_tags[base::nzchar(.arg_tags) & !base::vapply(.arg_dots, base::identical, base::logical(1L), base::quote(expr = ))] + .arg_tag_of <- .arg_tags[base::match(.arg_recovered, base::c("kind", "directed")[base::pmatch(.arg_tags, base::c("kind", "directed"), duplicates.ok = TRUE)])] + .arg_shown <- base::ifelse(base::is.na(.arg_tag_of), .arg_recovered, base::paste0(.arg_tag_of, " = ")) + lifecycle::deprecate_soft( + "3.0.0", + what = base::I("Calling `migration_fixture_rename()` with positional, abbreviated or renamed arguments"), + details = base::c( + i = base::paste0("Detected call: migration_fixture_rename(", base::paste(base::c("graph", "vertices", "to", .arg_shown), collapse = ", "), ")"), + i = base::paste0("Use instead: migration_fixture_rename(", base::paste(base::c("graph", "vertices", "to", base::paste0(.arg_names, " = ")), collapse = ", "), ")") + ) + ) + } + } + # END GENERATED ARG_HANDLE + + list( + graph = graph, + vertices = vertices, + to = to, + type = type, + directed = directed + ) +} + +# `migration_fixture_rename_nodots()`: pre-3.0.0 signature f(graph, name, index, +# value), now f(graph, name, vertices, value) with `index` renamed to +# `vertices` and no `...` (the set_vertex_attr() shape). +#' @noRd +migration_fixture_rename_nodots <- function( + graph, + name, + vertices = NULL, + value, + index = deprecated() +) { + # BEGIN GENERATED ARG_HANDLE: migration_fixture_rename_nodots, do not edit, see tools/generate-migrations.R + # fmt: skip + if (!base::missing(index)) { + # Pre-3.0.0 signature: migration_fixture_rename_nodots(graph, name, index, value) + .legacy_signature <- function(graph, name, index, value, ...) { + base::list(env = base::environment(), n_extra = ...length(), extra = base::as.character(base::names(base::substitute(...()))), recovered = base::c( + if (!base::missing(graph)) base::c(graph = "graph"), + if (!base::missing(name)) base::c(name = "name"), + if (!base::missing(index)) base::c(vertices = "index"), + if (!base::missing(value)) base::c(value = "value") + )) + } + .arg_call <- base::sys.call() + .arg_call[[1L]] <- .legacy_signature + .arg_handle <- base::eval(.arg_call, base::parent.frame()) + .arg_extra <- .arg_handle$extra + if (.arg_handle$n_extra > base::sum(base::nzchar(.arg_extra))) cli::cli_abort("Too many arguments passed to {.fn migration_fixture_rename_nodots}.") + .arg_explicit <- base::c("graph", "name", "vertices", "value", "index")[base::pmatch(.arg_extra, base::c("graph", "name", "vertices", "value", "index"), duplicates.ok = TRUE)] + if (base::anyNA(.arg_explicit)) cli::cli_abort("Unexpected argument passed to {.fn migration_fixture_rename_nodots}: {.arg {(.arg_extra[base::is.na(.arg_explicit)])}}.") + .arg_recovered <- .arg_handle$recovered + .arg_names <- base::names(.arg_recovered) + .arg_conflict <- base::intersect(.arg_names, .arg_explicit) + if (base::length(.arg_conflict) > 0L) { + .arg_conflict <- .arg_conflict[[1L]] + .arg_via <- .arg_recovered[[.arg_conflict]] + cli::cli_abort(base::c("Argument {.arg {(.arg_conflict)}} of {.fn migration_fixture_rename_nodots} was supplied more than once.", i = if (base::is.element(.arg_via, base::names(.arg_call))) "It was also supplied via its legacy name {.arg {(.arg_via)}}." else "It was also passed by position, as {.arg {(.arg_via)}} of the old signature.")) + } + for (.arg_i in base::seq_along(.arg_recovered)) base::do.call(base::delayedAssign, base::list(.arg_names[[.arg_i]], base::call("[[", .arg_handle$env, .arg_recovered[[.arg_i]]), base::baseenv(), base::environment())) + if (!base::missing(index)) lifecycle::deprecate_soft("3.0.0", "migration_fixture_rename_nodots(index = )", "migration_fixture_rename_nodots(vertices = )") + } + # END GENERATED ARG_HANDLE + + list(graph = graph, name = name, vertices = vertices, value = value) +} + +# The replacement-function twin of `migration_fixture_rename_nodots()`. +#' @noRd +`migration_fixture_rename<-` <- function( + graph, + name, + vertices = NULL, + index = deprecated(), + value +) { + # BEGIN GENERATED ARG_HANDLE: migration_fixture_rename<-, do not edit, see tools/generate-migrations.R + # fmt: skip + if (!base::missing(index)) { + # Pre-3.0.0 signature: migration_fixture_rename<-(graph, name, index, value) + .legacy_signature <- function(graph, name, index, value, ...) { + base::list(env = base::environment(), n_extra = ...length(), extra = base::as.character(base::names(base::substitute(...()))), recovered = base::c( + if (!base::missing(graph)) base::c(graph = "graph"), + if (!base::missing(name)) base::c(name = "name"), + if (!base::missing(index)) base::c(vertices = "index"), + if (!base::missing(value)) base::c(value = "value") + )) + } + .arg_call <- base::sys.call() + .arg_call[[1L]] <- .legacy_signature + .arg_handle <- base::eval(.arg_call, base::parent.frame()) + .arg_extra <- .arg_handle$extra + if (.arg_handle$n_extra > base::sum(base::nzchar(.arg_extra))) cli::cli_abort("Too many arguments passed to {.fn migration_fixture_rename<-}.") + .arg_explicit <- base::c("graph", "name", "vertices", "value", "index")[base::pmatch(.arg_extra, base::c("graph", "name", "vertices", "value", "index"), duplicates.ok = TRUE)] + if (base::anyNA(.arg_explicit)) cli::cli_abort("Unexpected argument passed to {.fn migration_fixture_rename<-}: {.arg {(.arg_extra[base::is.na(.arg_explicit)])}}.") + .arg_recovered <- .arg_handle$recovered + .arg_names <- base::names(.arg_recovered) + .arg_conflict <- base::intersect(.arg_names, .arg_explicit) + if (base::length(.arg_conflict) > 0L) { + .arg_conflict <- .arg_conflict[[1L]] + .arg_via <- .arg_recovered[[.arg_conflict]] + cli::cli_abort(base::c("Argument {.arg {(.arg_conflict)}} of {.fn migration_fixture_rename<-} was supplied more than once.", i = if (base::is.element(.arg_via, base::names(.arg_call))) "It was also supplied via its legacy name {.arg {(.arg_via)}}." else "It was also passed by position, as {.arg {(.arg_via)}} of the old signature.")) + } + for (.arg_i in base::seq_along(.arg_recovered)) base::do.call(base::delayedAssign, base::list(.arg_names[[.arg_i]], base::call("[[", .arg_handle$env, .arg_recovered[[.arg_i]]), base::baseenv(), base::environment())) + if (!base::missing(index)) lifecycle::deprecate_soft("3.0.0", "`migration_fixture_rename<-`(index = )", "`migration_fixture_rename<-`(vertices = )") + } + # END GENERATED ARG_HANDLE + + graph[[name]] <- list(vertices = vertices, value = value) + graph +} + +# `migration_fixture_passthrough()`: pre-3.0.0 signature f(x, from, to, ...), +# now f(x, vertices, to, ...) with `from` renamed to `vertices` and `...` +# passed on. +#' @noRd +migration_fixture_passthrough <- function( + x, + vertices = NULL, + to = NULL, + ..., + from = deprecated() +) { + # BEGIN GENERATED ARG_HANDLE: migration_fixture_passthrough, do not edit, see tools/generate-migrations.R + # fmt: skip + if (!base::missing(from)) { + # Pre-3.0.0 signature: migration_fixture_passthrough(x, from, to) + .legacy_signature <- function(x, from, to, ...) { + base::list(env = base::environment(), n_extra = ...length(), extra = base::as.character(base::names(base::substitute(...()))), recovered = base::c( + if (!base::missing(x)) base::c(x = "x"), + if (!base::missing(from)) base::c(vertices = "from"), + if (!base::missing(to)) base::c(to = "to") + )) + } + .arg_call <- base::sys.call() + .arg_call[[1L]] <- .legacy_signature + .arg_handle <- base::eval(.arg_call, base::parent.frame()) + .arg_extra <- .arg_handle$extra + if (base::length(.arg_extra) < .arg_handle$n_extra) .arg_extra <- base::rep("", .arg_handle$n_extra) + .arg_explicit <- base::c("x", "vertices", "to", "from")[base::pmatch(.arg_extra, base::c("x", "vertices", "to", "from"), duplicates.ok = TRUE)] + if (base::sum(base::is.na(.arg_explicit)) != ...length()) cli::cli_abort(base::c("Can't match this call to the pre-3.0.0 signature of {.fn migration_fixture_passthrough}.", i = "Name its arguments.")) + .arg_explicit <- .arg_explicit[!base::is.na(.arg_explicit)] + .arg_recovered <- .arg_handle$recovered + .arg_names <- base::names(.arg_recovered) + .arg_conflict <- base::intersect(.arg_names, .arg_explicit) + if (base::length(.arg_conflict) > 0L) { + .arg_conflict <- .arg_conflict[[1L]] + .arg_via <- .arg_recovered[[.arg_conflict]] + cli::cli_abort(base::c("Argument {.arg {(.arg_conflict)}} of {.fn migration_fixture_passthrough} was supplied more than once.", i = if (base::is.element(.arg_via, base::names(.arg_call))) "It was also supplied via its legacy name {.arg {(.arg_via)}}." else "It was also passed by position, as {.arg {(.arg_via)}} of the old signature.")) + } + for (.arg_i in base::seq_along(.arg_recovered)) base::do.call(base::delayedAssign, base::list(.arg_names[[.arg_i]], base::call("[[", .arg_handle$env, .arg_recovered[[.arg_i]]), base::baseenv(), base::environment())) + if (!base::missing(from)) lifecycle::deprecate_soft("3.0.0", "migration_fixture_passthrough(from = )", "migration_fixture_passthrough(vertices = )") + } + # END GENERATED ARG_HANDLE + + list(x = x, vertices = vertices, to = to, dots = list(...)) +} diff --git a/R/operators.R b/R/operators.R index 3e510a7e5b..37686bbcb8 100644 --- a/R/operators.R +++ b/R/operators.R @@ -1034,8 +1034,6 @@ compose <- function( # BEGIN GENERATED ARG_HANDLE: compose, do not edit, see tools/generate-migrations.R # fmt: skip if (...length() > 0L) { - .arg_ambiguous <- base::intersect(base::names(base::substitute(...())), base::c("g", "gr", "gra", "grap", "graph", "v", "ve", "ver", "vert", "verte", "vertex", "e", "ed", "edg", "edge")) - if (base::length(.arg_ambiguous) > 0L) cli::cli_abort("Argument {.arg {(.arg_ambiguous[[1L]])}} matches multiple arguments of {.fn compose}.") # Pre-3.0.0 signature: compose(g1, g2, byname, graph.attr.comb, vertex.attr.comb, edge.attr.comb) .old_signature <- function(byname, graph.attr.comb, vertex.attr.comb, edge.attr.comb, ...) { if (...length() > 0L) { diff --git a/R/simple.R b/R/simple.R index c6867d1a72..cf4b460c59 100644 --- a/R/simple.R +++ b/R/simple.R @@ -102,8 +102,6 @@ simplify <- function( # BEGIN GENERATED ARG_HANDLE: simplify, do not edit, see tools/generate-migrations.R # fmt: skip if (...length() > 0L) { - .arg_ambiguous <- base::intersect(base::names(base::substitute(...())), base::c("e", "ed", "edg", "edge")) - if (base::length(.arg_ambiguous) > 0L) cli::cli_abort("Argument {.arg {(.arg_ambiguous[[1L]])}} matches multiple arguments of {.fn simplify}.") # Pre-3.0.0 signature: simplify(graph, remove.multiple, remove.loops, edge.attr.comb) .old_signature <- function(edge.attr.comb, ...) { if (...length() > 0L) { diff --git a/R/structural-properties.R b/R/structural-properties.R index 4a65776adf..a4a7fc47e8 100644 --- a/R/structural-properties.R +++ b/R/structural-properties.R @@ -3313,7 +3313,7 @@ topo_sort <- function( #' `NULL`, then the edge attribute is used automatically. The goal of #' the feedback arc set problem is to find a feedback arc set with the smallest #' total weight. -#' @param algo Specifies the algorithm to use. \dQuote{`exact_ip`} solves +#' @param algorithm Specifies the algorithm to use. \dQuote{`exact_ip`} solves #' the feedback arc set problem with an exact integer programming algorithm that #' guarantees that the total weight of the removed edges is as small as possible. #' \dQuote{`approx_eades`} uses a fast (linear-time) approximation @@ -3333,12 +3333,12 @@ topo_sort <- function( #' #' g <- sample_gnm(20, 40, directed = TRUE) #' feedback_arc_set(g) -#' feedback_arc_set(g, algo = "approx_eades") +#' feedback_arc_set(g, algorithm = "approx_eades") feedback_arc_set <- function( graph, ..., weights = NULL, - algo = c("approx_eades", "exact_ip") + algorithm = c("approx_eades", "exact_ip") ) { # BEGIN GENERATED ARG_HANDLE: feedback_arc_set, do not edit, see tools/generate-migrations.R # fmt: skip @@ -3353,17 +3353,20 @@ feedback_arc_set <- function( } base::list(env = base::environment(), recovered = base::c( if (!base::missing(weights)) base::c(weights = "weights"), - if (!base::missing(algo)) base::c(algo = "algo") + if (!base::missing(algo)) base::c(algorithm = "algo") )) } .arg_given <- base::c( - if (!base::missing(weights)) "weights", - if (!base::missing(algo)) "algo" + if (!base::missing(weights)) "weights" ) .arg_handle <- base::eval(base::as.call(base::c(base::quote(.old_signature), base::structure(base::rep(base::list(NULL), base::length(.arg_given)), names = .arg_given), base::quote(...)))) .arg_recovered <- .arg_handle$recovered[!base::is.element(.arg_handle$recovered, .arg_given)] if (base::length(.arg_recovered) > 0L) { .arg_names <- base::names(.arg_recovered) + .arg_conflict <- base::intersect(.arg_names, base::c( + if (!base::missing(algorithm)) "algorithm" + )) + if (base::length(.arg_conflict) > 0L) cli::cli_abort(base::c("Argument {.arg {(.arg_conflict)}} of {.fn feedback_arc_set} was supplied more than once.", i = "Pass it exactly once, by its new name {.arg {(.arg_conflict)}}.")) for (.arg_i in base::seq_along(.arg_recovered)) base::do.call(base::delayedAssign, base::list(.arg_names[[.arg_i]], base::call("[[", .arg_handle$env, .arg_recovered[[.arg_i]]), base::baseenv(), base::environment())) .arg_dots <- base::substitute(...()) .arg_tags <- base::as.character(base::names(.arg_dots)) @@ -3372,7 +3375,7 @@ feedback_arc_set <- function( .arg_shown <- base::ifelse(base::is.na(.arg_tag_of), .arg_recovered, base::paste0(.arg_tag_of, " = ")) lifecycle::deprecate_soft( "3.0.0", - what = base::I("Calling `feedback_arc_set()` with positional or abbreviated arguments"), + what = base::I("Calling `feedback_arc_set()` with positional, abbreviated or renamed arguments"), details = base::c( i = base::paste0("Detected call: feedback_arc_set(", base::paste(base::c("graph", .arg_shown), collapse = ", "), ")"), i = base::paste0("Use instead: feedback_arc_set(", base::paste(base::c("graph", base::paste0(.arg_names, " = ")), collapse = ", "), ")") @@ -3385,7 +3388,7 @@ feedback_arc_set <- function( feedback_arc_set_impl( graph = graph, weights = weights, - algo = algo + algo = algorithm ) } @@ -3405,7 +3408,7 @@ feedback_arc_set <- function( #' `NULL`, then the vertex attribute is used automatically. The goal of #' the feedback vertex set problem is to find a feedback vertex set with #' the smallest total weight. -#' @param algo Specifies the algorithm to use. Currently, \dQuote{`exact_ip`}, +#' @param algorithm Specifies the algorithm to use. Currently, \dQuote{`exact_ip`}, #' which solves the feedback vertex set problem with an exact integer #' programming approach, is the only option. #' @return A vertex sequence (by default, but see the `return.vs.es` option @@ -3422,7 +3425,7 @@ feedback_vertex_set <- function( graph, ..., weights = NULL, - algo = c("exact_ip") + algorithm = c("exact_ip") ) { # BEGIN GENERATED ARG_HANDLE: feedback_vertex_set, do not edit, see tools/generate-migrations.R # fmt: skip @@ -3437,17 +3440,20 @@ feedback_vertex_set <- function( } base::list(env = base::environment(), recovered = base::c( if (!base::missing(weights)) base::c(weights = "weights"), - if (!base::missing(algo)) base::c(algo = "algo") + if (!base::missing(algo)) base::c(algorithm = "algo") )) } .arg_given <- base::c( - if (!base::missing(weights)) "weights", - if (!base::missing(algo)) "algo" + if (!base::missing(weights)) "weights" ) .arg_handle <- base::eval(base::as.call(base::c(base::quote(.old_signature), base::structure(base::rep(base::list(NULL), base::length(.arg_given)), names = .arg_given), base::quote(...)))) .arg_recovered <- .arg_handle$recovered[!base::is.element(.arg_handle$recovered, .arg_given)] if (base::length(.arg_recovered) > 0L) { .arg_names <- base::names(.arg_recovered) + .arg_conflict <- base::intersect(.arg_names, base::c( + if (!base::missing(algorithm)) "algorithm" + )) + if (base::length(.arg_conflict) > 0L) cli::cli_abort(base::c("Argument {.arg {(.arg_conflict)}} of {.fn feedback_vertex_set} was supplied more than once.", i = "Pass it exactly once, by its new name {.arg {(.arg_conflict)}}.")) for (.arg_i in base::seq_along(.arg_recovered)) base::do.call(base::delayedAssign, base::list(.arg_names[[.arg_i]], base::call("[[", .arg_handle$env, .arg_recovered[[.arg_i]]), base::baseenv(), base::environment())) .arg_dots <- base::substitute(...()) .arg_tags <- base::as.character(base::names(.arg_dots)) @@ -3456,7 +3462,7 @@ feedback_vertex_set <- function( .arg_shown <- base::ifelse(base::is.na(.arg_tag_of), .arg_recovered, base::paste0(.arg_tag_of, " = ")) lifecycle::deprecate_soft( "3.0.0", - what = base::I("Calling `feedback_vertex_set()` with positional or abbreviated arguments"), + what = base::I("Calling `feedback_vertex_set()` with positional, abbreviated or renamed arguments"), details = base::c( i = base::paste0("Detected call: feedback_vertex_set(", base::paste(base::c("graph", .arg_shown), collapse = ", "), ")"), i = base::paste0("Use instead: feedback_vertex_set(", base::paste(base::c("graph", base::paste0(.arg_names, " = ")), collapse = ", "), ")") @@ -3469,7 +3475,7 @@ feedback_vertex_set <- function( feedback_vertex_set_impl( graph = graph, weights = weights, - algo = algo + algo = algorithm ) } diff --git a/man/feedback_arc_set.Rd b/man/feedback_arc_set.Rd index a689eed785..c37f7b0539 100644 --- a/man/feedback_arc_set.Rd +++ b/man/feedback_arc_set.Rd @@ -8,7 +8,7 @@ feedback_arc_set( graph, ..., weights = NULL, - algo = c("approx_eades", "exact_ip") + algorithm = c("approx_eades", "exact_ip") ) } \arguments{ @@ -22,7 +22,7 @@ attribute called \sQuote{\code{weight}}, and this argument is the feedback arc set problem is to find a feedback arc set with the smallest total weight.} -\item{algo}{Specifies the algorithm to use. \dQuote{\code{exact_ip}} solves +\item{algorithm}{Specifies the algorithm to use. \dQuote{\code{exact_ip}} solves the feedback arc set problem with an exact integer programming algorithm that guarantees that the total weight of the removed edges is as small as possible. \dQuote{\code{approx_eades}} uses a fast (linear-time) approximation @@ -53,7 +53,7 @@ component is a tree). g <- sample_gnm(20, 40, directed = TRUE) feedback_arc_set(g) -feedback_arc_set(g, algo = "approx_eades") +feedback_arc_set(g, algorithm = "approx_eades") } \references{ Peter Eades, Xuemin Lin and W.F.Smyth: A fast and effective diff --git a/man/feedback_vertex_set.Rd b/man/feedback_vertex_set.Rd index 89dd657e20..660dd399f6 100644 --- a/man/feedback_vertex_set.Rd +++ b/man/feedback_vertex_set.Rd @@ -4,7 +4,7 @@ \alias{feedback_vertex_set} \title{Finding a feedback vertex set in a graph} \usage{ -feedback_vertex_set(graph, ..., weights = NULL, algo = c("exact_ip")) +feedback_vertex_set(graph, ..., weights = NULL, algorithm = c("exact_ip")) } \arguments{ \item{graph}{The input graph} @@ -17,7 +17,7 @@ attribute called \sQuote{\code{weight}}, and this argument is the feedback vertex set problem is to find a feedback vertex set with the smallest total weight.} -\item{algo}{Specifies the algorithm to use. Currently, \dQuote{\code{exact_ip}}, +\item{algorithm}{Specifies the algorithm to use. Currently, \dQuote{\code{exact_ip}}, which solves the feedback vertex set problem with an exact integer programming approach, is the only option.} } diff --git a/man/page.rank.Rd b/man/page.rank.Rd index 1a059e6ed3..dbb681bafc 100644 --- a/man/page.rank.Rd +++ b/man/page.rank.Rd @@ -18,14 +18,8 @@ page.rank( \arguments{ \item{graph}{The graph object.} -\item{algo}{Character scalar, which implementation to use to carry out the -calculation. The default is \code{"prpack"}, which uses the PRPACK library -(\url{https://github.com/dgleich/prpack}) to calculate PageRank scores -by solving a set of linear equations. This is a new implementation in igraph -version 0.7, and the suggested one, as it is the most stable and the fastest -for all but small graphs. \code{"arpack"} uses the ARPACK library, the -default implementation from igraph version 0.5 until version 0.7. It computes -PageRank scores by solving an eingevalue problem.} +\item{algo}{\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#deprecated}{\figure{lifecycle-deprecated.svg}{options: alt='[Deprecated]'}}}{\strong{[Deprecated]}} Use \code{algorithm} in +\code{\link[=page_rank]{page_rank()}} instead.} \item{vids}{The vertices of interest. The default \code{NULL} selects all vertices.} diff --git a/man/page_rank.Rd b/man/page_rank.Rd index a5eb3b276a..8d71b3ac4c 100644 --- a/man/page_rank.Rd +++ b/man/page_rank.Rd @@ -7,7 +7,7 @@ page_rank( graph, ..., - algo = c("prpack", "arpack"), + algorithm = c("prpack", "arpack"), vids = NULL, directed = TRUE, damping = 0.85, @@ -21,7 +21,7 @@ page_rank( \item{...}{These dots are for future extensions and must be empty.} -\item{algo}{Character scalar, which implementation to use to carry out the +\item{algorithm}{Character scalar, which implementation to use to carry out the calculation. The default is \code{"prpack"}, which uses the PRPACK library (\url{https://github.com/dgleich/prpack}) to calculate PageRank scores by solving a set of linear equations. This is a new implementation in igraph diff --git a/tests/testthat/_snaps/centrality.md b/tests/testthat/_snaps/centrality.md index bcb12bae95..ee47b2fc39 100644 --- a/tests/testthat/_snaps/centrality.md +++ b/tests/testthat/_snaps/centrality.md @@ -59,3 +59,13 @@ Error in `arpack()`: ! Can't use unkown ARPACK options: unknown_thing1, unknown_thing2 +# page_rank(algo = ) is deprecated but still works + + Code + res_legacy <- page_rank(star, algo = "prpack") + Condition + Warning: + Calling `page_rank()` with positional, abbreviated or renamed arguments was deprecated in igraph 3.0.0. + i Detected call: page_rank(graph, algo = ) + i Use instead: page_rank(graph, algorithm = ) + diff --git a/tests/testthat/_snaps/migration-fixture.md b/tests/testthat/_snaps/migration-fixture.md index b444ea0974..49004b2a38 100644 --- a/tests/testthat/_snaps/migration-fixture.md +++ b/tests/testthat/_snaps/migration-fixture.md @@ -374,6 +374,32 @@ [1] TRUE +--- + + Code + migration_fixture("g", 5, weig = 1) + Condition + Warning: + Calling `migration_fixture()` with positional, abbreviated or renamed arguments was deprecated in igraph 3.0.0. + i Detected call: migration_fixture(graph, n, weig = ) + i Use instead: migration_fixture(graph, n, weights = ) + Output + $graph + [1] "g" + + $n + [1] 5 + + $weights + [1] 1 + + $type + [1] "out" + + $directed + [1] FALSE + + --- Code @@ -506,14 +532,6 @@ # error message snapshots - Code - migration_fixture("g", 5, weig = 1) - Condition - Error in `migration_fixture()`: - ! Argument `weig` matches multiple arguments of `migration_fixture()`. - ---- - Code migration_fixture("g", 5, foo = 1) Condition @@ -659,3 +677,50 @@ ! Argument `weights` of `migration_fixture_shadow()` was supplied more than once. i Pass it exactly once, by its new name `weights`. +# positional old tail arguments are recovered on the same path + + Code + res <- migration_fixture_rename("g", from = 1, 2, "in", TRUE) + Condition + Warning: + The `from` argument of `migration_fixture_rename()` is deprecated as of igraph 3.0.0. + i Please use the `vertices` argument instead. + Warning: + Calling `migration_fixture_rename()` with positional, abbreviated or renamed arguments was deprecated in igraph 3.0.0. + i Detected call: migration_fixture_rename(graph, vertices, to, kind, directed) + i Use instead: migration_fixture_rename(graph, vertices, to, type = , directed = ) + +# a re-matched call reports what the old signature cannot take + + Code + migration_fixture_rename("g", from = 1, vertices = 2) + Condition + Error in `migration_fixture_rename()`: + ! Argument `vertices` of `migration_fixture_rename()` was supplied more than once. + i It was also supplied via its legacy name `from`. + Code + migration_fixture_rename("g", from = 1, 2, "in", type = "out") + Condition + Error in `migration_fixture_rename()`: + ! Argument `type` of `migration_fixture_rename()` was supplied more than once. + i It was also passed by position, as `kind` of the old signature. + Code + migration_fixture_rename("g", from = 1, 2, "in", TRUE, 99) + Condition + Error in `migration_fixture_rename()`: + ! Too many arguments passed to `migration_fixture_rename()`. + Code + migration_fixture_rename("g", from = 1, bogus = 3) + Condition + Error in `migration_fixture_rename()`: + ! Unexpected argument passed to `migration_fixture_rename()`: `bogus`. + +# a passed-on `...` that would shift is reported + + Code + migration_fixture_passthrough(1, from = 2, 3, 4) + Condition + Error in `migration_fixture_passthrough()`: + ! Can't match this call to the pre-3.0.0 signature of `migration_fixture_passthrough()`. + i Name its arguments. + diff --git a/tests/testthat/helper-migrations.R b/tests/testthat/helper-migrations.R index eb3c7f01f6..8b82ae4849 100644 --- a/tests/testthat/helper-migrations.R +++ b/tests/testthat/helper-migrations.R @@ -8,8 +8,27 @@ # (devtools::test(), covr). The generator only rewrites a file when its block # actually changes, so a clean tree stays clean. CI additionally guards against # committed drift (see .github/workflows/custom/before-install). +# The migration generator. tools/ is excluded from the built package, so it +# only exists in a source checkout. +migration_generator_path <- function() { + testthat::test_path("..", "..", "tools", "generate-migrations.R") +} + +# The generator's functions, sourced into a fresh environment, for tests of +# the generator itself. Skips the test when the generator is not available. +migration_generator_env <- function() { + generator <- migration_generator_path() + testthat::skip_if_not( + file.exists(generator), + "tools/generate-migrations.R is only available in a source checkout" + ) + gen_env <- new.env() + sys.source(generator, envir = gen_env) + gen_env +} + local({ - generator <- testthat::test_path("..", "..", "tools", "generate-migrations.R") + generator <- migration_generator_path() if (!file.exists(generator)) { return(invisible()) } diff --git a/tests/testthat/test-centrality.R b/tests/testthat/test-centrality.R index e4673cb4e3..8af9ee6897 100644 --- a/tests/testthat/test-centrality.R +++ b/tests/testthat/test-centrality.R @@ -1085,12 +1085,12 @@ test_that("page_rank() covers migrated tail args and positional recovery", { # The weight attribute is a decoy that the explicit `weights` must override. E(star)$weight <- c(10, rep(1, 8)) - # `algo` keeps its default value: + # `algorithm` keeps its default value: # non-default values select the legacy ARPACK implementation, # and `options` is only consumed by that implementation. res <- page_rank( star, - algo = "prpack", + algorithm = "prpack", vids = V(star)[1:5], directed = FALSE, damping = 0.9, @@ -1117,7 +1117,25 @@ test_that("page_rank() covers migrated tail args and positional recovery", { lifecycle::expect_deprecated( res_legacy <- page_rank(star, "prpack") ) - expect_identical(res_legacy, page_rank(star, algo = "prpack")) + expect_identical(res_legacy, page_rank(star, algorithm = "prpack")) +}) + +test_that("page_rank(algo = ) is deprecated but still works", { + rlang::local_options(lifecycle_verbosity = "warning") + star <- make_star(10, mode = "undirected") + expect_snapshot_igraph( + res_legacy <- page_rank(star, algo = "prpack") + ) + expect_identical(res_legacy, page_rank(star, algorithm = "prpack")) + # `alg` prefixes both `algo` and `algorithm`, but they are the same argument, + # so it is recovered rather than rejected as ambiguous. + lifecycle::expect_deprecated(res_abbrev <- page_rank(star, alg = "prpack")) + expect_identical(res_abbrev, page_rank(star, algorithm = "prpack")) + # `d` really is ambiguous (`damping` vs `directed`) and stays an error. + expect_error( + page_rank(star, d = 0.5), + "matches multiple arguments" + ) }) test_that("strength() covers migrated tail args and positional recovery", { diff --git a/tests/testthat/test-generate-migrations.R b/tests/testthat/test-generate-migrations.R index c1146679b2..6562e7914d 100644 --- a/tests/testthat/test-generate-migrations.R +++ b/tests/testthat/test-generate-migrations.R @@ -207,3 +207,101 @@ test_that("non-constant defaults are rejected, with no escape hatch", { "not supported" ) }) + +test_that("head renames are derived from the deprecated formals", { + gen <- local_generator() + norm <- gen$normalise_migration( + "fn_hr", + list( + old = function(graph, index, value) {}, + new = function( + graph, + vertices = NULL, + value, + ..., + index = deprecated() + ) {} + ) + ) + expect_identical(norm$head_renames, c(index = "vertices")) + expect_identical( + norm$old_to_new, + c(graph = "graph", index = "vertices", value = "value") + ) + + # A head slot renamed without a deprecated formal is no head rename: the old + # name could not be passed any more, so there is nothing to re-match. + norm <- gen$normalise_migration( + "fn_hr", + list( + old = function(graph, index, value) {}, + new = function(graph, vertices = NULL, value, ...) {} + ) + ) + expect_length(norm$head_renames, 0L) +}) + +test_that("a migration without `...` must be a head rename", { + gen <- local_generator() + norm <- gen$normalise_migration( + "fn_nd", + list( + old = function(graph, index, value) {}, + new = function(graph, vertices = NULL, value, index = deprecated()) {} + ) + ) + expect_false(norm$has_dots) + expect_identical(norm$head, c("graph", "vertices", "value")) + expect_identical(norm$tail, "index") + + expect_error( + gen$normalise_migration( + "fn_nd", + list(old = function(graph, x) {}, new = function(graph, x) {}) + ), + "must rename a head argument" + ) + expect_error( + gen$normalise_migration( + "fn_nd", + list( + old = function(graph, index, value, extra) {}, + new = function(graph, vertices = NULL, value, index = deprecated()) {} + ) + ), + "must not be longer than its head" + ) + expect_error( + gen$normalise_migration( + "fn_nd", + list(old = function(graph, ...) {}, new = function(graph, ...) {}) + ), + "must not contain `...`" + ) +}) + +test_that("a head rename with nothing past the head renders no `...` recovery", { + gen <- local_generator() + entry <- gen$normalise_migration( + "fn_hd", + list( + old = function(out.w, in.w) {}, + new = function( + out_w, + in_w = NULL, + ..., + loops = TRUE, + out.w = deprecated(), + in.w = deprecated() + ) {} + ) + ) + block <- gen$render_arg_handle(entry) + expect_true(any(grepl( + "!base::missing(out.w) || !base::missing(in.w)", + block, + fixed = TRUE + ))) + expect_false(any(grepl("...length() > 0L) {", block, fixed = TRUE))) + expect_identical(block[[length(block)]], "}") +}) diff --git a/tests/testthat/test-migration-fixture.R b/tests/testthat/test-migration-fixture.R index 8b8e95458a..14effc7f55 100644 --- a/tests/testthat/test-migration-fixture.R +++ b/tests/testthat/test-migration-fixture.R @@ -334,6 +334,9 @@ test_that("recovery deprecation messages", { expect_snapshot_igraph(migration_fixture("g", 5, kind = "in")) expect_snapshot_igraph(migration_fixture("g", 5, kin = "in")) expect_snapshot_igraph(migration_fixture("g", 5, dir = TRUE)) + # `weig` prefixes both the old `weight` and the new `weights`, but they are + # the same argument, so it recovers instead of being rejected. + expect_snapshot_igraph(migration_fixture("g", 5, weig = 1)) # mixed: a positional value and a named abbreviation in the same call expect_snapshot_igraph(migration_fixture("g", 5, 1:3, dir = TRUE)) # named before positional: the positional value fills the first free slot @@ -360,12 +363,8 @@ test_that("recovery deprecation messages", { }) test_that("error message snapshots", { - # `weig` prefixes both the old `weight` and the new `weights`, so it is - # rejected before `.old_signature()` -- which only knows the old names -- gets - # a chance to resolve it to `weight`. The rest overflow into - # `.old_signature()`'s own `...`, which reports them rather than letting base - # R's "unused argument" out. - expect_snapshot(migration_fixture("g", 5, weig = 1), error = TRUE) + # These overflow into `.old_signature()`'s own `...`, which reports them + # rather than letting base R's "unused argument" out. expect_snapshot(migration_fixture("g", 5, foo = 1), error = TRUE) expect_snapshot(migration_fixture("g", 5, 1:3, weights = 9), error = TRUE) expect_snapshot(migration_fixture("g", 5, 1, 2, 3, 4), error = TRUE) @@ -377,13 +376,9 @@ test_that("the generated block is in sync with the registry", { # tools/ is excluded from the built package (.Rbuildignore), so this only runs # from a source checkout (local dev + CI). The after-install action guards # drift in CI. - generator <- testthat::test_path("..", "..", "tools", "generate-migrations.R") - skip_if_not(file.exists(generator)) - - gen_env <- new.env() - sys.source(generator, envir = gen_env) + gen_env <- migration_generator_env() registry <- gen_env$migration_registry_files( - testthat::test_path("..", "..") + test_path("..", "..") ) migrations <- gen_env$load_migrations(registry) by_fn <- stats::setNames( @@ -391,22 +386,47 @@ test_that("the generated block is in sync with the registry", { vapply(migrations, function(e) e$fn, character(1)) ) - fixture <- testthat::test_path("..", "..", "R", "migration-fixture.R") + fixture <- test_path("..", "..", "R", "migration-fixture.R") lines <- readLines(fixture, warn = FALSE) spliced <- gen_env$splice_blocks(lines, by_fn) expect_identical(spliced$lines, lines) }) +test_that("ambiguous_tags() only flags prefixes with distinct targets", { + gen_env <- migration_generator_env() + + # The old name is a prefix of the new one, so `alg` matches both -- but both + # resolve to `algorithm`, and the call is recoverable. + norm <- gen_env$normalise_migration( + "prefix_rename", + list( + old = function(graph, algo = algorithm) {}, + new = function(graph, ..., algorithm = "prpack") {}, + when = "3.0.0" + ) + ) + expect_identical(gen_env$ambiguous_tags(norm), character(0)) + + # Two recoverable names with distinct targets: the shared prefix stays + # ambiguous. + norm <- gen_env$normalise_migration( + "distinct_targets", + list( + old = function(graph, damping, directed) {}, + new = function(graph, ..., damping = 0.85, directed = TRUE) {}, + when = "3.0.0" + ) + ) + expect_identical(gen_env$ambiguous_tags(norm), "d") +}) + test_that("normalise_migration() handles head/recoverable prefix overlaps", { # Head args are matched by base R (with partial matching) before `...`. # Exact matching protects full names, so prefix overlaps are allowed; the # generator enumerates the forbidden prefixes (strict prefixes of a head # arg that also prefix a recoverable name) for the runtime guard, which # rejects them only when legacy arguments in `...` engage recovery. - generator <- testthat::test_path("..", "..", "tools", "generate-migrations.R") - skip_if_not(file.exists(generator)) - gen_env <- new.env() - sys.source(generator, envir = gen_env) + gen_env <- migration_generator_env() # Head arg `type` is a prefix of the recoverable `typeof`: allowed. The # strict prefixes of `type` all prefix `typeof` too, so they are forbidden @@ -487,10 +507,7 @@ test_that("normalise_migration() handles head/recoverable prefix overlaps", { }) test_that("the BEGIN marker may carry a trailing note", { - generator <- testthat::test_path("..", "..", "tools", "generate-migrations.R") - skip_if_not(file.exists(generator)) - gen_env <- new.env() - sys.source(generator, envir = gen_env) + gen_env <- migration_generator_env() marker <- " # BEGIN GENERATED ARG_HANDLE: foo, do not edit, see x" m <- regmatches(marker, regexec(gen_env$begin_re, marker))[[1]] @@ -616,3 +633,123 @@ test_that("unasserted lifecycle deprecations are errors in tests when opted in", # verbosity back to "warning" internally. lifecycle::expect_deprecated(indirect()) }) + +# ---- head renames ------------------------------------------------------------- +# +# `migration_fixture_rename()`: old f(graph, from, to, kind, directed), new +# f(graph, vertices, to, ..., type, directed) with `from` kept behind as a +# deprecated formal. A call naming `from` was written against the old +# signature and is matched again, whole, against it. + +test_that("an old head name binds its new name", { + lifecycle::expect_deprecated(res <- migration_fixture_rename("g", from = 1)) + expect_equal(res$vertices, 1) + expect_null(res$to) +}) + +test_that("an old head name followed by a positional value keeps the old order", { + # Under the new signature base R hands `2` to `vertices`; the old one gave it + # to `to`. + lifecycle::expect_deprecated( + res <- migration_fixture_rename("g", from = 1, 2) + ) + expect_equal( + res, + list(graph = "g", vertices = 1, to = 2, type = "out", directed = FALSE) + ) +}) + +test_that("positional old tail arguments are recovered on the same path", { + rlang::local_options(lifecycle_verbosity = "warning") + expect_snapshot(res <- migration_fixture_rename("g", from = 1, 2, "in", TRUE)) + expect_equal( + res, + list(graph = "g", vertices = 1, to = 2, type = "in", directed = TRUE) + ) +}) + +test_that("new names past `...` keep their binding next to an old head name", { + lifecycle::expect_deprecated( + res <- migration_fixture_rename("g", from = 1, 2, directed = TRUE) + ) + expect_equal(res$to, 2) + expect_true(res$directed) +}) + +test_that("an old head name is matched through a wrapper's `...`", { + wrapper <- function(...) migration_fixture_rename(...) + lifecycle::expect_deprecated(res <- wrapper("g", from = 1, 2)) + expect_equal(res$vertices, 1) + expect_equal(res$to, 2) +}) + +test_that("each argument of a re-matched call is evaluated once", { + count <- 0 + bump <- function(x) { + count <<- count + 1 + x + } + lifecycle::expect_deprecated( + migration_fixture_rename(bump("g"), from = bump(1), bump(2)) + ) + expect_equal(count, 3) +}) + +test_that("a call without an old head name takes the `...` path", { + rlang::local_options(lifecycle_verbosity = "warning") + expect_no_warning(res <- migration_fixture_rename("g", 1, 2, type = "in")) + expect_equal(res$vertices, 1) + lifecycle::expect_deprecated(res <- migration_fixture_rename("g", 1, 2, "in")) + expect_equal(res$type, "in") +}) + +test_that("a re-matched call reports what the old signature cannot take", { + rlang::local_options(lifecycle_verbosity = "quiet") + expect_snapshot(error = TRUE, { + migration_fixture_rename("g", from = 1, vertices = 2) + migration_fixture_rename("g", from = 1, 2, "in", type = "out") + migration_fixture_rename("g", from = 1, 2, "in", TRUE, 99) + migration_fixture_rename("g", from = 1, bogus = 3) + }) +}) + +test_that("a rename-only migration without `...` keeps the old order", { + # The set_vertex_attr(g, "x", index = i, v) shape. + lifecycle::expect_deprecated( + res <- migration_fixture_rename_nodots("g", "x", index = 1:3, 4:6) + ) + expect_equal(res, list(graph = "g", name = "x", vertices = 1:3, value = 4:6)) + + rlang::local_options(lifecycle_verbosity = "warning") + expect_no_warning(res <- migration_fixture_rename_nodots("g", "x", 1:3, 4:6)) + expect_equal(res$vertices, 1:3) +}) + +test_that("a replacement function keeps the old order too", { + x <- list() + lifecycle::expect_deprecated( + migration_fixture_rename(x, "a", index = 2) <- 9 + ) + expect_equal(x$a, list(vertices = 2, value = 9)) +}) + +test_that("a function that passes `...` on keeps it next to an old head name", { + lifecycle::expect_deprecated( + res <- migration_fixture_passthrough(1, from = 2, 3, extra = 4) + ) + expect_equal(res, list(x = 1, vertices = 2, to = 3, dots = list(extra = 4))) + + # Without an old head name, `...` is left alone entirely. + rlang::local_options(lifecycle_verbosity = "warning") + expect_no_warning(res <- migration_fixture_passthrough(1, 2, 3, 4, extra = 5)) + expect_equal(res$dots, list(4, extra = 5)) +}) + +test_that("a passed-on `...` that would shift is reported", { + rlang::local_options(lifecycle_verbosity = "quiet") + # The old signature gives `4` to `...`; base R has already bound it to `to`. + expect_snapshot( + migration_fixture_passthrough(1, from = 2, 3, 4), + error = TRUE + ) +}) diff --git a/tests/testthat/test-structural-properties.R b/tests/testthat/test-structural-properties.R index 4bfb08d3b9..8cac800578 100644 --- a/tests/testthat/test-structural-properties.R +++ b/tests/testthat/test-structural-properties.R @@ -1434,11 +1434,21 @@ test_that("feedback_arc_set() tail arguments and legacy positional recovery", { g <- make_ring(4, directed = TRUE) # The exact algorithm removes the cheapest edge of the single cycle. - fas <- feedback_arc_set(g, weights = c(4, 3, 2, 1), algo = "exact_ip") + fas <- feedback_arc_set(g, weights = c(4, 3, 2, 1), algorithm = "exact_ip") expect_equal(as.numeric(fas), 4) lifecycle::expect_deprecated(res <- feedback_arc_set(g, c(4, 3, 2, 1))) expect_equal(res, feedback_arc_set(g, weights = c(4, 3, 2, 1))) + + # The legacy `algo` name is recovered as `algorithm`. + lifecycle::expect_deprecated( + res_legacy <- feedback_arc_set( + g, + weights = c(4, 3, 2, 1), + algo = "exact_ip" + ) + ) + expect_equal(res_legacy, fas) }) test_that("feedback_vertex_set() tail arguments and legacy positional recovery", { @@ -1447,11 +1457,21 @@ test_that("feedback_vertex_set() tail arguments and legacy positional recovery", g <- make_ring(4, directed = TRUE) # The cheapest vertex of the single cycle is removed. - fvs <- feedback_vertex_set(g, weights = c(4, 3, 2, 1), algo = "exact_ip") + fvs <- feedback_vertex_set(g, weights = c(4, 3, 2, 1), algorithm = "exact_ip") expect_equal(as.numeric(fvs), 4) lifecycle::expect_deprecated(res <- feedback_vertex_set(g, c(4, 3, 2, 1))) expect_equal(res, feedback_vertex_set(g, weights = c(4, 3, 2, 1))) + + # The legacy `algo` name is recovered as `algorithm`. + lifecycle::expect_deprecated( + res_legacy <- feedback_vertex_set( + g, + weights = c(4, 3, 2, 1), + algo = "exact_ip" + ) + ) + expect_equal(res_legacy, fvs) }) test_that("girth() tail arguments and legacy positional recovery", { diff --git a/tools/generate-migrations.R b/tools/generate-migrations.R index 90503af666..01d5649fae 100644 --- a/tools/generate-migrations.R +++ b/tools/generate-migrations.R @@ -12,7 +12,9 @@ # # The spliced block recovers a legacy call to the pre-migration signature (after # `...` was inserted) -- both positional values and (partially) named ones -- and -# emits a single soft-deprecation. There is no handler function and no runtime +# emits a single soft-deprecation. A call that uses an old head name kept as a +# `deprecated()` formal is matched again, whole, against the old signature +# (`render_legacy_call()`). There is no handler function and no runtime # matcher: the block declares the old signature as a local `.old_signature()` and # matches `...` against it, so base R does the matching, and everything else is # written out per argument. The `lifecycle::deprecate_soft()` call sits directly @@ -258,14 +260,37 @@ normalise_migration <- function(fn, entry) { entry$old <- names(old_fmls) entry$new <- names(new_fmls) - if (sum(entry$new == "...") != 1L) { + if (sum(entry$new == "...") > 1L) { stop( "Migration `", fn, - "`: `new` must contain exactly one `...`.", + "`: `new` must contain at most one `...`.", call. = FALSE ) } + if ("..." %in% entry$old) { + stop( + "Migration `", + fn, + "`: `old` must not contain `...`.", + call. = FALSE + ) + } + entry$has_dots <- "..." %in% entry$new + + # Formals kept behind as `deprecated()` under an old name. + deprecated_fmls <- entry$new[vapply( + entry$new, + function(nm) { + nm != "..." && + nzchar(default_expr(new_fmls, nm)) && + is.call(new_fmls[[nm]]) && + length(new_fmls[[nm]]) == 1L && + (identical(new_fmls[[nm]][[1]], quote(deprecated)) || + identical(new_fmls[[nm]][[1]], quote(lifecycle::deprecated))) + }, + logical(1) + )] # Renames: an old formal whose default is a bare symbol points at its new name. renames <- character(0) @@ -293,15 +318,35 @@ normalise_migration <- function(fn, entry) { } entry$defaults <- defaults - dots_idx <- which(entry$new == "...") - entry$head <- entry$new[seq_len(dots_idx - 1L)] - entry$tail <- if (dots_idx < length(entry$new)) { - entry$new[(dots_idx + 1L):length(entry$new)] + if (entry$has_dots) { + dots_idx <- which(entry$new == "...") + entry$head <- entry$new[seq_len(dots_idx - 1L)] + entry$tail <- if (dots_idx < length(entry$new)) { + entry$new[(dots_idx + 1L):length(entry$new)] + } else { + character(0) + } } else { - character(0) + # Without `...`, a migration only renames: every formal but the deprecated + # ones is matched by base R, as the head is. + entry$head <- setdiff(entry$new, deprecated_fmls) + entry$tail <- intersect(entry$new, deprecated_fmls) } entry$new_args <- c(entry$head, entry$tail) + # Head renames: an old head slot whose name changed, with the old name kept + # behind as a `deprecated()` formal. A call using that old name was written + # against the old signature, so the block re-matches the whole call against + # it (`render_legacy_call()`). + n_aligned <- min(length(entry$old), length(entry$head)) + aligned <- seq_len(n_aligned) + is_head_rename <- entry$old[aligned] != entry$head[aligned] & + entry$old[aligned] %in% deprecated_fmls + entry$head_renames <- stats::setNames( + entry$head[aligned][is_head_rename], + entry$old[aligned][is_head_rename] + ) + if (length(entry$old) < length(entry$head)) { stop( "Migration `", @@ -325,6 +370,55 @@ normalise_migration <- function(fn, entry) { USE.NAMES = FALSE ) + # Where each old formal lands in the new API: a head slot by position, a + # recovered slot by its (possibly renamed) name. + entry$old_to_new <- stats::setNames( + c(entry$head[aligned], entry$recover_new), + entry$old + ) + + # A function that passes its `...` on keeps it: no recovery out of `...`, so + # only head renames, and the old signature must not reach past the head. + entry$passthrough_dots <- isTRUE(entry$passthrough_dots) + if (entry$passthrough_dots) { + if (!entry$has_dots) { + stop( + "Migration `", + fn, + "`: `passthrough_dots` needs `...` in `new`.", + call. = FALSE + ) + } + if (length(entry$recover_old) || length(entry$head_renames) == 0L) { + stop( + "Migration `", + fn, + "`: with `passthrough_dots`, the migration may only rename head ", + "arguments, and `old` must not be longer than the head.", + call. = FALSE + ) + } + } + + if (!entry$has_dots) { + if (length(entry$recover_old)) { + stop( + "Migration `", + fn, + "`: without `...` in `new`, `old` must not be longer than its head.", + call. = FALSE + ) + } + if (length(entry$head_renames) == 0L) { + stop( + "Migration `", + fn, + "`: without `...` in `new`, the migration must rename a head argument.", + call. = FALSE + ) + } + } + bad <- setdiff(entry$recover_new, entry$tail) if (length(bad)) { stop( @@ -436,12 +530,18 @@ quote_items <- function(x) { } # Abbreviations that were ambiguous under the pre-migration matcher: proper -# prefixes matching more than one of the old and new names at once. Base R only -# ever sees the old names in `.old_signature()`, so it would resolve some of -# these silently (e.g. `at` for `as_adjacency_matrix(attr =)`, which the -# migration renamed to `weights` while a deprecated `attr` formal remains) and -# reject the rest with a message that does not name the argument. The block -# tests the dot tags against this list first, so both keep the message they had. +# prefixes matching two or more of the old and new names that resolve to +# *different* arguments of the new API. Base R only ever sees the old names in +# `.old_signature()`, so it would resolve some of these silently (e.g. `at` for +# `as_adjacency_matrix(attr =)`, which the migration renamed to `weights` while +# a deprecated `attr` formal remains) and reject the rest with a message that +# does not name the argument. The block tests the dot tags against this list +# first, so both keep the message they had. +# +# A prefix matching several names with a single target is not ambiguous: when +# the old name is a prefix of the new one (`algo` -> `algorithm`), `alg =` +# resolved uniquely before the migration and must keep resolving, under the +# soft deprecation, afterwards. ambiguous_tags <- function(entry) { names <- entry$match_names if (length(names) == 0L) { @@ -458,8 +558,12 @@ ambiguous_tags <- function(entry) { prefixes[vapply( prefixes, function(p) { - j <- charmatch(p, names) - !is.na(j) && j == 0L + # Ambiguous only if the candidates resolve to *different* arguments of the + # new API. A rename whose old name is a prefix of the new one (`algo` -> + # `algorithm`) matches two names but one target, and `alg` must keep + # working under the soft deprecation. + hits <- startsWith(names, p) + length(unique(entry$match_to[hits])) > 1L }, logical(1) )] @@ -590,6 +694,211 @@ render_vector <- function(items) { # written out as code, it would name variables R CMD check reports as undefined # globals. render_arg_handle <- function(entry) { + legacy <- render_legacy_call(entry) + # Nothing to recover out of `...` when the old signature ends with the head + # (a head rename in a function that already had `...`). + no_dots_block <- !entry$has_dots || + entry$passthrough_dots || + (length(entry$recover_old) == 0L && length(legacy) > 0L) + if (no_dots_block) { + return(c("# fmt: skip", legacy, "}")) + } + dots <- render_dots(entry) + if (length(legacy) == 0L) { + return(c("# fmt: skip", dots)) + } + # The legacy call is matched against the whole old signature, `...` included, + # so the recovery of `...` alone only runs when no old head name was used. + c("# fmt: skip", legacy, paste0("} else ", dots[[1]]), dots[-1]) +} + +# The opening part of the block for a migration that renamed head arguments: +# a call using an old head name (passed by that name, so it binds the retained +# `deprecated()` formal) was written against the old signature, and its +# positional values may sit in the wrong new formals -- in +# `set_vertex_attr(g, "x", index = i, v)`, `v` filled `vertices` where the old +# signature gave it to `value`. Such a call is matched again, whole, against the +# old signature: `sys.call()` with a local `.legacy_signature()` at its head, evaluated in +# the caller's frame, so `...` forwarded by a wrapper expands there and every +# argument becomes a fresh promise evaluated at most once (the host's own +# promises are never forced). Each old formal the call supplied is then bound to +# its new name, lazily, as `render_dots()` does. +# +# Named arguments the old signature did not know -- new names past `...` -- land +# in its own `...` and keep the binding the host gave them; anything else there +# is reported, as is a new name supplied next to the old name it replaces. +# +# Returns `character(0)` for a migration without head renames, or the lines up +# to (not including) the closing brace. +render_legacy_call <- function(entry) { + renames <- entry$head_renames + if (length(renames) == 0L) { + return(character(0)) + } + fn <- entry$fn + old <- entry$old + to <- entry$old_to_new + tail_old <- entry$recover_old + + lines <- c( + paste0( + "if (", + paste0("!base::missing(", names(renames), ")", collapse = " || "), + ") {" + ), + paste0( + " # Pre-", + entry$when, + " signature: ", + fn, + "(", + paste(old, collapse = ", "), + ")" + ), + paste0( + " .legacy_signature <- function(", + paste(c(old, "..."), collapse = ", "), + ") {" + ), + " base::list(env = base::environment(), n_extra = ...length(), extra = base::as.character(base::names(base::substitute(...()))), recovered = base::c(", + paste0( + " if (!base::missing(", + old, + ")) base::c(", + to, + " = \"", + old, + "\")", + c(rep(",", length(old) - 1L), "") + ), + " ))", + " }", + " .arg_call <- base::sys.call()", + " .arg_call[[1L]] <- .legacy_signature", + " .arg_handle <- base::eval(.arg_call, base::parent.frame())", + " .arg_extra <- .arg_handle$extra", + if (entry$passthrough_dots) { + " if (base::length(.arg_extra) < .arg_handle$n_extra) .arg_extra <- base::rep(\"\", .arg_handle$n_extra)" + } else { + paste0( + " if (.arg_handle$n_extra > base::sum(base::nzchar(.arg_extra))) cli::cli_abort(\"Too many arguments passed to {.fn ", + fn, + "}.\")" + ) + }, + paste0( + " .arg_explicit <- ", + render_vector(quote_items(entry$new_args)), + "[base::pmatch(.arg_extra, ", + render_vector(quote_items(entry$new_args)), + ", duplicates.ok = TRUE)]" + ), + if (entry$passthrough_dots) { + # What the old signature left in its `...` is what the function passes + # on. The host's own `...` holds the same values only if as many landed + # there; a long positional call can shift one more in or out. + c( + paste0( + " if (base::sum(base::is.na(.arg_explicit)) != ...length()) cli::cli_abort(base::c(\"Can't match this call to the pre-", + entry$when, + " signature of {.fn ", + fn, + "}.\", i = \"Name its arguments.\"))" + ), + " .arg_explicit <- .arg_explicit[!base::is.na(.arg_explicit)]" + ) + } else { + paste0( + " if (base::anyNA(.arg_explicit)) cli::cli_abort(\"Unexpected argument passed to {.fn ", + fn, + "}: {.arg {(.arg_extra[base::is.na(.arg_explicit)])}}.\")" + ) + }, + " .arg_recovered <- .arg_handle$recovered", + " .arg_names <- base::names(.arg_recovered)", + " .arg_conflict <- base::intersect(.arg_names, .arg_explicit)", + " if (base::length(.arg_conflict) > 0L) {", + " .arg_conflict <- .arg_conflict[[1L]]", + " .arg_via <- .arg_recovered[[.arg_conflict]]", + paste0( + " cli::cli_abort(base::c(\"Argument {.arg {(.arg_conflict)}} of {.fn ", + fn, + "} was supplied more than once.\", i = if (base::is.element(.arg_via, base::names(.arg_call))) \"It was also supplied via its legacy name {.arg {(.arg_via)}}.\" else \"It was also passed by position, as {.arg {(.arg_via)}} of the old signature.\"))" + ), + " }", + " for (.arg_i in base::seq_along(.arg_recovered)) base::do.call(base::delayedAssign, base::list(.arg_names[[.arg_i]], base::call(\"[[\", .arg_handle$env, .arg_recovered[[.arg_i]]), base::baseenv(), base::environment()))", + paste0( + " if (!base::missing(", + names(renames), + ")) lifecycle::deprecate_soft(\"", + entry$when, + "\", \"", + spec_name(fn), + "(", + names(renames), + " = )\", \"", + spec_name(fn), + "(", + renames, + " = )\")" + ) + ) + if (length(tail_old) == 0L) { + return(lines) + } + # Old arguments past the head that were passed positionally, abbreviated or + # under a name the migration changed get the same deprecation as in + # `render_dots()`. + c( + lines, + paste0( + " .arg_tail <- .arg_recovered[base::is.element(.arg_recovered, ", + render_vector(quote_items(tail_old)), + ")]" + ), + " .arg_tail <- .arg_tail[!base::is.element(.arg_tail, base::names(.arg_call)) | base::names(.arg_tail) != .arg_tail]", + " if (base::length(.arg_tail) > 0L) {", + " .arg_shown <- base::ifelse(base::is.element(.arg_tail, base::names(.arg_call)), base::paste0(.arg_tail, \" = \"), .arg_tail)", + " lifecycle::deprecate_soft(", + paste0(" \"", entry$when, "\","), + paste0( + " what = base::I(\"Calling `", + fn, + "()` with positional, abbreviated or renamed arguments\")," + ), + " details = base::c(", + paste0( + " i = base::paste0(\"Detected call: ", + fn, + "(\", base::paste(", + render_vector(c(quote_items(entry$head), ".arg_shown")), + ", collapse = \", \"), \")\")," + ), + paste0( + " i = base::paste0(\"Use instead: ", + fn, + "(\", base::paste(", + render_vector(c( + quote_items(entry$head), + "base::paste0(base::names(.arg_tail), \" = \")" + )), + ", collapse = \", \"), \")\")" + ), + " )", + " )", + " }" + ) +} + +# A function name as lifecycle parses it in a `what`/`with` spec: a +# replacement function is backquoted (`"\`vertex_attr<-\`(index = )"`). +spec_name <- function(fn) { + if (make.names(fn) == fn) fn else paste0("`", fn, "`") +} + +# The recovery of legacy arguments out of `...`: see the comment above +# `render_arg_handle()`. +render_dots <- function(entry) { fn <- entry$fn old <- entry$recover_old new <- entry$recover_new @@ -656,7 +965,6 @@ render_arg_handle <- function(entry) { } c( - "# fmt: skip", "if (...length() > 0L) {", guards, paste0( @@ -747,7 +1055,8 @@ render_arg_handle <- function(entry) { # The function name is captured up to the first non-identifier char, so the # BEGIN marker may carry a trailing note, e.g. # # BEGIN GENERATED ARG_HANDLE: foo, do not edit, see tools/generate-migrations.R -begin_re <- "^([\t ]*)#\\s*BEGIN GENERATED ARG_HANDLE:\\s*([A-Za-z0-9._]+)" +# A replacement function keeps its `<-` (`vertex_attr<-`). +begin_re <- "^([\t ]*)#\\s*BEGIN GENERATED ARG_HANDLE:\\s*([A-Za-z0-9._]+(<-)?)" end_re <- "^[\t ]*#\\s*END GENERATED ARG_HANDLE\\s*$" # Replace the content between each ARG_HANDLE marker pair with freshly rendered diff --git a/tools/migrations/README.md b/tools/migrations/README.md index 6dff797ebe..92b6fc67bf 100644 --- a/tools/migrations/README.md +++ b/tools/migrations/README.md @@ -79,10 +79,12 @@ migrations <- list( e.g. `c = c_renamed` means the old `c` argument is the new `c_renamed`. Formals without a symbol default keep their name. -- `new` --- The post-migration signature. Must contain exactly one `...`. The +- `new` --- The post-migration signature. Contains at most one `...`. The non-`...` formals are the new-API arguments, in order; their defaults become the function's defaults and the values the conflict check compares - against. + against. A formal with the default `deprecated()` keeps an old name behind + (see [Head renames](#head-renames)). Without `...`, the migration only + renames head arguments, and `old` must be no longer than the head. - `when` --- The version string passed to `lifecycle::deprecate_soft(when = )`. @@ -105,3 +107,31 @@ bites). To reorder or drop an argument, place it *after* `...` in `new`. It then becomes keyword-only and is recovered by (partial) name rather than by position, side-stepping the position math entirely. + +## Head renames + +A head argument can be renamed in its slot, +with the old name kept behind as a `deprecated()` formal, +usually after `...`: + +```r +set_vertex_attr = list( + old = function(graph, name, index, value) {}, + new = function(graph, name, vertices = NULL, value, index = deprecated()) {} +) +``` + +The generator reads the rename off the two signatures: +slot `index` of `old` holds `vertices` in `new`, +and `index` survives as a `deprecated()` formal. +A call that passes the old name was written against the old signature, +so the block matches the whole call against it again +and binds each value to its new name, with one deprecation per old name. +That keeps a legacy call like `set_vertex_attr(g, "x", index = i, v)` working, +where base R alone would hand `v` to `vertices` instead of `value`. +Do not write a bridge for the old name by hand. + +A function that passes its `...` on (to `FUN`, say) +declares `passthrough_dots = TRUE` in its entry. +It then gets no recovery out of `...`, so the migration may only rename head arguments, +and named arguments the old signature did not know are left to `...`. diff --git a/tools/migrations/centrality.R b/tools/migrations/centrality.R index 394818faff..4ccc6f2a61 100644 --- a/tools/migrations/centrality.R +++ b/tools/migrations/centrality.R @@ -88,7 +88,7 @@ migrations <- list( page_rank = list( old = function( graph, - algo, + algo = algorithm, vids, directed, damping, @@ -99,7 +99,7 @@ migrations <- list( new = function( graph, ..., - algo = c("prpack", "arpack"), + algorithm = c("prpack", "arpack"), vids = NULL, directed = TRUE, damping = 0.85, diff --git a/tools/migrations/fixture.R b/tools/migrations/fixture.R index 8a9bde6839..b679a8cfef 100644 --- a/tools/migrations/fixture.R +++ b/tools/migrations/fixture.R @@ -6,8 +6,9 @@ migrations <- list( # Exercises the generator end-to-end without migrating a real function. The # arg names are chosen to cover every recovery path: two renames (`weight -> # weights`, `kind -> type`), a surviving arg (`directed`), unique - # abbreviations (`kin`, `dir`) and an ambiguous one (`weig` matches both - # `weight` and `weights`). Head args (`graph`, `n`) stay before `...` and are + # abbreviations (`kin`, `dir`) and one that matches both the old `weight` and + # the new `weights` (`weig`) but resolves unambiguously because they are the + # same argument. Head args (`graph`, `n`) stay before `...` and are # matched by base R, including abbreviations like `gr =`. Consumed by # tests/testthat/test-migration-fixture.R. migration_fixture = list( @@ -58,5 +59,61 @@ migrations <- list( attr = deprecated() ) {}, when = "3.0.0" + ), + # Exercises head renames: `from` was renamed to `vertices` in its head slot + # and stays behind as a `deprecated()` formal. A legacy call naming `from` + # and passing the next old argument positionally, + # `migration_fixture_rename(g, from = 1, 2)`, gave `2` to `to` -- under the new + # signature base R hands it to `vertices`, so the block re-matches the whole + # call against the old signature. `kind -> type` and `directed` cover the + # recovered tail on the same path. + migration_fixture_rename = list( + old = function(graph, from, to, kind = type, directed) {}, + new = function( + graph, + vertices = NULL, + to = NULL, + ..., + type = "out", + directed = FALSE, + from = deprecated() + ) {}, + when = "3.0.0" + ), + + # Exercises a rename-only migration without `...`, the set_vertex_attr() + # shape: `index` became `vertices`, and `value` followed it positionally. + migration_fixture_rename_nodots = list( + old = function(graph, name, index, value) {}, + new = function( + graph, + name, + vertices = NULL, + value, + index = deprecated() + ) {}, + when = "3.0.0" + ), + + # The same for a replacement function, whose marker keeps the `<-`. + `migration_fixture_rename<-` = list( + old = function(graph, name, index, value) {}, + new = function( + graph, + name, + vertices = NULL, + index = deprecated(), + value + ) {}, + when = "3.0.0" + ), + # Exercises a head rename in a function that passes its `...` on (the + # fit_power_law() and local_scan() shape): no recovery out of `...`, and + # named arguments the old signature did not know are left to `...`. + migration_fixture_passthrough = list( + old = function(x, from, to) {}, + new = function(x, vertices = NULL, to = NULL, ..., from = deprecated()) {}, + passthrough_dots = TRUE, + when = "3.0.0" ) ) diff --git a/tools/migrations/structural-properties.R b/tools/migrations/structural-properties.R index bcd7229fbb..26dc6b78ac 100644 --- a/tools/migrations/structural-properties.R +++ b/tools/migrations/structural-properties.R @@ -218,23 +218,23 @@ migrations <- list( ), feedback_arc_set = list( - old = function(graph, weights, algo) {}, + old = function(graph, weights, algo = algorithm) {}, new = function( graph, ..., weights = NULL, - algo = c("approx_eades", "exact_ip") + algorithm = c("approx_eades", "exact_ip") ) {}, when = "3.0.0" ), feedback_vertex_set = list( - old = function(graph, weights, algo) {}, + old = function(graph, weights, algo = algorithm) {}, new = function( graph, ..., weights = NULL, - algo = c("exact_ip") + algorithm = c("exact_ip") ) {}, when = "3.0.0" ),