From 9d1e64deba85bbe5aa2c69406b6b4e7ce28d0d71 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 17 Aug 2026 17:49:27 +0000 Subject: [PATCH 01/12] refactor: Rename `algo` to `algorithm` in `page_rank()`, `feedback_arc_set()` and `feedback_vertex_set()` (#2788, #526) The legacy `algo` spelling is recovered by the generated ARG_HANDLE blocks and soft-deprecated; abbreviations of both spellings are guarded as ambiguous. Co-Authored-By: Claude Fable 5 Claude-Session: https://claude.ai/code/session_01RTPj4qNv2FWui6etixZeuR --- R/centrality.R | 18 +++++++------ R/structural-properties.R | 30 ++++++++++++--------- man/feedback_arc_set.Rd | 6 ++--- man/feedback_vertex_set.Rd | 4 +-- man/page.rank.Rd | 10 ++----- man/page_rank.Rd | 4 +-- tests/testthat/_snaps/centrality.md | 10 +++++++ tests/testthat/test-centrality.R | 15 ++++++++--- tests/testthat/test-structural-properties.R | 24 +++++++++++++++-- tools/migrations/centrality.R | 4 +-- tools/migrations/structural-properties.R | 8 +++--- 11 files changed, 86 insertions(+), 47 deletions(-) diff --git a/R/centrality.R b/R/centrality.R index aa62926c4b..49d184830a 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, @@ -1884,7 +1886,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 @@ -1953,7 +1955,7 @@ hub_score <- function( page_rank <- function( graph, ..., - algo = c("prpack", "arpack"), + algorithm = c("prpack", "arpack"), vids = NULL, directed = TRUE, damping = 0.85, @@ -1964,7 +1966,7 @@ page_rank <- function( # BEGIN GENERATED ARG_HANDLE: page_rank, do not edit, see tools/generate-migrations.R # fmt: skip if (...length() > 0L) { - .arg_ambiguous <- base::intersect(base::names(base::substitute(...())), base::c("d")) + .arg_ambiguous <- base::intersect(base::names(base::substitute(...())), base::c("a", "al", "alg", "d")) if (base::length(.arg_ambiguous) > 0L) cli::cli_abort("Argument {.arg {(.arg_ambiguous[[1L]])}} matches multiple arguments of {.fn page_rank}.") # Pre-3.0.0 signature: page_rank(graph, algo, vids, directed, damping, personalized, weights, options) .old_signature <- function(algo, vids, directed, damping, personalized, weights, options, ...) { @@ -1975,7 +1977,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::c( - if (!base::missing(algo)) base::list(algo = algo), + if (!base::missing(algo)) base::list(algorithm = algo), if (!base::missing(vids)) base::list(vids = vids), if (!base::missing(directed)) base::list(directed = directed), if (!base::missing(damping)) base::list(damping = damping), @@ -1988,7 +1990,7 @@ page_rank <- function( if (base::length(.arg_handle) > 0L) { .arg_names <- base::names(.arg_handle) .arg_conflict <- base::intersect(.arg_names, base::c( - if (!base::missing(algo)) "algo", + if (!base::missing(algorithm)) "algorithm", if (!base::missing(vids)) "vids", if (!base::missing(directed)) "directed", if (!base::missing(damping)) "damping", @@ -2002,7 +2004,7 @@ page_rank <- function( "3.0.0", what = base::I("Calling `page_rank()` with positional or abbreviated arguments"), details = base::c( - i = base::paste0("Detected call: page_rank(", base::paste(base::c("graph", .arg_names), collapse = ", "), ")"), + i = base::paste0("Detected call: page_rank(", base::paste(base::c("graph", base::c(algorithm = "algo", vids = "vids", directed = "directed", damping = "damping", personalized = "personalized", weights = "weights", options = "options")[.arg_names]), collapse = ", "), ")"), i = base::paste0("Use instead: page_rank(", base::paste(base::c("graph", base::paste0(.arg_names, " = ")), collapse = ", "), ")") ) ) @@ -2016,7 +2018,7 @@ page_rank <- function( personalized_pagerank_impl( graph = graph, - algo = algo, + algo = algorithm, vids = vids, directed = directed, damping = damping, diff --git a/R/structural-properties.R b/R/structural-properties.R index 6206c631c4..59e905a16e 100644 --- a/R/structural-properties.R +++ b/R/structural-properties.R @@ -3216,7 +3216,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 @@ -3236,16 +3236,18 @@ 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 if (...length() > 0L) { + .arg_ambiguous <- base::intersect(base::names(base::substitute(...())), base::c("a", "al", "alg")) + if (base::length(.arg_ambiguous) > 0L) cli::cli_abort("Argument {.arg {(.arg_ambiguous[[1L]])}} matches multiple arguments of {.fn feedback_arc_set}.") # Pre-3.0.0 signature: feedback_arc_set(graph, weights, algo) .old_signature <- function(weights, algo, ...) { if (...length() > 0L) { @@ -3256,7 +3258,7 @@ feedback_arc_set <- function( } base::c( if (!base::missing(weights)) base::list(weights = weights), - if (!base::missing(algo)) base::list(algo = algo) + if (!base::missing(algo)) base::list(algorithm = algo) ) } .arg_handle <- .old_signature(...) @@ -3264,7 +3266,7 @@ feedback_arc_set <- function( .arg_names <- base::names(.arg_handle) .arg_conflict <- base::intersect(.arg_names, base::c( if (!base::missing(weights)) "weights", - if (!base::missing(algo)) "algo" + 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)}}.")) base::list2env(.arg_handle, base::environment()) @@ -3272,7 +3274,7 @@ feedback_arc_set <- function( "3.0.0", what = base::I("Calling `feedback_arc_set()` with positional or abbreviated arguments"), details = base::c( - i = base::paste0("Detected call: feedback_arc_set(", base::paste(base::c("graph", .arg_names), collapse = ", "), ")"), + i = base::paste0("Detected call: feedback_arc_set(", base::paste(base::c("graph", base::c(weights = "weights", algorithm = "algo")[.arg_names]), collapse = ", "), ")"), i = base::paste0("Use instead: feedback_arc_set(", base::paste(base::c("graph", base::paste0(.arg_names, " = ")), collapse = ", "), ")") ) ) @@ -3283,7 +3285,7 @@ feedback_arc_set <- function( feedback_arc_set_impl( graph = graph, weights = weights, - algo = algo + algo = algorithm ) } @@ -3303,7 +3305,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 @@ -3320,11 +3322,13 @@ 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 if (...length() > 0L) { + .arg_ambiguous <- base::intersect(base::names(base::substitute(...())), base::c("a", "al", "alg")) + if (base::length(.arg_ambiguous) > 0L) cli::cli_abort("Argument {.arg {(.arg_ambiguous[[1L]])}} matches multiple arguments of {.fn feedback_vertex_set}.") # Pre-3.0.0 signature: feedback_vertex_set(graph, weights, algo) .old_signature <- function(weights, algo, ...) { if (...length() > 0L) { @@ -3335,7 +3339,7 @@ feedback_vertex_set <- function( } base::c( if (!base::missing(weights)) base::list(weights = weights), - if (!base::missing(algo)) base::list(algo = algo) + if (!base::missing(algo)) base::list(algorithm = algo) ) } .arg_handle <- .old_signature(...) @@ -3343,7 +3347,7 @@ feedback_vertex_set <- function( .arg_names <- base::names(.arg_handle) .arg_conflict <- base::intersect(.arg_names, base::c( if (!base::missing(weights)) "weights", - if (!base::missing(algo)) "algo" + 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)}}.")) base::list2env(.arg_handle, base::environment()) @@ -3351,7 +3355,7 @@ feedback_vertex_set <- function( "3.0.0", what = base::I("Calling `feedback_vertex_set()` with positional or abbreviated arguments"), details = base::c( - i = base::paste0("Detected call: feedback_vertex_set(", base::paste(base::c("graph", .arg_names), collapse = ", "), ")"), + i = base::paste0("Detected call: feedback_vertex_set(", base::paste(base::c("graph", base::c(weights = "weights", algorithm = "algo")[.arg_names]), collapse = ", "), ")"), i = base::paste0("Use instead: feedback_vertex_set(", base::paste(base::c("graph", base::paste0(.arg_names, " = ")), collapse = ", "), ")") ) ) @@ -3362,7 +3366,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 4e243de763..b356af260c 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 0c81f6fdfc..41839e26df 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 58937b15df..c205f129a6 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..fd6a1913e6 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 or abbreviated 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/test-centrality.R b/tests/testthat/test-centrality.R index b7abe50bec..240ac2ab84 100644 --- a/tests/testthat/test-centrality.R +++ b/tests/testthat/test-centrality.R @@ -1074,12 +1074,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, @@ -1106,7 +1106,16 @@ 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( + res_legacy <- page_rank(star, algo = "prpack") + ) + expect_identical(res_legacy, page_rank(star, algorithm = "prpack")) }) test_that("strength() covers migrated tail args and positional recovery", { diff --git a/tests/testthat/test-structural-properties.R b/tests/testthat/test-structural-properties.R index 594d7ec0bf..f17b5e1f4a 100644 --- a/tests/testthat/test-structural-properties.R +++ b/tests/testthat/test-structural-properties.R @@ -1398,11 +1398,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", { @@ -1411,11 +1421,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/migrations/centrality.R b/tools/migrations/centrality.R index 4e6c02ac37..09b62a0fcb 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/structural-properties.R b/tools/migrations/structural-properties.R index b7b781964f..b0581d1290 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" ), From d010bfda11a2e6ac2de38fc743e52c9b18804f75 Mon Sep 17 00:00:00 2001 From: David Schoch Date: Thu, 3 Sep 2026 14:40:55 +0200 Subject: [PATCH 02/12] fix(migrations): don't treat a prefix-rename abbreviation as ambiguous `ambiguous_tags()` charmatched a candidate prefix against `match_names`, so any rename whose old name is a prefix of the new one (`algo` -> `algorithm`) flagged `a`, `al` and `alg` as ambiguous even though both candidates resolve to the same argument. Those calls used to work with a soft deprecation and hard-errored instead, with a message that was factually wrong. Dedupe on `match_to` so a prefix is ambiguous only when the names it matches resolve to different arguments of the new API. Regenerating narrows the `page_rank()` guard to `d` (`damping` vs `directed`, a real ambiguity), drops the guards from `feedback_arc_set()` and `feedback_vertex_set()` entirely, and also drops the same latent false positive from `migration_fixture()` (`weight` -> `weights`). `migration_fixture_shadow()` keeps rejecting `at =`, where the two candidates really do have distinct targets. Co-Authored-By: Claude Opus 5 (1M context) --- R/centrality.R | 2 +- R/migration-fixture.R | 2 -- R/structural-properties.R | 4 --- tests/testthat/_snaps/migration-fixture.md | 34 +++++++++++++----- tests/testthat/test-centrality.R | 9 +++++ tests/testthat/test-migration-fixture.R | 42 ++++++++++++++++++---- tools/generate-migrations.R | 26 +++++++++----- tools/migrations/fixture.R | 5 +-- 8 files changed, 93 insertions(+), 31 deletions(-) diff --git a/R/centrality.R b/R/centrality.R index 49d184830a..d51fbdea84 100644 --- a/R/centrality.R +++ b/R/centrality.R @@ -1966,7 +1966,7 @@ page_rank <- function( # BEGIN GENERATED ARG_HANDLE: page_rank, do not edit, see tools/generate-migrations.R # fmt: skip if (...length() > 0L) { - .arg_ambiguous <- base::intersect(base::names(base::substitute(...())), base::c("a", "al", "alg", "d")) + .arg_ambiguous <- base::intersect(base::names(base::substitute(...())), base::c("d")) if (base::length(.arg_ambiguous) > 0L) cli::cli_abort("Argument {.arg {(.arg_ambiguous[[1L]])}} matches multiple arguments of {.fn page_rank}.") # Pre-3.0.0 signature: page_rank(graph, algo, vids, directed, damping, personalized, weights, options) .old_signature <- function(algo, vids, directed, damping, personalized, weights, options, ...) { diff --git a/R/migration-fixture.R b/R/migration-fixture.R index 9d9de569d2..1ce06ae04a 100644 --- a/R/migration-fixture.R +++ b/R/migration-fixture.R @@ -20,8 +20,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) { diff --git a/R/structural-properties.R b/R/structural-properties.R index 59e905a16e..4998b91a1c 100644 --- a/R/structural-properties.R +++ b/R/structural-properties.R @@ -3246,8 +3246,6 @@ feedback_arc_set <- function( # BEGIN GENERATED ARG_HANDLE: feedback_arc_set, do not edit, see tools/generate-migrations.R # fmt: skip if (...length() > 0L) { - .arg_ambiguous <- base::intersect(base::names(base::substitute(...())), base::c("a", "al", "alg")) - if (base::length(.arg_ambiguous) > 0L) cli::cli_abort("Argument {.arg {(.arg_ambiguous[[1L]])}} matches multiple arguments of {.fn feedback_arc_set}.") # Pre-3.0.0 signature: feedback_arc_set(graph, weights, algo) .old_signature <- function(weights, algo, ...) { if (...length() > 0L) { @@ -3327,8 +3325,6 @@ feedback_vertex_set <- function( # BEGIN GENERATED ARG_HANDLE: feedback_vertex_set, do not edit, see tools/generate-migrations.R # fmt: skip if (...length() > 0L) { - .arg_ambiguous <- base::intersect(base::names(base::substitute(...())), base::c("a", "al", "alg")) - if (base::length(.arg_ambiguous) > 0L) cli::cli_abort("Argument {.arg {(.arg_ambiguous[[1L]])}} matches multiple arguments of {.fn feedback_vertex_set}.") # Pre-3.0.0 signature: feedback_vertex_set(graph, weights, algo) .old_signature <- function(weights, algo, ...) { if (...length() > 0L) { diff --git a/tests/testthat/_snaps/migration-fixture.md b/tests/testthat/_snaps/migration-fixture.md index a801fbf30f..2f5a00b435 100644 --- a/tests/testthat/_snaps/migration-fixture.md +++ b/tests/testthat/_snaps/migration-fixture.md @@ -322,6 +322,32 @@ [1] TRUE +--- + + Code + migration_fixture("g", 5, weig = 1) + Condition + Warning: + Calling `migration_fixture()` with positional or abbreviated arguments was deprecated in igraph 3.0.0. + i Detected call: migration_fixture(graph, n, weight) + 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 @@ -350,14 +376,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 diff --git a/tests/testthat/test-centrality.R b/tests/testthat/test-centrality.R index 240ac2ab84..b1b1c26c95 100644 --- a/tests/testthat/test-centrality.R +++ b/tests/testthat/test-centrality.R @@ -1116,6 +1116,15 @@ test_that("page_rank(algo = ) is deprecated but still works", { 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-migration-fixture.R b/tests/testthat/test-migration-fixture.R index 7d9f4f82d4..fd6c70d074 100644 --- a/tests/testthat/test-migration-fixture.R +++ b/tests/testthat/test-migration-fixture.R @@ -275,17 +275,16 @@ test_that("recovery deprecation messages", { expect_snapshot(migration_fixture("g", 5, kind = "in")) expect_snapshot(migration_fixture("g", 5, kin = "in")) expect_snapshot(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(migration_fixture("g", 5, weig = 1)) # mixed: a positional value and a named abbreviation in the same call expect_snapshot(migration_fixture("g", 5, 1:3, dir = TRUE)) }) 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) @@ -317,6 +316,37 @@ test_that("the generated block is in sync with the registry", { expect_identical(spliced$lines, lines) }) +test_that("ambiguous_tags() only flags prefixes with distinct targets", { + generator <- testthat::test_path("..", "..", "tools", "generate-migrations.R") + skip_if_not(file.exists(generator)) + gen_env <- new.env() + sys.source(generator, envir = gen_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 diff --git a/tools/generate-migrations.R b/tools/generate-migrations.R index c67fb4ea27..1c74aaf33e 100644 --- a/tools/generate-migrations.R +++ b/tools/generate-migrations.R @@ -436,12 +436,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 +464,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) )] diff --git a/tools/migrations/fixture.R b/tools/migrations/fixture.R index 8a9bde6839..33aa51ec7e 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( From b44ae031adf3b6a5a5680a2ebbd01156a17389f1 Mon Sep 17 00:00:00 2001 From: David Schoch Date: Thu, 15 Oct 2026 21:07:43 +0200 Subject: [PATCH 03/12] chore: Regenerate migration blocks and use `expect_snapshot_igraph()` after merging main Part of #2788. Co-Authored-By: Claude Opus 5.5 (1M context) --- R/community.R | 2 -- R/conversion.R | 2 -- R/operators.R | 2 -- R/simple.R | 2 -- tests/testthat/test-centrality.R | 2 +- 5 files changed, 1 insertion(+), 9 deletions(-) diff --git a/R/community.R b/R/community.R index b5d355a76c..8f1b9a53e4 100644 --- a/R/community.R +++ b/R/community.R @@ -3660,8 +3660,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 c89f89fb15..a53aaf3a10 100644 --- a/R/conversion.R +++ b/R/conversion.R @@ -806,8 +806,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/operators.R b/R/operators.R index 7a2187b772..551d39e280 100644 --- a/R/operators.R +++ b/R/operators.R @@ -1029,8 +1029,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 8ea5ebcc21..92d3a56e0c 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/tests/testthat/test-centrality.R b/tests/testthat/test-centrality.R index 46b1f8e3a6..fc08340284 100644 --- a/tests/testthat/test-centrality.R +++ b/tests/testthat/test-centrality.R @@ -1112,7 +1112,7 @@ test_that("page_rank() covers migrated tail args and positional recovery", { test_that("page_rank(algo = ) is deprecated but still works", { rlang::local_options(lifecycle_verbosity = "warning") star <- make_star(10, mode = "undirected") - expect_snapshot( + expect_snapshot_igraph( res_legacy <- page_rank(star, algo = "prpack") ) expect_identical(res_legacy, page_rank(star, algorithm = "prpack")) From d659e07527f58c7b4965fe90d6dd3bbb8355faed Mon Sep 17 00:00:00 2001 From: David Schoch Date: Wed, 2 Dec 2026 19:42:00 +0100 Subject: [PATCH 04/12] test: Use `test_path()` without the `testthat::` prefix Co-Authored-By: Claude Opus 5.5 --- tests/testthat/test-migration-fixture.R | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/testthat/test-migration-fixture.R b/tests/testthat/test-migration-fixture.R index 8475516950..cfcac0d7d7 100644 --- a/tests/testthat/test-migration-fixture.R +++ b/tests/testthat/test-migration-fixture.R @@ -351,13 +351,13 @@ 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") + generator <- test_path("..", "..", "tools", "generate-migrations.R") skip_if_not(file.exists(generator)) gen_env <- new.env() sys.source(generator, envir = gen_env) registry <- gen_env$migration_registry_files( - testthat::test_path("..", "..") + test_path("..", "..") ) migrations <- gen_env$load_migrations(registry) by_fn <- stats::setNames( @@ -365,14 +365,14 @@ 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", { - generator <- testthat::test_path("..", "..", "tools", "generate-migrations.R") + generator <- test_path("..", "..", "tools", "generate-migrations.R") skip_if_not(file.exists(generator)) gen_env <- new.env() sys.source(generator, envir = gen_env) @@ -408,7 +408,7 @@ test_that("normalise_migration() handles head/recoverable prefix overlaps", { # 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") + generator <- test_path("..", "..", "tools", "generate-migrations.R") skip_if_not(file.exists(generator)) gen_env <- new.env() sys.source(generator, envir = gen_env) @@ -492,7 +492,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") + generator <- test_path("..", "..", "tools", "generate-migrations.R") skip_if_not(file.exists(generator)) gen_env <- new.env() sys.source(generator, envir = gen_env) From 3659ea47ce910dc068ee34c703a3326d32bd9bdb Mon Sep 17 00:00:00 2001 From: David Schoch Date: Wed, 2 Dec 2026 19:56:26 +0100 Subject: [PATCH 05/12] test: Source the migration generator through one helper The generator-level tests repeated the same lookup, skip and `sys.source()` of tools/generate-migrations.R; `migration_generator_env()` in helper-migrations.R now does that once. Co-Authored-By: Claude Opus 5.5 --- tests/testthat/helper-migrations.R | 21 ++++++++++++++++++++- tests/testthat/test-migration-fixture.R | 21 ++++----------------- 2 files changed, 24 insertions(+), 18 deletions(-) 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-migration-fixture.R b/tests/testthat/test-migration-fixture.R index cfcac0d7d7..eeea5ff0ec 100644 --- a/tests/testthat/test-migration-fixture.R +++ b/tests/testthat/test-migration-fixture.R @@ -351,11 +351,7 @@ 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 <- 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( test_path("..", "..") ) @@ -372,10 +368,7 @@ test_that("the generated block is in sync with the registry", { }) test_that("ambiguous_tags() only flags prefixes with distinct targets", { - generator <- 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() # The old name is a prefix of the new one, so `alg` matches both -- but both # resolve to `algorithm`, and the call is recoverable. @@ -408,10 +401,7 @@ test_that("normalise_migration() handles head/recoverable prefix overlaps", { # 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 <- 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 @@ -492,10 +482,7 @@ test_that("normalise_migration() handles head/recoverable prefix overlaps", { }) test_that("the BEGIN marker may carry a trailing note", { - generator <- 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]] From 767c882b45f4e3c95ee6d2362365a1db84be7b80 Mon Sep 17 00:00:00 2001 From: David Schoch Date: Thu, 3 Dec 2026 15:11:38 +0100 Subject: [PATCH 06/12] feat: Let the migration generator re-match calls that use an old head name A head argument renamed in its slot keeps its old name as a `deprecated()` formal. A call that passes that name was written against the old signature, but base R binds its later positional values against the new one: `set_vertex_attr(g, "x", index = i, v)` hands `v` to `vertices`. The generator now reads such renames off the registry and emits a block that matches the whole call against the old signature again, binds each value to its new name lazily, and deprecates each old name, so hand-written bridges are no longer needed. Registry entries may also omit `...` (rename-only migrations) and name replacement functions. Co-Authored-By: Claude Opus 5.5 --- R/migration-fixture.R | 185 +++++++++++++++ tests/testthat/_snaps/migration-fixture.md | 31 +++ tests/testthat/test-generate-migrations.R | 72 ++++++ tests/testthat/test-migration-fixture.R | 98 ++++++++ tools/generate-migrations.R | 257 ++++++++++++++++++++- tools/migrations/README.md | 29 ++- tools/migrations/fixture.R | 47 ++++ 7 files changed, 707 insertions(+), 12 deletions(-) diff --git a/R/migration-fixture.R b/R/migration-fixture.R index 149e3f4c6c..e4168c73e9 100644 --- a/R/migration-fixture.R +++ b/R/migration-fixture.R @@ -207,3 +207,188 @@ 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) + .old_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]] <- .old_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) 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())) + 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) { + 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("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) + .old_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]] <- .old_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) cli::cli_abort(base::c("Argument {.arg {(.arg_conflict)}} of {.fn migration_fixture_rename_nodots} 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())) + 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, + value, + index = deprecated() +) { + # 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) + .old_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]] <- .old_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) 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())) + 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 +} diff --git a/tests/testthat/_snaps/migration-fixture.md b/tests/testthat/_snaps/migration-fixture.md index 55fee56d5b..c8d27206eb 100644 --- a/tests/testthat/_snaps/migration-fixture.md +++ b/tests/testthat/_snaps/migration-fixture.md @@ -677,3 +677,34 @@ ! 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 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 Pass it exactly once, by its new name `vertices`. + 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`. + diff --git a/tests/testthat/test-generate-migrations.R b/tests/testthat/test-generate-migrations.R index c1146679b2..bd70688b44 100644 --- a/tests/testthat/test-generate-migrations.R +++ b/tests/testthat/test-generate-migrations.R @@ -207,3 +207,75 @@ 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 `...`" + ) +}) diff --git a/tests/testthat/test-migration-fixture.R b/tests/testthat/test-migration-fixture.R index 1654f72479..4e5a73c0c5 100644 --- a/tests/testthat/test-migration-fixture.R +++ b/tests/testthat/test-migration-fixture.R @@ -633,3 +633,101 @@ 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", 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)) +}) diff --git a/tools/generate-migrations.R b/tools/generate-migrations.R index eae7db952d..6f8f75f8ba 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,32 @@ 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 + ) + + 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( @@ -600,6 +671,172 @@ 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) + if (!entry$has_dots) { + 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 `.old_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( + " .old_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]] <- .old_signature", + " .arg_handle <- base::eval(.arg_call, base::parent.frame())", + " .arg_extra <- .arg_handle$extra", + 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)]" + ), + 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)", + paste0( + " if (base::length(.arg_conflict) > 0L) cli::cli_abort(base::c(\"Argument {.arg {(.arg_conflict)}} of {.fn ", + fn, + "} 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()))", + 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) {", + " lifecycle::deprecate_soft(", + paste0(" \"", entry$when, "\","), + paste0( + " what = base::I(\"Calling `", + fn, + "()` with positional, abbreviated or renamed arguments\")," + ), + paste0( + " details = base::c(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 @@ -666,7 +903,6 @@ render_arg_handle <- function(entry) { } c( - "# fmt: skip", "if (...length() > 0L) {", guards, paste0( @@ -757,7 +993,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..857539295d 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,26 @@ 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. diff --git a/tools/migrations/fixture.R b/tools/migrations/fixture.R index 33aa51ec7e..081fd24c4f 100644 --- a/tools/migrations/fixture.R +++ b/tools/migrations/fixture.R @@ -59,5 +59,52 @@ 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, + value, + index = deprecated() + ) {}, + when = "3.0.0" ) ) From 31ad3ae6175cc5bae0da990e80a5f9e152644773 Mon Sep 17 00:00:00 2001 From: David Schoch Date: Thu, 3 Dec 2026 15:14:23 +0100 Subject: [PATCH 07/12] feat: Name the legacy argument when a re-matched call supplies one twice Co-Authored-By: Claude Opus 5.5 --- R/migration-fixture.R | 18 +++++++++++++++--- tests/testthat/_snaps/migration-fixture.md | 8 +++++++- tests/testthat/test-migration-fixture.R | 1 + tools/generate-migrations.R | 8 ++++++-- 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/R/migration-fixture.R b/R/migration-fixture.R index e4168c73e9..dc03fe9abe 100644 --- a/R/migration-fixture.R +++ b/R/migration-fixture.R @@ -244,7 +244,11 @@ migration_fixture_rename <- function( .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) 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)}}.")) + 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"))] @@ -343,7 +347,11 @@ migration_fixture_rename_nodots <- function( .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) cli::cli_abort(base::c("Argument {.arg {(.arg_conflict)}} of {.fn migration_fixture_rename_nodots} was supplied more than once.", i = "Pass it exactly once, by its new name {.arg {(.arg_conflict)}}.")) + 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 = )") } @@ -383,7 +391,11 @@ migration_fixture_rename_nodots <- function( .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) 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)}}.")) + 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 = )") } diff --git a/tests/testthat/_snaps/migration-fixture.md b/tests/testthat/_snaps/migration-fixture.md index c8d27206eb..c6e3917e1b 100644 --- a/tests/testthat/_snaps/migration-fixture.md +++ b/tests/testthat/_snaps/migration-fixture.md @@ -696,7 +696,13 @@ Condition Error in `migration_fixture_rename()`: ! Argument `vertices` of `migration_fixture_rename()` was supplied more than once. - i Pass it exactly once, by its new name `vertices`. + 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 diff --git a/tests/testthat/test-migration-fixture.R b/tests/testthat/test-migration-fixture.R index 4e5a73c0c5..74f17bb9fa 100644 --- a/tests/testthat/test-migration-fixture.R +++ b/tests/testthat/test-migration-fixture.R @@ -707,6 +707,7 @@ 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) }) diff --git a/tools/generate-migrations.R b/tools/generate-migrations.R index 6f8f75f8ba..5598bd9906 100644 --- a/tools/generate-migrations.R +++ b/tools/generate-migrations.R @@ -769,11 +769,15 @@ render_legacy_call <- function(entry) { " .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( - " if (base::length(.arg_conflict) > 0L) cli::cli_abort(base::c(\"Argument {.arg {(.arg_conflict)}} of {.fn ", + " cli::cli_abort(base::c(\"Argument {.arg {(.arg_conflict)}} of {.fn ", fn, - "} was supplied more than once.\", i = \"Pass it exactly once, by its new name {.arg {(.arg_conflict)}}.\"))" + "} 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(", From a81d7c66918c2989cdfba882833738e9f4f384d0 Mon Sep 17 00:00:00 2001 From: David Schoch Date: Thu, 3 Dec 2026 15:30:43 +0100 Subject: [PATCH 08/12] feat: Show the detected call when a re-matched call recovers old tail arguments Co-Authored-By: Claude Opus 5.5 --- R/migration-fixture.R | 6 +++++- tests/testthat/_snaps/migration-fixture.md | 1 + tools/generate-migrations.R | 14 ++++++++++++-- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/R/migration-fixture.R b/R/migration-fixture.R index dc03fe9abe..1d988767d3 100644 --- a/R/migration-fixture.R +++ b/R/migration-fixture.R @@ -254,10 +254,14 @@ migration_fixture_rename <- function( .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("Use instead: migration_fixture_rename(", base::paste(base::c("graph", "vertices", "to", base::paste0(base::names(.arg_tail), " = ")), collapse = ", "), ")")) + 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) { diff --git a/tests/testthat/_snaps/migration-fixture.md b/tests/testthat/_snaps/migration-fixture.md index c6e3917e1b..35626bfa62 100644 --- a/tests/testthat/_snaps/migration-fixture.md +++ b/tests/testthat/_snaps/migration-fixture.md @@ -687,6 +687,7 @@ 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 diff --git a/tools/generate-migrations.R b/tools/generate-migrations.R index 5598bd9906..d4be13909f 100644 --- a/tools/generate-migrations.R +++ b/tools/generate-migrations.R @@ -810,6 +810,7 @@ render_legacy_call <- function(entry) { ), " .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( @@ -817,16 +818,25 @@ render_legacy_call <- function(entry) { 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( - " details = base::c(i = base::paste0(\"Use instead: ", + " i = base::paste0(\"Use instead: ", fn, "(\", base::paste(", render_vector(c( quote_items(entry$head), "base::paste0(base::names(.arg_tail), \" = \")" )), - ", collapse = \", \"), \")\"))" + ", collapse = \", \"), \")\")" ), + " )", " )", " }" ) From d34a54e72fbccef996319d92c16758edfc1d515e Mon Sep 17 00:00:00 2001 From: David Schoch Date: Thu, 3 Dec 2026 15:33:22 +0100 Subject: [PATCH 09/12] feat: Support head renames in functions that pass `...` on A registry entry with `passthrough_dots = TRUE` gets the re-match for old head names but no recovery out of `...`, and named arguments the old signature did not know are left to `...`. Co-Authored-By: Claude Opus 5.5 --- R/migration-fixture.R | 46 +++++++++++++++ tests/testthat/_snaps/migration-fixture.md | 9 +++ tests/testthat/test-migration-fixture.R | 21 +++++++ tools/generate-migrations.R | 65 ++++++++++++++++++---- tools/migrations/README.md | 5 ++ tools/migrations/fixture.R | 9 +++ 6 files changed, 144 insertions(+), 11 deletions(-) diff --git a/R/migration-fixture.R b/R/migration-fixture.R index 1d988767d3..cfe807e868 100644 --- a/R/migration-fixture.R +++ b/R/migration-fixture.R @@ -408,3 +408,49 @@ migration_fixture_rename_nodots <- function( 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) + .old_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]] <- .old_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/tests/testthat/_snaps/migration-fixture.md b/tests/testthat/_snaps/migration-fixture.md index 35626bfa62..49004b2a38 100644 --- a/tests/testthat/_snaps/migration-fixture.md +++ b/tests/testthat/_snaps/migration-fixture.md @@ -715,3 +715,12 @@ 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/test-migration-fixture.R b/tests/testthat/test-migration-fixture.R index 74f17bb9fa..14effc7f55 100644 --- a/tests/testthat/test-migration-fixture.R +++ b/tests/testthat/test-migration-fixture.R @@ -732,3 +732,24 @@ test_that("a replacement function keeps the old order too", { ) 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/tools/generate-migrations.R b/tools/generate-migrations.R index d4be13909f..5a563edc10 100644 --- a/tools/generate-migrations.R +++ b/tools/generate-migrations.R @@ -377,6 +377,29 @@ normalise_migration <- function(fn, entry) { 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( @@ -672,7 +695,7 @@ render_vector <- function(items) { # globals. render_arg_handle <- function(entry) { legacy <- render_legacy_call(entry) - if (!entry$has_dots) { + if (!entry$has_dots || entry$passthrough_dots) { return(c("# fmt: skip", legacy, "}")) } dots <- render_dots(entry) @@ -749,11 +772,15 @@ render_legacy_call <- function(entry) { " .arg_call[[1L]] <- .old_signature", " .arg_handle <- base::eval(.arg_call, base::parent.frame())", " .arg_extra <- .arg_handle$extra", - paste0( - " if (.arg_handle$n_extra > base::sum(base::nzchar(.arg_extra))) cli::cli_abort(\"Too many arguments passed to {.fn ", - fn, - "}.\")" - ), + 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)), @@ -761,11 +788,27 @@ render_legacy_call <- function(entry) { render_vector(quote_items(entry$new_args)), ", duplicates.ok = TRUE)]" ), - paste0( - " if (base::anyNA(.arg_explicit)) cli::cli_abort(\"Unexpected argument passed to {.fn ", - fn, - "}: {.arg {(.arg_extra[base::is.na(.arg_explicit)])}}.\")" - ), + 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)", diff --git a/tools/migrations/README.md b/tools/migrations/README.md index 857539295d..92b6fc67bf 100644 --- a/tools/migrations/README.md +++ b/tools/migrations/README.md @@ -130,3 +130,8 @@ 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/fixture.R b/tools/migrations/fixture.R index 081fd24c4f..2bd37a5b4c 100644 --- a/tools/migrations/fixture.R +++ b/tools/migrations/fixture.R @@ -106,5 +106,14 @@ migrations <- list( index = deprecated() ) {}, 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" ) ) From 02448e8ec365f00e6ab7c22e29eda8e36a8386da Mon Sep 17 00:00:00 2001 From: David Schoch Date: Thu, 3 Dec 2026 15:44:47 +0100 Subject: [PATCH 10/12] feat: Render no `...` recovery for a head rename with nothing past the head Co-Authored-By: Claude Opus 5.5 --- tests/testthat/test-generate-migrations.R | 26 +++++++++++++++++++++++ tools/generate-migrations.R | 7 +++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/tests/testthat/test-generate-migrations.R b/tests/testthat/test-generate-migrations.R index bd70688b44..6562e7914d 100644 --- a/tests/testthat/test-generate-migrations.R +++ b/tests/testthat/test-generate-migrations.R @@ -279,3 +279,29 @@ test_that("a migration without `...` must be a head rename", { "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/tools/generate-migrations.R b/tools/generate-migrations.R index 5a563edc10..cd73b8fad0 100644 --- a/tools/generate-migrations.R +++ b/tools/generate-migrations.R @@ -695,7 +695,12 @@ render_vector <- function(items) { # globals. render_arg_handle <- function(entry) { legacy <- render_legacy_call(entry) - if (!entry$has_dots || entry$passthrough_dots) { + # 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) From 6d6f35d81e1e3ea28e34c667edada56cfa48e6d2 Mon Sep 17 00:00:00 2001 From: David Schoch Date: Thu, 3 Dec 2026 17:18:16 +0100 Subject: [PATCH 11/12] fix: Keep `value` last in the replacement-function migration fixture R CMD check requires the last formal of a replacement function to be `value`. Co-Authored-By: Claude Opus 5.5 --- R/migration-fixture.R | 4 ++-- tools/migrations/fixture.R | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/R/migration-fixture.R b/R/migration-fixture.R index cfe807e868..1a90e01869 100644 --- a/R/migration-fixture.R +++ b/R/migration-fixture.R @@ -370,8 +370,8 @@ migration_fixture_rename_nodots <- function( graph, name, vertices = NULL, - value, - index = deprecated() + index = deprecated(), + value ) { # BEGIN GENERATED ARG_HANDLE: migration_fixture_rename<-, do not edit, see tools/generate-migrations.R # fmt: skip diff --git a/tools/migrations/fixture.R b/tools/migrations/fixture.R index 2bd37a5b4c..b679a8cfef 100644 --- a/tools/migrations/fixture.R +++ b/tools/migrations/fixture.R @@ -102,8 +102,8 @@ migrations <- list( graph, name, vertices = NULL, - value, - index = deprecated() + index = deprecated(), + value ) {}, when = "3.0.0" ), From 2db505c60ed6b206ca517bfe2988100f1df485c4 Mon Sep 17 00:00:00 2001 From: David Schoch Date: Thu, 3 Dec 2026 17:49:28 +0100 Subject: [PATCH 12/12] fix: Give the legacy re-match helper its own name A function with both a legacy block and a `...` recovery block defined `.old_signature()` twice with different formals, which R CMD check reports as a NOTE. The legacy block now uses `.legacy_signature()`. Co-Authored-By: Claude Opus 5.5 --- R/migration-fixture.R | 16 ++++++++-------- tools/generate-migrations.R | 6 +++--- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/R/migration-fixture.R b/R/migration-fixture.R index 1a90e01869..3bcc374c39 100644 --- a/R/migration-fixture.R +++ b/R/migration-fixture.R @@ -225,7 +225,7 @@ migration_fixture_rename <- function( # fmt: skip if (!base::missing(from)) { # Pre-3.0.0 signature: migration_fixture_rename(graph, from, to, kind, directed) - .old_signature <- function(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"), @@ -235,7 +235,7 @@ migration_fixture_rename <- function( )) } .arg_call <- base::sys.call() - .arg_call[[1L]] <- .old_signature + .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}.") @@ -333,7 +333,7 @@ migration_fixture_rename_nodots <- function( # fmt: skip if (!base::missing(index)) { # Pre-3.0.0 signature: migration_fixture_rename_nodots(graph, name, index, value) - .old_signature <- function(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"), @@ -342,7 +342,7 @@ migration_fixture_rename_nodots <- function( )) } .arg_call <- base::sys.call() - .arg_call[[1L]] <- .old_signature + .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}.") @@ -377,7 +377,7 @@ migration_fixture_rename_nodots <- function( # fmt: skip if (!base::missing(index)) { # Pre-3.0.0 signature: migration_fixture_rename<-(graph, name, index, value) - .old_signature <- function(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"), @@ -386,7 +386,7 @@ migration_fixture_rename_nodots <- function( )) } .arg_call <- base::sys.call() - .arg_call[[1L]] <- .old_signature + .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<-}.") @@ -424,7 +424,7 @@ migration_fixture_passthrough <- function( # fmt: skip if (!base::missing(from)) { # Pre-3.0.0 signature: migration_fixture_passthrough(x, from, to) - .old_signature <- function(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"), @@ -432,7 +432,7 @@ migration_fixture_passthrough <- function( )) } .arg_call <- base::sys.call() - .arg_call[[1L]] <- .old_signature + .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) diff --git a/tools/generate-migrations.R b/tools/generate-migrations.R index cd73b8fad0..01d5649fae 100644 --- a/tools/generate-migrations.R +++ b/tools/generate-migrations.R @@ -718,7 +718,7 @@ render_arg_handle <- function(entry) { # 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 `.old_signature()` at its head, evaluated in +# 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 @@ -756,7 +756,7 @@ render_legacy_call <- function(entry) { ")" ), paste0( - " .old_signature <- function(", + " .legacy_signature <- function(", paste(c(old, "..."), collapse = ", "), ") {" ), @@ -774,7 +774,7 @@ render_legacy_call <- function(entry) { " ))", " }", " .arg_call <- base::sys.call()", - " .arg_call[[1L]] <- .old_signature", + " .arg_call[[1L]] <- .legacy_signature", " .arg_handle <- base::eval(.arg_call, base::parent.frame())", " .arg_extra <- .arg_handle$extra", if (entry$passthrough_dots) {