diff --git a/NAMESPACE b/NAMESPACE index e752736e0..221d799df 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -6,6 +6,7 @@ S3method("$",vctrs_list_of) S3method("$",vctrs_rcrd) S3method("$",vctrs_sclr) S3method("$",vctrs_vctr) +S3method("$<-",vctrs_error_subscript_type) S3method("$<-",vctrs_list_of) S3method("$<-",vctrs_rcrd) S3method("$<-",vctrs_sclr) @@ -34,6 +35,7 @@ S3method("[[",vctrs_list_of) S3method("[[",vctrs_rcrd) S3method("[[",vctrs_sclr) S3method("[[",vctrs_vctr) +S3method("[[<-",vctrs_error_subscript_type) S3method("[[<-",vctrs_list_of) S3method("[[<-",vctrs_rcrd) S3method("[[<-",vctrs_sclr) @@ -100,7 +102,6 @@ S3method(cnd_body,vctrs_error_names_cannot_be_dot_dot) S3method(cnd_body,vctrs_error_names_cannot_be_empty) S3method(cnd_body,vctrs_error_names_must_be_unique) S3method(cnd_body,vctrs_error_subscript_oob) -S3method(cnd_body,vctrs_error_subscript_type) S3method(cnd_header,vctrs_error_cast_lossy) S3method(cnd_header,vctrs_error_incompatible_size) S3method(cnd_header,vctrs_error_matches_incomplete) diff --git a/R/subscript-loc.R b/R/subscript-loc.R index 3fabb24b3..417ac0578 100644 --- a/R/subscript-loc.R +++ b/R/subscript-loc.R @@ -199,25 +199,18 @@ vec_as_location2_result <- function(i, arg = arg, call = call ) - if (!is_null(result$err)) { - parent <- result$err - return(result(err = new_error_location2_type( - i = i, - subscript_arg = arg, - body = parent$body, - call = call - ))) + return(result) } # Locations must be size 1, can't be NA, and must be positive i <- result$ok if (length(i) != 1L) { - return(result(err = new_error_location2_type( + return(result(err = new_chained_error_location2_type( i = i, subscript_arg = arg, - body = cnd_bullets_location2_need_scalar, + header = cnd_header_location2_need_scalar, call = call ))) } @@ -229,10 +222,10 @@ vec_as_location2_result <- function(i, if (is.na(i)) { if (!allow_missing && is.na(i)) { - result <- result(err = new_error_location2_type( + result <- result(err = new_chained_error_location2_type( i = i, subscript_arg = arg, - body = cnd_bullets_location2_need_present, + header = cnd_header_location2_need_present, call = call )) } else { @@ -242,19 +235,19 @@ vec_as_location2_result <- function(i, } if (identical(i, 0L)) { - return(result(err = new_error_location2_type( + return(result(err = new_chained_error_location2_type( i = i, subscript_arg = arg, - body = cnd_bullets_location2_need_positive, + header = cnd_header_location2_need_positive, call = call ))) } if (!allow_negative && neg) { - return(result(err = new_error_location2_type( + return(result(err = new_chained_error_location2_type( i = i, subscript_arg = arg, - body = cnd_bullets_location2_need_positive, + header = cnd_header_location2_need_positive, call = call ))) } @@ -284,32 +277,72 @@ vec_as_location2_result <- function(i, } } +new_error_location2_type <- function(i, + ..., + class = NULL) { + new_error_subscript2_type( + class = class, + i = i, + numeric = "cast", + character = "cast", + ... + ) +} +new_chained_error_location2_type <- function(i, + ..., + header = NULL, + call = caller_env()) { + causal <- error_cnd( + i = i, + header = header, + ..., + call = NULL, + use_cli_format = TRUE + ) + new_error_location2_type( + i = i, + ..., + body = function(...) chr(), + call = call, + parent = causal + ) +} + +cnd_header_location2_need_scalar <- function(cnd, ...) { + cnd$subscript_arg <- cnd_subscript_arg(cnd) + glue::glue_data(cnd, "{subscript_arg} must be size 1, not {length(i)}.") +} +cnd_header_location2_need_present <- function(cnd, ...) { + cnd$subscript_arg <- cnd_subscript_arg(cnd) + glue::glue_data(cnd, "{subscript_arg} must be a location, not {obj_type_friendly(i)}.") +} +cnd_header_location2_need_positive <- function(cnd, ...) { + cnd$subscript_arg <- cnd_subscript_arg(cnd) + glue::glue_data(cnd, "{subscript_arg} must be a positive location, not {i}.") +} stop_location_negative_missing <- function(i, ..., call = caller_env()) { - cnd_signal(new_error_subscript_type( + cnd <- new_chained_error_subscript_type( i, ..., - body = cnd_body_vctrs_error_location_negative_missing, + header = cnd_header_location_negative_missing, call = call - )) + ) + cnd_signal(cnd) } -cnd_body_vctrs_error_location_negative_missing <- function(cnd, ...) { + +cnd_header_location_negative_missing <- function(cnd, ...) { missing_loc <- which(is.na(cnd$i)) - arg <- append_arg("Subscript", cnd$subscript_arg) + arg <- cnd_subscript_arg(cnd) - if (length(missing_loc) == 1) { - loc <- glue::glue("{arg} has a missing value at location {missing_loc}.") - } else { - n_loc <- length(missing_loc) - missing_loc <- ensure_full_stop(enumerate(missing_loc)) - loc <- glue::glue( - "{arg} has {n_loc} missing values at locations {missing_loc}" + n_loc <- length(missing_loc) + + c( + "Negative locations can't have missing values.", + "x" = cli::format_inline( + "{arg} has {n_loc} missing value{?s} at location{?s} {missing_loc}." ) - } - format_error_bullets(c( - x = "Negative locations can't have missing values.", - i = loc - )) + ) } stop_location_negative_positive <- function(i, ..., call = caller_env()) { @@ -339,64 +372,35 @@ cnd_body_vctrs_error_location_negative_positive <- function(cnd, ...) { )) } - -new_error_location2_type <- function(i, - ..., - class = NULL) { - new_error_subscript2_type( - class = class, - i = i, - numeric = "cast", - character = "cast", - ... - ) -} -cnd_bullets_location2_need_scalar <- function(cnd, ...) { - cnd$subscript_arg <- append_arg("Subscript", cnd$subscript_arg) - format_error_bullets(c( - x = glue::glue_data(cnd, "{subscript_arg} must be size 1, not {length(i)}.") - )) -} -cnd_bullets_location2_need_present <- function(cnd, ...) { - cnd$subscript_arg <- append_arg("Subscript", cnd$subscript_arg) - format_error_bullets(c( - x = glue::glue_data(cnd, "{subscript_arg} must be a location, not {obj_type_friendly(i)}.") - )) -} -cnd_bullets_location2_need_positive <- function(cnd, ...) { - cnd$subscript_arg <- append_arg("Subscript", cnd$subscript_arg) - format_error_bullets(c( - x = glue::glue_data(cnd, "{subscript_arg} must be a positive location, not {i}.") - )) -} - stop_location_negative <- function(i, ..., call = caller_env()) { - cnd_signal(new_error_subscript_type( + cnd <- new_chained_error_subscript_type( i, - body = cnd_bullets_location_need_non_negative, ..., + header = cnd_header_location_need_non_negative, call = call - )) + ) + cnd_signal(cnd) } -cnd_bullets_location_need_non_negative <- function(cnd, ...) { - cnd$subscript_arg <- append_arg("Subscript", cnd$subscript_arg) - format_error_bullets(c( - x = glue::glue_data(cnd, "{subscript_arg} can't contain negative locations.") - )) +cnd_header_location_need_non_negative <- function(cnd, ...) { + arg <- cnd_subscript_arg(cnd) + glue::glue("{arg} can't contain negative locations.") } stop_location_zero <- function(i, ..., call = caller_env()) { - cnd_signal(new_error_subscript_type( + cnd <- new_chained_error_subscript_type( i, - body = cnd_bullets_location_need_non_zero, ..., + header = cnd_header_location_need_non_zero, call = call - )) + ) + cnd_signal(cnd) } -cnd_bullets_location_need_non_zero <- function(cnd, ...) { +cnd_header_location_need_non_zero <- function(cnd, ...) { + arg <- cnd_subscript_arg(cnd) + header <- glue::glue("{arg} can't contain `0` values.") + zero_loc <- which(cnd$i == 0) zero_loc_size <- length(zero_loc) - arg <- append_arg("Subscript", cnd$subscript_arg) if (zero_loc_size == 1) { loc <- glue::glue("It has a `0` value at location {zero_loc}.") @@ -406,22 +410,21 @@ cnd_bullets_location_need_non_zero <- function(cnd, ...) { "It has {zero_loc_size} `0` values at locations {zero_loc}" ) } - format_error_bullets(c( - x = glue::glue("{arg} can't contain `0` values."), - i = loc - )) + c(header, i = loc) } stop_subscript_missing <- function(i, ..., call = caller_env()) { - cnd_signal(new_error_subscript_type( - i = i, - body = cnd_bullets_subscript_missing, + cnd <- new_chained_error_subscript_type( + i, ..., + header = cnd_header_subscript_missing, call = call - )) + ) + cnd_signal(cnd) } -cnd_bullets_subscript_missing <- function(cnd, ...) { - cnd$subscript_arg <- append_arg("Subscript", cnd$subscript_arg) +cnd_header_subscript_missing <- function(cnd, ...) { + arg <- cnd_subscript_arg(cnd) + header <- glue::glue("{arg} can't contain missing values.") missing_loc <- which(is.na(cnd$i)) if (length(missing_loc) == 1) { @@ -431,52 +434,46 @@ cnd_bullets_subscript_missing <- function(cnd, ...) { missing_line <- glue::glue("It has missing values at locations {missing_enum}") } - format_error_bullets(c( - x = glue::glue_data(cnd, "{subscript_arg} can't contain missing values."), - x = missing_line - )) + c(header, x = missing_line) } stop_subscript_empty <- function(i, ..., call = caller_env()) { - cnd_signal(new_error_subscript_type( - i = i, - body = cnd_bullets_subscript_empty, + cnd <- new_chained_error_subscript_type( + i, ..., + header = cnd_bullets_subscript_empty, call = call - )) + ) + cnd_signal(cnd) } cnd_bullets_subscript_empty <- function(cnd, ...) { - cnd$subscript_arg <- append_arg("Subscript", cnd$subscript_arg) + arg <- cnd_subscript_arg(cnd) + header <- glue::glue("{arg} can't contain the empty string.") loc <- which(cnd$i == "") if (length(loc) == 1) { - line <- glue::glue("It has an empty string at location {loc}.") + locations <- glue::glue("It has an empty string at location {loc}.") } else { enum <- ensure_full_stop(enumerate(loc)) - line <- glue::glue("It has an empty string at locations {enum}") + locations <- glue::glue("It has an empty string at locations {enum}") } - format_error_bullets(c( - x = glue::glue_data(cnd, "{subscript_arg} can't contain the empty string."), - x = line - )) + c(header, x = locations) } stop_indicator_size <- function(i, n, ..., call = caller_env()) { - cnd_signal(new_error_subscript_size( + cnd <- new_chained_error_subscript_size( i, n = n, ..., - body = cnd_body_vctrs_error_indicator_size, + header = cnd_header_logical_subscript_size, call = call - )) -} -cnd_body_vctrs_error_indicator_size <- function(cnd, ...) { - cnd$subscript_arg <- append_arg("Logical subscript", cnd$subscript_arg) - glue_data_bullets( - cnd, - x = "{subscript_arg} must be size 1 or {n}, not {vec_size(i)}." ) + cnd_signal(cnd) +} +cnd_header_logical_subscript_size <- function(cnd, ...) { + cnd$arg <- append_arg("Logical subscript", cnd$subscript_arg) + glue::glue_data(cnd, "{arg} must be size 1 or {n}, not {vec_size(i)}.") } stop_subscript_oob <- function(i, @@ -511,7 +508,8 @@ cnd_header.vctrs_error_subscript_oob <- function(cnd, ...) { #' @export cnd_body.vctrs_error_subscript_oob <- function(cnd, ...) { - switch(cnd_subscript_type(cnd), + switch( + cnd_subscript_type(cnd), numeric = if (cnd_subscript_oob_non_consecutive(cnd)) { cnd_body_vctrs_error_subscript_oob_non_consecutive(cnd, ...) @@ -520,7 +518,7 @@ cnd_body.vctrs_error_subscript_oob <- function(cnd, ...) { }, character = cnd_body_vctrs_error_subscript_oob_name(cnd, ...), - abort("Internal error: subscript type can't be `logical` for OOB errors.") + abort("Subscript type can't be `logical` for OOB errors.", .internal = TRUE) ) } cnd_body_vctrs_error_subscript_oob_location <- function(cnd, ...) { diff --git a/R/subscript.R b/R/subscript.R index 7b87c0ba0..317aa236a 100644 --- a/R/subscript.R +++ b/R/subscript.R @@ -173,29 +173,114 @@ new_error_subscript_type <- function(i, ) } +new_chained_error_subscript_type <- function(i, + ..., + header = NULL, + call = caller_env()) { + header <- header %||% cnd_header_subscript_type + + causal <- error_cnd( + i = i, + header = header, + ..., + call = NULL, + use_cli_format = TRUE + ) + new_error_subscript_type( + i, + ..., + body = function(...) chr(), + call = call, + parent = causal + ) +} +new_chained_error_subscript_size <- function(i, + ..., + header = NULL, + call = caller_env()) { + causal <- error_cnd( + i = i, + header = header, + ..., + call = NULL, + use_cli_format = TRUE + ) + new_error_subscript_size( + i, + ..., + body = function(...) chr(), + call = call, + parent = causal + ) +} + +# Subscripts errors are currently customised by rethrowing errors with +# special fields. The setter methods forward these fields to parent +# errors so that causal errors may benefit from these customisations. + +#' @export +`$<-.vctrs_error_subscript_type` <- function(x, i, value) { + x <- NextMethod() + i <- as_string(i) + + if (!is_null(x[["parent"]]) && is_subscript_field(i)) { + x[["parent"]][[i]] <- value + } + + x +} +#' @export +`[[<-.vctrs_error_subscript_type` <- function(x, i, value) { + x <- NextMethod() + + if (!is_null(x[["parent"]]) && is_subscript_field(i)) { + x[["parent"]][[i]] <- value + } + + x +} + +is_subscript_field <- function(x) { + is_character(x) && x %in% c( + "i", + "subscript_arg", + "subscript_elt", + "subscript_action" + ) +} + + #' @export cnd_header.vctrs_error_subscript_type <- function(cnd) { - action <- cnd_subscript_action(cnd) + arg <- cnd[["subscript_arg"]] + if (is_subscript_arg(arg)) { + with <- glue::glue(" with {format_subscript_arg(arg)}") + } else { + with <- "" + } + + action <- cnd_subscript_action(cnd, assign_to = FALSE) elt <- cnd_subscript_element(cnd) + if (cnd_subscript_scalar(cnd)) { - glue::glue("Must {action} {elt[[1]]} with a single valid subscript.") + glue::glue("Can't {action} {elt[[1]]}{with}.") } else { - glue::glue("Must {action} {elt[[2]]} with a valid subscript vector.") + glue::glue("Can't {action} {elt[[2]]}{with}.") } } -#' @export -cnd_body.vctrs_error_subscript_type <- function(cnd) { + +cnd_header_subscript_type <- function(cnd, ...) { arg <- cnd_subscript_arg(cnd) type <- obj_type_friendly(cnd$i) expected_types <- cnd_subscript_expected_types(cnd) - format_error_bullets(c( - x = cli::format_inline("{arg} must be {.or {expected_types}}, not {type}.") - )) + cli::format_inline("{arg} must be {.or {expected_types}}, not {type}.") } -new_cnd_bullets_subscript_lossy_cast <- function(lossy_err) { + +new_cnd_header_subscript_lossy_cast <- function(lossy_err) { + force(lossy_err) function(cnd, ...) { - format_error_bullets(c(x = cnd_header(lossy_err))) + cnd_header(lossy_err) } } @@ -244,8 +329,8 @@ new_error_subscript2_type <- function(i, ) } -cnd_body_subscript_dim <- function(cnd, ...) { - arg <- append_arg("Subscript", cnd$subscript_arg) +cnd_header_subscript_dim <- function(cnd, ...) { + arg <- cnd_subscript_arg(cnd) dim <- length(dim(cnd$i)) if (dim < 2) { @@ -257,9 +342,9 @@ cnd_body_subscript_dim <- function(cnd, ...) { shape <- "an array" } - format_error_bullets(c( - x = glue::glue("{arg} must be a simple vector, not {shape}.") - )) + c( + glue::glue("{arg} must be a simple vector, not {shape}.") + ) } cnd_subscript_element <- function(cnd, capital = FALSE) { diff --git a/src/decl/subscript-decl.h b/src/decl/subscript-decl.h index 118c66e38..4e2f66ce7 100644 --- a/src/decl/subscript-decl.h +++ b/src/decl/subscript-decl.h @@ -1,10 +1,10 @@ static -r_obj* fns_cnd_body_subscript_dim; +r_obj* fns_cnd_header_subscript_dim; static -r_obj* new_error_subscript_type(r_obj* subscript, - const struct subscript_opts* opts, - r_obj* body); +r_obj* new_chained_error_subscript_type(r_obj* subscript, + const struct subscript_opts* opts, + r_obj* body); static enum subscript_type_action parse_subscript_arg_type(r_obj* x, @@ -26,10 +26,10 @@ r_obj* dbl_cast_subscript_fallback(r_obj* subscript, ERR* err); static -r_obj* syms_new_dbl_cast_subscript_body; +r_obj* syms_new_cnd_header_subscript_lossy_cast; static r_obj* syms_lossy_err; static -r_obj* syms_new_error_subscript_type; +r_obj* syms_new_chained_error_subscript_type; diff --git a/src/globals.c b/src/globals.c index 772e4998a..141bdaaf0 100644 --- a/src/globals.c +++ b/src/globals.c @@ -54,6 +54,7 @@ void vctrs_init_globals(r_obj* ns) { syms.dot_error_arg = r_sym(".error_arg"); syms.dot_error_call = r_sym(".error_call"); syms.haystack_arg = r_sym("haystack_arg"); + syms.header = r_sym("header"); syms.needles_arg = r_sym("needles_arg"); syms.recurse = r_sym("recurse"); syms.repair_arg = r_sym("repair_arg"); diff --git a/src/globals.h b/src/globals.h index b2a6e501d..b6b89e6ed 100644 --- a/src/globals.h +++ b/src/globals.h @@ -11,6 +11,7 @@ struct syms { r_obj* dot_error_arg; r_obj* dot_error_call; r_obj* haystack_arg; + r_obj* header; r_obj* needles_arg; r_obj* recurse; r_obj* repair_arg; diff --git a/src/subscript.c b/src/subscript.c index 99c99e77b..88a9e1576 100644 --- a/src/subscript.c +++ b/src/subscript.c @@ -6,7 +6,7 @@ r_obj* vec_as_subscript_opts(r_obj* subscript, const struct subscript_opts* opts, ERR* err) { if (vec_dim_n(subscript) != 1) { - *err = new_error_subscript_type(subscript, opts, fns_cnd_body_subscript_dim); + *err = new_chained_error_subscript_type(subscript, opts, fns_cnd_header_subscript_dim); return r_null; } @@ -32,7 +32,7 @@ r_obj* vec_as_subscript_opts(r_obj* subscript, KEEP_AT(subscript, subscript_pi); if (!vec_is_vector(subscript)) { - *err = new_error_subscript_type(subscript, opts, r_null); + *err = new_chained_error_subscript_type(subscript, opts, r_null); FREE(2); return r_null; } @@ -78,7 +78,7 @@ r_obj* vec_as_subscript_opts(r_obj* subscript, } if (action == SUBSCRIPT_TYPE_ACTION_ERROR) { - *err = new_error_subscript_type(subscript, opts, r_null); + *err = new_chained_error_subscript_type(subscript, opts, r_null); FREE(2); return r_null; } @@ -126,7 +126,7 @@ r_obj* obj_cast_subscript(r_obj* subscript, return vec_cast_opts(&cast_opts); } - *err = new_error_subscript_type(subscript, opts, r_null); + *err = new_chained_error_subscript_type(subscript, opts, r_null); return r_null; } @@ -185,10 +185,10 @@ r_obj* dbl_cast_subscript_fallback(r_obj* subscript, if (*err) { r_obj* err_obj = KEEP(*err); - r_obj* body = KEEP(vctrs_eval_mask1(syms_new_dbl_cast_subscript_body, - syms_lossy_err, err_obj)); + r_obj* header = KEEP(vctrs_eval_mask1(syms_new_cnd_header_subscript_lossy_cast, + syms_lossy_err, err_obj)); - *err = new_error_subscript_type(subscript, opts, body); + *err = new_chained_error_subscript_type(subscript, opts, header); FREE(3); return r_null; } @@ -285,9 +285,9 @@ enum subscript_type_action parse_subscript_arg_type(r_obj* x, // Conditions ------------------------------------------------------------------ static -r_obj* new_error_subscript_type(r_obj* subscript, - const struct subscript_opts* opts, - r_obj* body) { +r_obj* new_chained_error_subscript_type(r_obj* subscript, + const struct subscript_opts* opts, + r_obj* header) { r_obj* logical = subscript_type_action_chr(opts->logical); r_obj* numeric = subscript_type_action_chr(opts->numeric); r_obj* character = subscript_type_action_chr(opts->character); @@ -296,7 +296,7 @@ r_obj* new_error_subscript_type(r_obj* subscript, r_obj* subscript_arg = KEEP(vctrs_arg(opts->subscript_arg)); r_obj* ffi_call = r_lazy_eval_protect(opts->call); - r_obj* syms[] = { + r_obj* params[] = { syms_i, syms_subscript_arg, syms_subscript_action, @@ -304,7 +304,7 @@ r_obj* new_error_subscript_type(r_obj* subscript, syms_logical, syms_numeric, syms_character, - syms_body, + syms.header, NULL }; r_obj* args[] = { @@ -315,11 +315,11 @@ r_obj* new_error_subscript_type(r_obj* subscript, logical, numeric, character, - body, + header, NULL }; - r_obj* call = KEEP(r_call_n(syms_new_error_subscript_type, syms, args)); + r_obj* call = KEEP(r_call_n(syms_new_chained_error_subscript_type, params, args)); r_obj* out = r_eval(call, vctrs_ns_env); FREE(3); @@ -330,21 +330,21 @@ r_obj* new_error_subscript_type(r_obj* subscript, // Init ---------------------------------------------------------------- void vctrs_init_subscript(r_obj* ns) { - syms_new_error_subscript_type = r_sym("new_error_subscript_type"); - syms_new_dbl_cast_subscript_body = r_sym("new_cnd_bullets_subscript_lossy_cast"); + syms_new_chained_error_subscript_type = r_sym("new_chained_error_subscript_type"); + syms_new_cnd_header_subscript_lossy_cast = r_sym("new_cnd_header_subscript_lossy_cast"); syms_lossy_err = r_sym("lossy_err"); - fns_cnd_body_subscript_dim = r_eval(r_sym("cnd_body_subscript_dim"), ns); + fns_cnd_header_subscript_dim = r_eval(r_sym("cnd_header_subscript_dim"), ns); } static -r_obj* fns_cnd_body_subscript_dim = NULL; +r_obj* fns_cnd_header_subscript_dim = NULL; static -r_obj* syms_new_dbl_cast_subscript_body = NULL; +r_obj* syms_new_cnd_header_subscript_lossy_cast = NULL; static r_obj* syms_lossy_err = NULL; static -r_obj* syms_new_error_subscript_type = NULL; +r_obj* syms_new_chained_error_subscript_type = NULL; diff --git a/tests/testthat/_snaps/error-call.md b/tests/testthat/_snaps/error-call.md index eb9c1af3b..83ae58ac2 100644 --- a/tests/testthat/_snaps/error-call.md +++ b/tests/testthat/_snaps/error-call.md @@ -218,8 +218,9 @@ Output Error in `my_function()`: - ! Must subset elements with a valid subscript vector. - x Can't convert from `my_arg` to due to loss of precision. + ! Can't subset elements with `my_arg`. + Caused by error: + ! Can't convert from `my_arg` to due to loss of precision. --- @@ -228,8 +229,9 @@ Output Error in `my_function()`: - ! Must subset elements with a valid subscript vector. - x Can't convert from to due to loss of precision. + ! Can't subset elements. + Caused by error: + ! Can't convert from to due to loss of precision. --- @@ -238,8 +240,9 @@ Output Error in `my_function()`: - ! Must subset elements with a valid subscript vector. - x `my_arg` must be logical, numeric, or character, not an empty list. + ! Can't subset elements with `my_arg`. + Caused by error: + ! `my_arg` must be logical, numeric, or character, not an empty list. --- @@ -258,8 +261,9 @@ Output Error in `my_function()`: - ! Must subset elements with a valid subscript vector. - x Subscript can't contain missing values. + ! Can't subset elements. + Caused by error: + ! Subscript can't contain missing values. x It has a missing value at location 1. # `vec_ptype()` reports correct error call @@ -326,8 +330,9 @@ Output Error in `vec_slice()`: - ! Must subset elements with a valid subscript vector. - x `i` must be logical, numeric, or character, not an environment. + ! Can't subset elements with `i`. + Caused by error: + ! `i` must be logical, numeric, or character, not an environment. # list_sizes() reports error context diff --git a/tests/testthat/_snaps/slice-assign.md b/tests/testthat/_snaps/slice-assign.md index 5d1d2b540..7867502ce 100644 --- a/tests/testthat/_snaps/slice-assign.md +++ b/tests/testthat/_snaps/slice-assign.md @@ -16,8 +16,9 @@ Output Error: - ! Must assign to elements with a valid subscript vector. - x Logical subscript must be size 1 or 2, not 3. + ! Can't assign elements. + Caused by error: + ! Logical subscript must be size 1 or 2, not 3. --- @@ -27,8 +28,9 @@ Output Error: - ! Must assign to elements with a valid subscript vector. - x Logical subscript must be size 1 or 32, not 2. + ! Can't assign elements. + Caused by error: + ! Logical subscript must be size 1 or 32, not 2. # must assign existing elements @@ -72,7 +74,7 @@ Output Error: - ! Must assign to elements with a valid subscript vector. + ! Can't assign elements. x Negative and positive locations can't be mixed. i Subscript has a positive value at location 2. Code @@ -81,9 +83,10 @@ Output Error: - ! Must assign to elements with a valid subscript vector. - x Negative locations can't have missing values. - i Subscript has a missing value at location 2. + ! Can't assign elements. + Caused by error: + ! Negative locations can't have missing values. + x Subscript has 1 missing value at location 2. # `vec_assign()` error args can be overridden diff --git a/tests/testthat/_snaps/slice-chop.md b/tests/testthat/_snaps/slice-chop.md index 97d2cd8d1..ed80d14bc 100644 --- a/tests/testthat/_snaps/slice-chop.md +++ b/tests/testthat/_snaps/slice-chop.md @@ -158,8 +158,9 @@ Output Error: - ! Must subset elements with a valid subscript vector. - x Subscript can't contain `0` values. + ! Can't subset elements. + Caused by error: + ! Subscript can't contain `0` values. i It has a `0` value at location 1. Code (expect_error(list_unchop(list(1), indices = list(-1)), class = "vctrs_error_subscript_type") @@ -167,8 +168,9 @@ Output Error: - ! Must subset elements with a valid subscript vector. - x Subscript can't contain negative locations. + ! Can't subset elements. + Caused by error: + ! Subscript can't contain negative locations. # list_unchop() fails with complex foreign S3 classes @@ -298,16 +300,18 @@ Output Error: - ! Must subset elements with a valid subscript vector. - x Subscript must be numeric, not the string "x". + ! Can't subset elements. + Caused by error: + ! Subscript must be numeric, not the string "x". Code (expect_error(list_unchop(list(1), indices = list(foobar(1L))), class = "vctrs_error_subscript_type") ) Output Error: - ! Must subset elements with a valid subscript vector. - x Subscript must be numeric, not a object. + ! Can't subset elements. + Caused by error: + ! Subscript must be numeric, not a object. # can ignore names in `list_unchop()` by providing a `zap()` name-spec (#232) diff --git a/tests/testthat/_snaps/slice.md b/tests/testthat/_snaps/slice.md index 62fed97f0..1a99298bc 100644 --- a/tests/testthat/_snaps/slice.md +++ b/tests/testthat/_snaps/slice.md @@ -5,16 +5,18 @@ Output Error in `vec_slice()`: - ! Must subset elements with a valid subscript vector. - x `i` must be logical, numeric, or character, not a object. + ! Can't subset elements with `i`. + Caused by error: + ! `i` must be logical, numeric, or character, not a object. Code (expect_error(vec_slice(1:3, matrix(TRUE, nrow = 1)), class = "vctrs_error_subscript_type") ) Output Error in `vec_slice()`: - ! Must subset elements with a valid subscript vector. - x Subscript `i` must be a simple vector, not a matrix. + ! Can't subset elements with `i`. + Caused by error: + ! `i` must be a simple vector, not a matrix. # can't index beyond the end of a vector @@ -42,8 +44,9 @@ Output Error: - ! Must subset elements with a valid subscript vector. - x Can't convert from `2^31` to due to loss of precision. + ! Can't subset elements with `2^31`. + Caused by error: + ! Can't convert from `2^31` to due to loss of precision. # Unnamed vector with character subscript is caught @@ -59,9 +62,10 @@ vec_slice(1:3, -c(1L, NA)) Condition Error in `vec_slice()`: - ! Must subset elements with a valid subscript vector. - x Negative locations can't have missing values. - i Subscript `i` has a missing value at location 2. + ! Can't subset elements with `i`. + Caused by error: + ! Negative locations can't have missing values. + x `i` has 1 missing value at location 2. --- @@ -69,7 +73,7 @@ vec_slice(1:3, c(-1L, 1L)) Condition Error in `vec_slice()`: - ! Must subset elements with a valid subscript vector. + ! Can't subset elements with `i`. x Negative and positive locations can't be mixed. i Subscript `i` has a positive value at location 2. diff --git a/tests/testthat/_snaps/subscript-loc.md b/tests/testthat/_snaps/subscript-loc.md index 641f0af34..8fe851989 100644 --- a/tests/testthat/_snaps/subscript-loc.md +++ b/tests/testthat/_snaps/subscript-loc.md @@ -6,54 +6,61 @@ Output Error: - ! Must extract element with a single valid subscript. - x `TRUE` must be numeric or character, not `TRUE`. + ! Can't extract element with `TRUE`. + Caused by error: + ! `TRUE` must be numeric or character, not `TRUE`. Code (expect_error(vec_as_location2(mtcars, 10L), class = "vctrs_error_subscript_type") ) Output Error: - ! Must extract element with a single valid subscript. - x `mtcars` must be numeric or character, not a object. + ! Can't extract element with `mtcars`. + Caused by error: + ! `mtcars` must be numeric or character, not a object. Code (expect_error(vec_as_location2(env(), 10L), class = "vctrs_error_subscript_type") ) Output Error: - ! Must extract element with a single valid subscript. - x `env()` must be numeric or character, not an environment. + ! Can't extract element with `env()`. + Caused by error: + ! `env()` must be numeric or character, not an environment. Code (expect_error(vec_as_location2(foobar(), 10L), class = "vctrs_error_subscript_type") ) Output Error: - ! Must extract element with a single valid subscript. - x `foobar()` must be numeric or character, not a object. + ! Can't extract element with `foobar()`. + Caused by error: + ! `foobar()` must be numeric or character, not a object. Code (expect_error(vec_as_location2(2.5, 10L), class = "vctrs_error_subscript_type")) Output Error: - ! Must extract element with a single valid subscript. - x Can't convert from `2.5` to due to loss of precision. + ! Can't extract element with `2.5`. + Caused by error: + ! Can't convert from `2.5` to due to loss of precision. Code (expect_error(vec_as_location2(Inf, 10L), class = "vctrs_error_subscript_type")) Output Error: - ! Must extract element with a single valid subscript. - x Can't convert from `Inf` to due to loss of precision. + ! Can't extract element with `Inf`. + Caused by error: + ! Can't convert from `Inf` to due to loss of precision. Code (expect_error(vec_as_location2(-Inf, 10L), class = "vctrs_error_subscript_type") ) Output Error: - ! Must extract element with a single valid subscript. - x Can't convert from `-Inf` to due to loss of precision. + ! Can't extract element with `-Inf`. + Caused by error: + ! Can't convert from `-Inf` to due to loss of precision. Code # Idem with custom `arg` (expect_error(vec_as_location2(foobar(), 10L, arg = "foo", call = call( @@ -61,24 +68,27 @@ Output Error in `my_function()`: - ! Must extract element with a single valid subscript. - x `foo` must be numeric or character, not a object. + ! Can't extract element with `foo`. + Caused by error: + ! `foo` must be numeric or character, not a object. Code (expect_error(vec_as_location2(2.5, 3L, arg = "foo", call = call("my_function")), class = "vctrs_error_subscript_type")) Output Error in `my_function()`: - ! Must extract element with a single valid subscript. - x Can't convert from `foo` to due to loss of precision. + ! Can't extract element with `foo`. + Caused by error: + ! Can't convert from `foo` to due to loss of precision. Code (expect_error(with_tibble_rows(vec_as_location2(TRUE)), class = "vctrs_error_subscript_type") ) Output Error: - ! Must remove row with a single valid subscript. - x `foo(bar)` must be numeric or character, not `TRUE`. + ! Can't remove row with `foo(bar)`. + Caused by error: + ! `foo(bar)` must be numeric or character, not `TRUE`. # vec_as_location() requires integer, character, or logical inputs @@ -88,55 +98,62 @@ Output Error: - ! Must subset elements with a valid subscript vector. - x `mtcars` must be logical, numeric, or character, not a object. + ! Can't subset elements with `mtcars`. + Caused by error: + ! `mtcars` must be logical, numeric, or character, not a object. Code (expect_error(vec_as_location(env(), 10L), class = "vctrs_error_subscript_type") ) Output Error: - ! Must subset elements with a valid subscript vector. - x `env()` must be logical, numeric, or character, not an environment. + ! Can't subset elements with `env()`. + Caused by error: + ! `env()` must be logical, numeric, or character, not an environment. Code (expect_error(vec_as_location(foobar(), 10L), class = "vctrs_error_subscript_type") ) Output Error: - ! Must subset elements with a valid subscript vector. - x `foobar()` must be logical, numeric, or character, not a object. + ! Can't subset elements with `foobar()`. + Caused by error: + ! `foobar()` must be logical, numeric, or character, not a object. Code (expect_error(vec_as_location(2.5, 10L), class = "vctrs_error_subscript_type")) Output Error: - ! Must subset elements with a valid subscript vector. - x Can't convert from `2.5` to due to loss of precision. + ! Can't subset elements with `2.5`. + Caused by error: + ! Can't convert from `2.5` to due to loss of precision. Code (expect_error(vec_as_location(list(), 10L), class = "vctrs_error_subscript_type") ) Output Error: - ! Must subset elements with a valid subscript vector. - x `list()` must be logical, numeric, or character, not an empty list. + ! Can't subset elements with `list()`. + Caused by error: + ! `list()` must be logical, numeric, or character, not an empty list. Code (expect_error(vec_as_location(function() NULL, 10L), class = "vctrs_error_subscript_type") ) Output Error: - ! Must subset elements with a valid subscript vector. - x `function() NULL` must be logical, numeric, or character, not a function. + ! Can't subset elements with `function() NULL`. + Caused by error: + ! `function() NULL` must be logical, numeric, or character, not a function. Code (expect_error(vec_as_location(Sys.Date(), 3L), class = "vctrs_error_subscript_type") ) Output Error: - ! Must subset elements with a valid subscript vector. - x `Sys.Date()` must be logical, numeric, or character, not a object. + ! Can't subset elements with `Sys.Date()`. + Caused by error: + ! `Sys.Date()` must be logical, numeric, or character, not a object. Code # Idem with custom `arg` (expect_error(vec_as_location(env(), 10L, arg = "foo", call = call( @@ -144,24 +161,27 @@ Output Error in `my_function()`: - ! Must subset elements with a valid subscript vector. - x `foo` must be logical, numeric, or character, not an environment. + ! Can't subset elements with `foo`. + Caused by error: + ! `foo` must be logical, numeric, or character, not an environment. Code (expect_error(vec_as_location(foobar(), 10L, arg = "foo", call = call( "my_function")), class = "vctrs_error_subscript_type")) Output Error in `my_function()`: - ! Must subset elements with a valid subscript vector. - x `foo` must be logical, numeric, or character, not a object. + ! Can't subset elements with `foo`. + Caused by error: + ! `foo` must be logical, numeric, or character, not a object. Code (expect_error(vec_as_location(2.5, 3L, arg = "foo", call = call("my_function")), class = "vctrs_error_subscript_type")) Output Error in `my_function()`: - ! Must subset elements with a valid subscript vector. - x Can't convert from `foo` to due to loss of precision. + ! Can't subset elements with `foo`. + Caused by error: + ! Can't convert from `foo` to due to loss of precision. # vec_as_location() and variants check for OOB elements (#1605) @@ -223,16 +243,18 @@ Output Error: - ! Must extract element with a single valid subscript. - x Subscript `1:2` must be size 1, not 2. + ! Can't extract element with `1:2`. + Caused by error: + ! `1:2` must be size 1, not 2. Code (expect_error(vec_as_location2(c("foo", "bar"), 2L, c("foo", "bar")), class = "vctrs_error_subscript_type") ) Output Error: - ! Must extract element with a single valid subscript. - x Subscript `c("foo", "bar")` must be size 1, not 2. + ! Can't extract element with `c("foo", "bar")`. + Caused by error: + ! `c("foo", "bar")` must be size 1, not 2. Code # Idem with custom `arg` (expect_error(vec_as_location2(1:2, 2L, arg = "foo", call = call("my_function")), @@ -240,24 +262,27 @@ Output Error in `my_function()`: - ! Must extract element with a single valid subscript. - x Subscript `foo` must be size 1, not 2. + ! Can't extract element with `foo`. + Caused by error: + ! `foo` must be size 1, not 2. Code (expect_error(vec_as_location2(mtcars, 10L, arg = "foo", call = call( "my_function")), class = "vctrs_error_subscript_type")) Output Error in `my_function()`: - ! Must extract element with a single valid subscript. - x `foo` must be numeric or character, not a object. + ! Can't extract element with `foo`. + Caused by error: + ! `foo` must be numeric or character, not a object. Code (expect_error(vec_as_location2(1:2, 2L, arg = "foo", call = call("my_function")), class = "vctrs_error_subscript_type")) Output Error in `my_function()`: - ! Must extract element with a single valid subscript. - x Subscript `foo` must be size 1, not 2. + ! Can't extract element with `foo`. + Caused by error: + ! `foo` must be size 1, not 2. # vec_as_location2() requires positive integers @@ -266,15 +291,17 @@ Output Error: - ! Must extract element with a single valid subscript. - x Subscript `0` must be a positive location, not 0. + ! Can't extract element with `0`. + Caused by error: + ! `0` must be a positive location, not 0. Code (expect_error(vec_as_location2(-1, 2L), class = "vctrs_error_subscript_type")) Output Error: - ! Must extract element with a single valid subscript. - x Subscript `-1` must be a positive location, not -1. + ! Can't extract element with `-1`. + Caused by error: + ! `-1` must be a positive location, not -1. Code # Idem with custom `arg` (expect_error(vec_as_location2(0, 2L, arg = "foo", call = call("my_function")), @@ -282,8 +309,9 @@ Output Error in `my_function()`: - ! Must extract element with a single valid subscript. - x Subscript `foo` must be a positive location, not 0. + ! Can't extract element with `foo`. + Caused by error: + ! `foo` must be a positive location, not 0. # vec_as_location2() fails with NA @@ -293,16 +321,18 @@ Output Error: - ! Must extract element with a single valid subscript. - x Subscript `na_int` must be a location, not an integer `NA`. + ! Can't extract element with `na_int`. + Caused by error: + ! `na_int` must be a location, not an integer `NA`. Code (expect_error(vec_as_location2(na_chr, 1L, names = "foo"), class = "vctrs_error_subscript_type") ) Output Error: - ! Must extract element with a single valid subscript. - x Subscript `na_chr` must be a location, not a character `NA`. + ! Can't extract element with `na_chr`. + Caused by error: + ! `na_chr` must be a location, not a character `NA`. Code # Idem with custom `arg` (expect_error(vec_as_location2(na_int, 2L, arg = "foo", call = call( @@ -310,8 +340,9 @@ Output Error in `my_function()`: - ! Must extract element with a single valid subscript. - x Subscript `foo` must be a location, not an integer `NA`. + ! Can't extract element with `foo`. + Caused by error: + ! `foo` must be a location, not an integer `NA`. # num_as_location() optionally forbids negative indices @@ -321,8 +352,9 @@ Output Error: - ! Must subset elements with a valid subscript vector. - x Subscript `dbl(1, -1)` can't contain negative locations. + ! Can't subset elements with `dbl(1, -1)`. + Caused by error: + ! `dbl(1, -1)` can't contain negative locations. # num_as_location() optionally forbids zero indices @@ -332,8 +364,9 @@ Output Error: - ! Must subset elements with a valid subscript vector. - x Subscript `0L` can't contain `0` values. + ! Can't subset elements with `0L`. + Caused by error: + ! `0L` can't contain `0` values. i It has a `0` value at location 1. Code (expect_error(num_as_location(c(0, 0, 0, 0, 0, 0), 1, zero = "error"), class = "vctrs_error_subscript_type") @@ -341,8 +374,9 @@ Output Error: - ! Must subset elements with a valid subscript vector. - x Subscript `c(0, 0, 0, 0, 0, 0)` can't contain `0` values. + ! Can't subset elements with `c(0, 0, 0, 0, 0, 0)`. + Caused by error: + ! `c(0, 0, 0, 0, 0, 0)` can't contain `0` values. i It has 6 `0` values at locations 1, 2, 3, 4, 5, etc. # vec_as_location() checks for mix of negative and missing locations @@ -353,18 +387,20 @@ Output Error: - ! Must subset elements with a valid subscript vector. - x Negative locations can't have missing values. - i Subscript `-c(1L, NA)` has a missing value at location 2. + ! Can't subset elements with `-c(1L, NA)`. + Caused by error: + ! Negative locations can't have missing values. + x `-c(1L, NA)` has 1 missing value at location 2. Code (expect_error(vec_as_location(-c(1L, rep(NA, 10)), 30), class = "vctrs_error_subscript_type") ) Output Error: - ! Must subset elements with a valid subscript vector. - x Negative locations can't have missing values. - i Subscript `-c(1L, rep(NA, 10))` has 10 missing values at locations 2, 3, 4, 5, 6, etc. + ! Can't subset elements with `-c(1L, rep(NA, 10))`. + Caused by error: + ! Negative locations can't have missing values. + x `-c(1L, rep(NA, 10))` has 10 missing values at locations 2, 3, 4, 5, 6, 7, 8, 9, 10, and 11. # vec_as_location() checks for mix of negative and positive locations @@ -374,7 +410,7 @@ Output Error: - ! Must subset elements with a valid subscript vector. + ! Can't subset elements with `c(-1L, 1L)`. x Negative and positive locations can't be mixed. i Subscript `c(-1L, 1L)` has a positive value at location 2. Code @@ -383,7 +419,7 @@ Output Error: - ! Must subset elements with a valid subscript vector. + ! Can't subset elements with `c(-1L, rep(1L, 10))`. x Negative and positive locations can't be mixed. i Subscript `c(-1L, rep(1L, 10))` has 10 positive values at locations 2, 3, 4, 5, 6, etc. @@ -395,8 +431,9 @@ Output Error: - ! Must subset elements with a valid subscript vector. - x Logical subscript `c(TRUE, FALSE)` must be size 1 or 3, not 2. + ! Can't subset elements with `c(TRUE, FALSE)`. + Caused by error: + ! Logical subscript `c(TRUE, FALSE)` must be size 1 or 3, not 2. # character subscripts require named vectors @@ -481,8 +518,9 @@ num_as_location(c(0, -1), n = 2L, negative = "invert", zero = "error") Condition Error: - ! Must subset elements with a valid subscript vector. - x Subscript `c(0, -1)` can't contain `0` values. + ! Can't subset elements with `c(0, -1)`. + Caused by error: + ! `c(0, -1)` can't contain `0` values. i It has a `0` value at location 1. --- @@ -491,8 +529,9 @@ num_as_location(c(-1, 0), n = 2L, negative = "invert", zero = "error") Condition Error: - ! Must subset elements with a valid subscript vector. - x Subscript `c(-1, 0)` can't contain `0` values. + ! Can't subset elements with `c(-1, 0)`. + Caused by error: + ! `c(-1, 0)` can't contain `0` values. i It has a `0` value at location 2. # num_as_location() with `oob = 'extend'` doesn't allow ignored oob negative values (#1614) @@ -564,8 +603,9 @@ Output Error: - ! Must subset elements with a valid subscript vector. - x Subscript can't contain missing values. + ! Can't subset elements. + Caused by error: + ! Subscript can't contain missing values. x It has a missing value at location 2. Code (expect_error(vec_as_location(c(1, NA, 2, NA), 2, missing = "error", arg = "foo", @@ -573,8 +613,9 @@ Output Error in `my_function()`: - ! Must subset elements with a valid subscript vector. - x Subscript can't contain missing values. + ! Can't subset elements. + Caused by error: + ! Subscript can't contain missing values. x It has missing values at locations 2 and 4. Code (expect_error(with_tibble_cols(vec_as_location(c(1, NA, 2, NA), 2, missing = "error")), @@ -582,8 +623,9 @@ Output Error: - ! Must rename columns with a valid subscript vector. - x Subscript `foo(bar)` can't contain missing values. + ! Can't rename columns with `foo(bar)`. + Caused by error: + ! `foo(bar)` can't contain missing values. x It has missing values at locations 2 and 4. Code (expect_error(with_tibble_cols(vec_as_location(NA, 1, missing = "error")), @@ -591,8 +633,9 @@ Output Error: - ! Must rename columns with a valid subscript vector. - x Subscript `foo(bar)` can't contain missing values. + ! Can't rename columns with `foo(bar)`. + Caused by error: + ! `foo(bar)` can't contain missing values. x It has a missing value at location 1. Code (expect_error(with_tibble_cols(vec_as_location(NA, 3, missing = "error")), @@ -600,8 +643,9 @@ Output Error: - ! Must rename columns with a valid subscript vector. - x Subscript `foo(bar)` can't contain missing values. + ! Can't rename columns with `foo(bar)`. + Caused by error: + ! `foo(bar)` can't contain missing values. x It has a missing value at location 1. Code (expect_error(with_tibble_cols(vec_as_location(c(TRUE, NA, FALSE), 3, missing = "error")), @@ -609,8 +653,9 @@ Output Error: - ! Must rename columns with a valid subscript vector. - x Subscript `foo(bar)` can't contain missing values. + ! Can't rename columns with `foo(bar)`. + Caused by error: + ! `foo(bar)` can't contain missing values. x It has a missing value at location 2. Code (expect_error(with_tibble_cols(vec_as_location(NA_character_, 2, missing = "error", @@ -618,8 +663,9 @@ Output Error: - ! Must rename columns with a valid subscript vector. - x Subscript `foo(bar)` can't contain missing values. + ! Can't rename columns with `foo(bar)`. + Caused by error: + ! `foo(bar)` can't contain missing values. x It has a missing value at location 1. # can alter logical missing value handling (#1595) @@ -628,8 +674,9 @@ vec_as_location(x, n = 4L, missing = "error") Condition Error: - ! Must subset elements with a valid subscript vector. - x Subscript can't contain missing values. + ! Can't subset elements. + Caused by error: + ! Subscript can't contain missing values. x It has missing values at locations 2 and 4. --- @@ -638,8 +685,9 @@ vec_as_location(x, n = 2L, missing = "error") Condition Error: - ! Must subset elements with a valid subscript vector. - x Subscript can't contain missing values. + ! Can't subset elements. + Caused by error: + ! Subscript can't contain missing values. x It has a missing value at location 1. # can alter character missing value handling (#1595) @@ -648,8 +696,9 @@ vec_as_location(x, n = 2L, names = names, missing = "error") Condition Error: - ! Must subset elements with a valid subscript vector. - x Subscript can't contain missing values. + ! Can't subset elements. + Caused by error: + ! Subscript can't contain missing values. x It has missing values at locations 1 and 3. # can alter integer missing value handling (#1595) @@ -658,8 +707,9 @@ vec_as_location(x, n = 4L, missing = "error") Condition Error: - ! Must subset elements with a valid subscript vector. - x Subscript can't contain missing values. + ! Can't subset elements. + Caused by error: + ! Subscript can't contain missing values. x It has missing values at locations 1 and 3. # can alter negative integer missing value handling (#1595) @@ -668,9 +718,10 @@ num_as_location(x, n = 4L, missing = "propagate", negative = "invert") Condition Error: - ! Must subset elements with a valid subscript vector. - x Negative locations can't have missing values. - i Subscript `x` has 2 missing values at locations 2 and 3. + ! Can't subset elements with `x`. + Caused by error: + ! Negative locations can't have missing values. + x `x` has 2 missing values at locations 2 and 3. --- @@ -678,9 +729,10 @@ num_as_location(x, n = 4L, missing = "error", negative = "invert") Condition Error: - ! Must subset elements with a valid subscript vector. - x Negative locations can't have missing values. - i Subscript `x` has 2 missing values at locations 2 and 3. + ! Can't subset elements with `x`. + Caused by error: + ! Negative locations can't have missing values. + x `x` has 2 missing values at locations 2 and 3. # empty string character indices never match empty string names (#1489) @@ -688,8 +740,9 @@ vec_as_location("", n = 2L, names = names) Condition Error: - ! Must subset elements with a valid subscript vector. - x Subscript can't contain the empty string. + ! Can't subset elements. + Caused by error: + ! Subscript can't contain the empty string. x It has an empty string at location 1. --- @@ -698,8 +751,9 @@ vec_as_location(c("", "y", ""), n = 2L, names = names) Condition Error: - ! Must subset elements with a valid subscript vector. - x Subscript can't contain the empty string. + ! Can't subset elements. + Caused by error: + ! Subscript can't contain the empty string. x It has an empty string at locations 1 and 3. # can customise subscript type errors @@ -711,64 +765,71 @@ Output Error in `my_function()`: - ! Must subset elements with a valid subscript vector. - x Subscript `foo` can't contain negative locations. + ! Can't subset elements with `foo`. + Caused by error: + ! `foo` can't contain negative locations. Code (expect_error(num_as_location2(-1, 2, negative = "error", arg = "foo", call = call( "my_function")), class = "vctrs_error_subscript_type")) Output Error in `my_function()`: - ! Must extract element with a single valid subscript. - x Subscript `foo` must be a positive location, not -1. + ! Can't extract element with `foo`. + Caused by error: + ! `foo` must be a positive location, not -1. Code (expect_error(vec_as_location2(0, 2, arg = "foo", call = call("my_function")), class = "vctrs_error_subscript_type")) Output Error in `my_function()`: - ! Must extract element with a single valid subscript. - x Subscript `foo` must be a positive location, not 0. + ! Can't extract element with `foo`. + Caused by error: + ! `foo` must be a positive location, not 0. Code (expect_error(vec_as_location2(na_dbl, 2, arg = "foo", call = call( "my_function")), class = "vctrs_error_subscript_type")) Output Error in `my_function()`: - ! Must extract element with a single valid subscript. - x Subscript `foo` must be a location, not an integer `NA`. + ! Can't extract element with `foo`. + Caused by error: + ! `foo` must be a location, not an integer `NA`. Code (expect_error(vec_as_location2(c(1, 2), 2, arg = "foo", call = call( "my_function")), class = "vctrs_error_subscript_type")) Output Error in `my_function()`: - ! Must extract element with a single valid subscript. - x Subscript `foo` must be size 1, not 2. + ! Can't extract element with `foo`. + Caused by error: + ! `foo` must be size 1, not 2. Code (expect_error(vec_as_location(c(TRUE, FALSE), 3, arg = "foo", call = call( "my_function")), class = "vctrs_error_subscript_size")) Output Error in `my_function()`: - ! Must subset elements with a valid subscript vector. - x Logical subscript `foo` must be size 1 or 3, not 2. + ! Can't subset elements with `foo`. + Caused by error: + ! Logical subscript `foo` must be size 1 or 3, not 2. Code (expect_error(vec_as_location(c(-1, NA), 3, arg = "foo", call = call( "my_function")), class = "vctrs_error_subscript_type")) Output Error in `my_function()`: - ! Must subset elements with a valid subscript vector. - x Negative locations can't have missing values. - i Subscript `foo` has a missing value at location 2. + ! Can't subset elements with `foo`. + Caused by error: + ! Negative locations can't have missing values. + x `foo` has 1 missing value at location 2. Code (expect_error(vec_as_location(c(-1, 1), 3, arg = "foo", call = call( "my_function")), class = "vctrs_error_subscript_type")) Output Error in `my_function()`: - ! Must subset elements with a valid subscript vector. + ! Can't subset elements with `foo`. x Negative and positive locations can't be mixed. i Subscript `foo` has a positive value at location 2. Code @@ -786,8 +847,9 @@ Output Error in `my_function()`: - ! Must subset elements with a valid subscript vector. - x Subscript `foo` can't contain `0` values. + ! Can't subset elements with `foo`. + Caused by error: + ! `foo` can't contain `0` values. i It has a `0` value at location 1. Code # With tibble columns @@ -796,64 +858,71 @@ Output Error: - ! Must rename columns with a valid subscript vector. - x Subscript `foo(bar)` can't contain negative locations. + ! Can't rename columns with `foo(bar)`. + Caused by error: + ! `foo(bar)` can't contain negative locations. Code (expect_error(with_tibble_cols(num_as_location2(-1, 2, negative = "error")), class = "vctrs_error_subscript_type")) Output Error: - ! Must rename column with a single valid subscript. - x Subscript `foo(bar)` must be a positive location, not -1. + ! Can't rename column with `foo(bar)`. + Caused by error: + ! `foo(bar)` must be a positive location, not -1. Code (expect_error(with_tibble_cols(vec_as_location2(0, 2)), class = "vctrs_error_subscript_type") ) Output Error: - ! Must rename column with a single valid subscript. - x Subscript `foo(bar)` must be a positive location, not 0. + ! Can't rename column with `foo(bar)`. + Caused by error: + ! `foo(bar)` must be a positive location, not 0. Code (expect_error(with_tibble_cols(vec_as_location2(na_dbl, 2)), class = "vctrs_error_subscript_type") ) Output Error: - ! Must rename column with a single valid subscript. - x Subscript `foo(bar)` must be a location, not an integer `NA`. + ! Can't rename column with `foo(bar)`. + Caused by error: + ! `foo(bar)` must be a location, not an integer `NA`. Code (expect_error(with_tibble_cols(vec_as_location2(c(1, 2), 2)), class = "vctrs_error_subscript_type") ) Output Error: - ! Must rename column with a single valid subscript. - x Subscript `foo(bar)` must be size 1, not 2. + ! Can't rename column with `foo(bar)`. + Caused by error: + ! `foo(bar)` must be size 1, not 2. Code (expect_error(with_tibble_cols(vec_as_location(c(TRUE, FALSE), 3)), class = "vctrs_error_subscript_size") ) Output Error: - ! Must rename columns with a valid subscript vector. - x Logical subscript `foo(bar)` must be size 1 or 3, not 2. + ! Can't rename columns with `foo(bar)`. + Caused by error: + ! Logical subscript `c(TRUE, FALSE)` must be size 1 or 3, not 2. Code (expect_error(with_tibble_cols(vec_as_location(c(-1, NA), 3)), class = "vctrs_error_subscript_type") ) Output Error: - ! Must rename columns with a valid subscript vector. - x Negative locations can't have missing values. - i Subscript `foo(bar)` has a missing value at location 2. + ! Can't rename columns with `foo(bar)`. + Caused by error: + ! Negative locations can't have missing values. + x `foo(bar)` has 1 missing value at location 2. Code (expect_error(with_tibble_cols(vec_as_location(c(-1, 1), 3)), class = "vctrs_error_subscript_type") ) Output Error: - ! Must rename columns with a valid subscript vector. + ! Can't rename columns with `foo(bar)`. x Negative and positive locations can't be mixed. i Subscript `foo(bar)` has a positive value at location 2. Code @@ -871,8 +940,9 @@ Output Error: - ! Must rename columns with a valid subscript vector. - x Subscript `foo(bar)` can't contain `0` values. + ! Can't rename columns with `foo(bar)`. + Caused by error: + ! `foo(bar)` can't contain `0` values. i It has a `0` value at location 1. # can customise OOB errors @@ -1020,24 +1090,27 @@ Output Error: - ! Must subset elements with a valid subscript vector. - x Subscript `matrix(TRUE, nrow = 1)` must be a simple vector, not a matrix. + ! Can't subset elements with `matrix(TRUE, nrow = 1)`. + Caused by error: + ! `matrix(TRUE, nrow = 1)` must be a simple vector, not a matrix. Code (expect_error(vec_as_location(array(TRUE, dim = c(1, 1, 1)), 3L), class = "vctrs_error_subscript_type") ) Output Error: - ! Must subset elements with a valid subscript vector. - x Subscript `array(TRUE, dim = c(1, 1, 1))` must be a simple vector, not an array. + ! Can't subset elements with `array(TRUE, dim = c(1, 1, 1))`. + Caused by error: + ! `array(TRUE, dim = c(1, 1, 1))` must be a simple vector, not an array. Code (expect_error(with_tibble_rows(vec_as_location(matrix(TRUE, nrow = 1), 3L)), class = "vctrs_error_subscript_type")) Output Error: - ! Must remove rows with a valid subscript vector. - x Subscript `foo(bar)` must be a simple vector, not a matrix. + ! Can't remove rows with `foo(bar)`. + Caused by error: + ! `foo(bar)` must be a simple vector, not a matrix. # vec_as_location() UI diff --git a/tests/testthat/_snaps/subscript.md b/tests/testthat/_snaps/subscript.md index 1e0a13fa4..7385e7810 100644 --- a/tests/testthat/_snaps/subscript.md +++ b/tests/testthat/_snaps/subscript.md @@ -6,8 +6,9 @@ Output Error: - ! Must rename columns with a valid subscript vector. - x `foo(bar)` must be logical, numeric, or character, not an environment. + ! Can't rename columns with `foo(bar)`. + Caused by error: + ! `foo(bar)` must be logical, numeric, or character, not an environment. --- @@ -17,8 +18,9 @@ Output Error: - ! Must extract tables with a valid subscript vector. - x `foo(bar)` must be logical, numeric, or character, not an environment. + ! Can't extract tables with `foo(bar)`. + Caused by error: + ! `foo(bar)` must be logical, numeric, or character, not an environment. # vec_as_subscript() checks dimensionality @@ -28,24 +30,27 @@ Output Error: - ! Must subset elements with a valid subscript vector. - x Subscript must be a simple vector, not a matrix. + ! Can't subset elements. + Caused by error: + ! Subscript must be a simple vector, not a matrix. Code (expect_error(vec_as_subscript(array(TRUE, dim = c(1, 1, 1))), class = "vctrs_error_subscript_type") ) Output Error: - ! Must subset elements with a valid subscript vector. - x Subscript must be a simple vector, not an array. + ! Can't subset elements. + Caused by error: + ! Subscript must be a simple vector, not an array. Code (expect_error(with_tibble_rows(vec_as_subscript(matrix(TRUE, nrow = 1))), class = "vctrs_error_subscript_type")) Output Error: - ! Must remove rows with a valid subscript vector. - x Subscript `foo(bar)` must be a simple vector, not a matrix. + ! Can't remove rows with `foo(bar)`. + Caused by error: + ! `foo(bar)` must be a simple vector, not a matrix. # vec_as_subscript() forbids subscript types @@ -53,8 +58,9 @@ vec_as_subscript(1L, logical = "error", numeric = "error") Condition Error: - ! Must subset elements with a valid subscript vector. - x Subscript must be character, not the number 1. + ! Can't subset elements. + Caused by error: + ! Subscript must be character, not the number 1. --- @@ -62,8 +68,9 @@ vec_as_subscript("foo", logical = "error", character = "error") Condition Error: - ! Must subset elements with a valid subscript vector. - x Subscript must be numeric, not the string "foo". + ! Can't subset elements. + Caused by error: + ! Subscript must be numeric, not the string "foo". --- @@ -71,8 +78,9 @@ vec_as_subscript(TRUE, logical = "error") Condition Error: - ! Must subset elements with a valid subscript vector. - x Subscript must be numeric or character, not `TRUE`. + ! Can't subset elements. + Caused by error: + ! Subscript must be numeric or character, not `TRUE`. --- @@ -80,8 +88,9 @@ vec_as_subscript("foo", character = "error") Condition Error: - ! Must subset elements with a valid subscript vector. - x Subscript must be logical or numeric, not the string "foo". + ! Can't subset elements. + Caused by error: + ! Subscript must be logical or numeric, not the string "foo". --- @@ -89,8 +98,9 @@ vec_as_subscript(NULL, numeric = "error") Condition Error: - ! Must subset elements with a valid subscript vector. - x Subscript must be logical or character, not `NULL`. + ! Can't subset elements. + Caused by error: + ! Subscript must be logical or character, not `NULL`. --- @@ -98,8 +108,9 @@ vec_as_subscript(quote(foo), character = "error") Condition Error: - ! Must subset elements with a valid subscript vector. - x Subscript must be logical or numeric, not a symbol. + ! Can't subset elements. + Caused by error: + ! Subscript must be logical or numeric, not a symbol. # vec_as_subscript2() forbids subscript types @@ -107,8 +118,9 @@ vec_as_subscript2(1L, numeric = "error") Condition Error: - ! Must extract element with a single valid subscript. - x Subscript must be character, not the number 1. + ! Can't extract element. + Caused by error: + ! Subscript must be character, not the number 1. --- @@ -116,8 +128,9 @@ vec_as_subscript2("foo", character = "error") Condition Error: - ! Must extract element with a single valid subscript. - x Subscript must be numeric, not the string "foo". + ! Can't extract element. + Caused by error: + ! Subscript must be numeric, not the string "foo". --- @@ -125,8 +138,9 @@ vec_as_subscript2(TRUE) Condition Error: - ! Must extract element with a single valid subscript. - x Subscript must be numeric or character, not `TRUE`. + ! Can't extract element. + Caused by error: + ! Subscript must be numeric or character, not `TRUE`. # vec_as_subscript2() retains the call when throwing vec_as_subscript() errors (#1605) @@ -134,8 +148,9 @@ vec_as_subscript2(1L, numeric = "error", call = call("foo")) Condition Error in `foo()`: - ! Must extract element with a single valid subscript. - x Subscript must be character, not the number 1. + ! Can't extract element. + Caused by error: + ! Subscript must be character, not the number 1. --- @@ -143,8 +158,9 @@ vec_as_subscript2(1.5, call = call("foo")) Condition Error in `foo()`: - ! Must extract element with a single valid subscript. - x Can't convert from to due to loss of precision. + ! Can't extract element. + Caused by error: + ! Can't convert from to due to loss of precision. # vec_as_subscript2() retains the call when erroring on logical input (#1605) @@ -152,8 +168,9 @@ vec_as_subscript2(TRUE, call = call("foo")) Condition Error in `foo()`: - ! Must extract element with a single valid subscript. - x Subscript must be numeric or character, not `TRUE`. + ! Can't extract element. + Caused by error: + ! Subscript must be numeric or character, not `TRUE`. # `logical = 'cast'` is deprecated @@ -169,8 +186,9 @@ vec_as_subscript2(TRUE, logical = "error") Condition Error: - ! Must extract element with a single valid subscript. - x Subscript must be numeric or character, not `TRUE`. + ! Can't extract element. + Caused by error: + ! Subscript must be numeric or character, not `TRUE`. # lossy cast errors for scalar subscripts work (#1606) @@ -178,6 +196,7 @@ vec_as_subscript2(1.5) Condition Error: - ! Must extract element with a single valid subscript. - x Can't convert from to due to loss of precision. + ! Can't extract element. + Caused by error: + ! Can't convert from to due to loss of precision.