Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass calls for improved error messages #1340

Merged
merged 23 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions R/chop.R
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,17 @@ unchop <- function(data, cols, keep_empty = FALSE, ptype = NULL) {
# used to slice the data frame `x` was subset from to align it with `val`.
# - `val` the unchopped data frame.

df_unchop <- function(x, ..., ptype = NULL, keep_empty = FALSE) {
df_unchop <- function(x, ..., ptype = NULL, keep_empty = FALSE, call = caller_env()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our emerging convention when passing error calls through as arguments seems to be:

  • Name them call and arg when the function's main purpose is for some kind of input checking, i.e. vec_assert(), check_*() functions, abort(), etc.

  • Name them error_call and error_arg otherwise.

Since df_unchop()'s main purpose is unchopping, it should use error_call. The rest of the PR probably needs a pass to do this updating too. I'll try and mark them as I see them.

We have started doing this in vctrs, tidyselect, internally in dplyr, and I've done it in ivs, so probably best to take the time to be consistent here since call improvements is what this PR is all about.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW I don't like this convention because it means there are 4 names it could be: call, .call, error_call, or .error_call. I'd prefer to use error_call everywhere.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means we need to rename a lot of already exported functions? Isn't it a bit late to change this now?

By the way I don't consider a dotted variant to represent a different name. I see it as different syntax for the same name.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I don't think of them as different names either, but pragmatically you have to remember which of 4 options it is.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only in the terminal right? In IDEs you immediately see if you have a function that requires dotted arguments. It also shows you whether you need call or error_call.

check_dots_empty()

if (!is.data.frame(x)) {
abort("`x` must be a data frame.")
abort("`x` must be a data frame.", call = call)
}
if (!is_bool(keep_empty)) {
abort("`keep_empty` must be a single `TRUE` or `FALSE`.")
abort("`keep_empty` must be a single `TRUE` or `FALSE`.", call = call)
}

ptype <- check_list_of_ptypes(ptype, names = names(x), arg = "ptype")
ptype <- check_list_of_ptypes(ptype, names = names(x), arg = "ptype", call = call)

size <- vec_size(x)

Expand Down Expand Up @@ -201,7 +201,7 @@ df_unchop <- function(x, ..., ptype = NULL, keep_empty = FALSE) {
x_nulls[[i]] <- info$null
}

sizes <- reduce(x_sizes, unchop_sizes2)
sizes <- reduce(x_sizes, unchop_sizes2, call = call)

info <- unchop_finalize(x, sizes, x_nulls, keep_empty)
x <- info$x
Expand All @@ -221,7 +221,7 @@ df_unchop <- function(x, ..., ptype = NULL, keep_empty = FALSE) {

if (!col_is_list) {
if (!is_null(col_ptype)) {
col <- vec_cast(col, col_ptype, x_arg = col_name)
col <- vec_cast(col, col_ptype, x_arg = col_name, call = call)
}
out_cols[[i]] <- vec_slice(col, out_loc)
next
Expand All @@ -237,7 +237,7 @@ df_unchop <- function(x, ..., ptype = NULL, keep_empty = FALSE) {

col_sizes <- x_sizes[[i]]
row_recycle <- col_sizes != sizes
col[row_recycle] <- map2(col[row_recycle], sizes[row_recycle], vec_recycle)
col[row_recycle] <- map2(col[row_recycle], sizes[row_recycle], vec_recycle, call = call)

col <- list_unchop(col, ptype = col_ptype)

Expand All @@ -264,7 +264,7 @@ df_unchop <- function(x, ..., ptype = NULL, keep_empty = FALSE) {
out
}

unchop_sizes2 <- function(x, y) {
unchop_sizes2 <- function(x, y, call) {
hadley marked this conversation as resolved.
Show resolved Hide resolved
# Standard tidyverse recycling rules, just vectorized.

# Recycle `x` values with `y`
Expand All @@ -286,7 +286,7 @@ unchop_sizes2 <- function(x, y) {
row <- which(incompatible)[[1]]
x <- x[[row]]
y <- y[[row]]
abort(glue("In row {row}, can't recycle input of size {x} to size {y}."))
abort(glue("In row {row}, can't recycle input of size {x} to size {y}."), call = call)
}

x
Expand Down
4 changes: 2 additions & 2 deletions R/expand.R
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ expand_grid <- function(..., .name_repair = "check_unique") {
}

# Flattens unnamed data frames after grid expansion
out <- df_list(!!!out, .name_repair = .name_repair)
out <- df_list(!!!out, .name_repair = .name_repair, .error_call = current_env())
out <- tibble::new_tibble(out, nrow = size)

out
Expand Down Expand Up @@ -277,7 +277,7 @@ grid_dots <- function(..., `_data` = NULL) {
}

arg <- paste0("..", i)
vec_assert(dot, arg = arg)
vec_assert(dot, arg = arg, call = caller_env())
hadley marked this conversation as resolved.
Show resolved Hide resolved

out[[i]] <- dot

Expand Down
8 changes: 4 additions & 4 deletions R/extract.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ extract.data.frame <- function(data, col, into, regex = "([[:alnum:]]+)",
reconstruct_tibble(data, out, if (remove) var else chr())
}

str_extract <- function(x, into, regex, convert = FALSE) {
str_extract <- function(x, into, regex, convert = FALSE, call = caller_env()) {
hadley marked this conversation as resolved.
Show resolved Hide resolved
check_not_stringr_pattern(regex, "regex")
hadley marked this conversation as resolved.
Show resolved Hide resolved

stopifnot(
Expand All @@ -60,9 +60,9 @@ str_extract <- function(x, into, regex, convert = FALSE) {

out <- str_match_first(x, regex)
if (length(out) != length(into)) {
stop(
"`regex` should define ", length(into), " groups; ", length(out), " found.",
call. = FALSE
abort(
glue("`regex` should define {length(into)} groups; {length(out)} found."),
call = call
)
}

Expand Down
6 changes: 3 additions & 3 deletions R/hoist.R
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ hoist <- function(.data,
out
}

check_pluckers <- function(...) {
check_pluckers <- function(..., .call = caller_env()) {
hadley marked this conversation as resolved.
Show resolved Hide resolved
pluckers <- list2(...)

is_string <- map_lgl(pluckers, ~ is.character(.x) && length(.x) == 1)
Expand All @@ -144,11 +144,11 @@ check_pluckers <- function(...) {
}

if (length(pluckers) > 0 && !is_named(pluckers)) {
abort("All elements of `...` must be named.")
abort("All elements of `...` must be named.", call = .call)
}

if (vec_duplicate_any(names(pluckers))) {
abort("The names of `...` must be unique.")
abort("The names of `...` must be unique.", call = .call)
}

# Standardize all pluckers to lists for splicing into `pluck()`
Expand Down
3 changes: 2 additions & 1 deletion R/pivot-long.R
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,8 @@ pivot_longer_spec <- function(data,
data_cols,
keys,
vals,
.name_repair = names_repair
.name_repair = names_repair,
.error_call = current_env()
))

if (values_drop_na) {
Expand Down
32 changes: 18 additions & 14 deletions R/pivot-wide.R
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,8 @@ pivot_wider_spec <- function(data,
rows,
values,
unused,
.name_repair = names_repair
.name_repair = names_repair,
.error_call = current_env()
))

reconstruct_tibble(input, out)
Expand Down Expand Up @@ -525,17 +526,19 @@ build_wider_spec <- function(data,
build_wider_id_cols_expr <- function(data,
id_cols = NULL,
names_from = name,
values_from = value) {
values_from = value,
call = caller_env()) {
hadley marked this conversation as resolved.
Show resolved Hide resolved
# TODO: Use `allow_rename = FALSE`.
# Requires https://github.com/r-lib/tidyselect/issues/225.
names_from_cols <- names(tidyselect::eval_select(enquo(names_from), data))
values_from_cols <- names(tidyselect::eval_select(enquo(values_from), data))
names_from_cols <- names(tidyselect::eval_select(enquo(names_from), data, error_call = call))
values_from_cols <- names(tidyselect::eval_select(enquo(values_from), data, error_call = call))

out <- select_wider_id_cols(
data = data,
id_cols = {{ id_cols }},
names_from_cols = names_from_cols,
values_from_cols = values_from_cols
values_from_cols = values_from_cols,
call = caller_env()
hadley marked this conversation as resolved.
Show resolved Hide resolved
)

expr(c(!!!out))
Expand All @@ -544,7 +547,8 @@ build_wider_id_cols_expr <- function(data,
select_wider_id_cols <- function(data,
id_cols = NULL,
names_from_cols = character(),
values_from_cols = character()) {
values_from_cols = character(),
call = caller_env()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
call = caller_env()) {
error_call = caller_env()) {

id_cols <- enquo(id_cols)

# Remove known non-id-cols so they are never selected
Expand All @@ -558,38 +562,38 @@ select_wider_id_cols <- function(data,
try_fetch(
# TODO: Use `allow_rename = FALSE`.
# Requires https://github.com/r-lib/tidyselect/issues/225.
id_cols <- tidyselect::eval_select(enquo(id_cols), data),
id_cols <- tidyselect::eval_select(enquo(id_cols), data, error_call = call),
vctrs_error_subscript_oob = function(cnd) {
rethrow_id_cols_oob(cnd, names_from_cols, values_from_cols)
rethrow_id_cols_oob(cnd, names_from_cols, values_from_cols, call)
}
)

names(id_cols)
}

rethrow_id_cols_oob <- function(cnd, names_from_cols, values_from_cols) {
rethrow_id_cols_oob <- function(cnd, names_from_cols, values_from_cols, call) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

call is fine here, main purpose is error related

i <- cnd[["i"]]

if (!is_string(i)) {
abort("`i` is expected to be a string.", .internal = TRUE)
}

if (i %in% names_from_cols) {
stop_id_cols_oob(i, "names_from")
stop_id_cols_oob(i, "names_from", call = call)
} else if (i %in% values_from_cols) {
stop_id_cols_oob(i, "values_from")
stop_id_cols_oob(i, "values_from", call = call)
} else {
# Zap this special handler, throw the normal condition
zap()
}
}

stop_id_cols_oob <- function(i, arg) {
stop_id_cols_oob <- function(i, arg, call) {
message <- c(
glue("`id_cols` can't select a column already selected by `{arg}`."),
i = glue("Column `{i}` has already been selected.")
)
abort(message, parent = NA)
abort(message, parent = NA, call = call)
}

# Helpers -----------------------------------------------------------------
Expand Down Expand Up @@ -618,7 +622,7 @@ value_summarize <- function(value, value_locs, value_name, fn, fn_name) {
x = glue("Applying `{fn_name}` resulted in a value with length {size}.")
)

abort(c(header, bullet))
abort(c(header, bullet), call = caller_env())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably worth making error_call = caller_env() an argument to value_summarize()

}

value <- vec_c(!!!value)
Expand Down
10 changes: 5 additions & 5 deletions R/pivot.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@
#' check_pivot_spec(spec)
check_pivot_spec <- function(spec) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hadley do you think this exported check_*() function should get a call argument?

Or is using caller_env() like this "good enough"?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it probably should

if (!is.data.frame(spec)) {
abort("`spec` must be a data frame.")
abort("`spec` must be a data frame.", call = caller_env())
}

if (!has_name(spec, ".name") || !has_name(spec, ".value")) {
abort("`spec` must have `.name` and `.value` columns.")
abort("`spec` must have `.name` and `.value` columns.", call = caller_env())
}

if (!is.character(spec$.name)) {
abort("The `.name` column of `spec` must be a character vector.")
abort("The `.name` column of `spec` must be a character vector.", call = caller_env())
}
if (vec_duplicate_any(spec$.name)) {
abort("The `.name` column of `spec` must be unique.")
abort("The `.name` column of `spec` must be unique.", call = caller_env())
}

if (!is.character(spec$.value)) {
abort("The `.value` column of `spec` must be a character vector.")
abort("The `.value` column of `spec` must be a character vector.", call = caller_env())
}

# Ensure `.name` and `.value` come first, in that order
Expand Down
2 changes: 1 addition & 1 deletion R/replace_na.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,6 @@ check_replacement <- function(x, var) {
n <- vec_size(x)

if (n != 1) {
abort(glue("Replacement for `{var}` is length {n}, not length 1."))
abort(glue("Replacement for `{var}` is length {n}, not length 1."), call = caller_env())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth adding error_call = caller_env() as an argument to check_replacement(), even thought it is only used in replace_na().

I think adding the error_call argument should be the "go to" solution for most of these, because we never know how the helpers might be used in the future, and having 1 rule to follow means we don't have to think about it as much.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, agreed. I just missed updating these.

}
}
12 changes: 6 additions & 6 deletions R/separate.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,19 @@ separate.data.frame <- function(data, col, into, sep = "[^[:alnum:]]+",
reconstruct_tibble(data, out, if (remove) var else NULL)
}

str_separate <- function(x, into, sep, convert = FALSE, extra = "warn", fill = "warn") {
check_not_stringr_pattern(sep, "sep")
str_separate <- function(x, into, sep, convert = FALSE, extra = "warn", fill = "warn", call = caller_env()) {
hadley marked this conversation as resolved.
Show resolved Hide resolved
check_not_stringr_pattern(sep, "sep", call = call)

if (!is.character(into)) {
abort("`into` must be a character vector")
abort("`into` must be a character vector.", call = call)
}

if (is.numeric(sep)) {
out <- strsep(x, sep)
} else if (is_character(sep)) {
out <- str_split_fixed(x, sep, length(into), extra = extra, fill = fill)
} else {
abort("`sep` must be either numeric or character")
abort("`sep` must be either numeric or character.", call = call)
}

names(out) <- as_utf8_character(into)
Expand Down Expand Up @@ -186,9 +186,9 @@ list_indices <- function(x, max = 20) {
paste(x, collapse = ", ")
}

check_not_stringr_pattern <- function(x, arg) {
check_not_stringr_pattern <- function(x, arg, call = caller_env()) {
if (inherits_any(x, c("pattern", "stringr_pattern"))) {
abort(glue("`{arg}` can't use modifiers from stringr."))
abort(glue("`{arg}` can't use modifiers from stringr."), call = call)
}

invisible(x)
Expand Down
23 changes: 13 additions & 10 deletions R/unnest-helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ df_simplify <- function(x,
...,
ptype = NULL,
transform = NULL,
simplify = TRUE) {
simplify = TRUE,
call = caller_env()) {
hadley marked this conversation as resolved.
Show resolved Hide resolved
check_dots_empty()

ptype <- check_list_of_ptypes(ptype, names(x), "ptype")
transform <- check_list_of_functions(transform, names(x), "transform")
ptype <- check_list_of_ptypes(ptype, names(x), "ptype", call = call)
transform <- check_list_of_functions(transform, names(x), "transform", call = call)

if (is_bool(simplify)) {
simplify_default <- simplify
Expand All @@ -19,13 +20,13 @@ df_simplify <- function(x,
}

if (!vec_is_list(simplify)) {
abort("`simplify` must be a list or a single `TRUE` or `FALSE`.")
abort("`simplify` must be a list or a single `TRUE` or `FALSE`.", call = call)
}
if (length(simplify) > 0L && !is_named(simplify)) {
abort("All elements of `simplify` must be named.")
abort("All elements of `simplify` must be named.", call = call)
}
if (vec_duplicate_any(names(simplify))) {
abort("The names of `simplify` must be unique.")
abort("The names of `simplify` must be unique.", call = call)
}

x_n <- length(x)
Expand All @@ -47,7 +48,8 @@ df_simplify <- function(x,
x = col,
ptype = col_ptype,
transform = col_transform,
simplify = col_simplify
simplify = col_simplify,
call = call
)
}

Expand All @@ -58,7 +60,8 @@ col_simplify <- function(x,
...,
ptype = NULL,
transform = NULL,
simplify = TRUE) {
simplify = TRUE,
call = caller_env()) {
hadley marked this conversation as resolved.
Show resolved Hide resolved
check_dots_empty()

if (!is.null(transform)) {
Expand All @@ -70,7 +73,7 @@ col_simplify <- function(x,
x <- transform(x)
}
if (!is.null(ptype)) {
x <- vec_cast(x, ptype)
x <- vec_cast(x, ptype, call = call)
}
return(x)
}
Expand All @@ -82,7 +85,7 @@ col_simplify <- function(x,
}
if (!is.null(ptype)) {
x <- tidyr_new_list(x)
x <- vec_cast_common(!!!x, .to = ptype)
x <- vec_cast_common(!!!x, .to = ptype, .call = call)
x <- new_list_of(x, ptype = ptype)
}

Expand Down
Loading