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

Improve arg_match() message based on context #1733

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# rlang (development version)

* `arg_match()` now throws better error messages if `multiple = TRUE` or if a single value is allowed (@olivroy, #1635, #1682).

* `env_browse()` and `env_is_browsed()` are now defunct as they require an API
that is no longer available to packages (#1727).

Expand Down
25 changes: 20 additions & 5 deletions R/arg.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ arg_match <- function(arg,
abort(msg, call = error_call, arg = error_arg)
}
if (length(arg) > 1 && !setequal(arg, values)) {
msg <- arg_match_invalid_msg(arg, values, error_arg)
msg <- arg_match_invalid_msg(arg, values, multiple, error_arg)
abort(msg, call = error_call, arg = error_arg)
}

Expand All @@ -74,6 +74,8 @@ arg_match <- function(arg,
}

arg_match_multi <- function(arg, values, error_arg, error_call) {
# Set an option temporarily to make sure the multiple message shows.
local_options("rlang_multiple_error" = TRUE)
map_chr(arg, ~ arg_match0(.x, values, error_arg, error_call = error_call))
}

Expand Down Expand Up @@ -128,7 +130,8 @@ stop_arg_match <- function(arg, values, error_arg, error_call) {
check_string(arg, arg = error_arg, call = error_call)
}

msg <- arg_match_invalid_msg(arg, values, error_arg)
multiple <- getOption("rlang_multiple_error", FALSE)
msg <- arg_match_invalid_msg(arg, values, multiple, error_arg)

# Try suggest the most probable and helpful candidate value
candidate <- NULL
Expand Down Expand Up @@ -160,14 +163,26 @@ stop_arg_match <- function(arg, values, error_arg, error_call) {
abort(msg, call = error_call, arg = error_arg)
}

arg_match_invalid_msg <- function(val, values, error_arg) {
msg <- paste0(format_arg(error_arg), " must be one of ")
arg_match_invalid_msg <- function(val, values, multiple, error_arg) {
if (length(values) == 1) {
#1635
word <- " must be "
} else if (multiple) {
#1682
word <- " must be in "
} else {
word <- " must be one of "
}
msg <- paste0(format_arg(error_arg), word)
msg <- paste0(msg, oxford_comma(chr_quoted(values, "\"")))

if (is_null(val)) {
msg <- paste0(msg, ".")
} else {
msg <- paste0(msg, sprintf(', not "%s\".', val[[1]]))
# only show incorrect values.
incorrect_val <- setdiff(val, values)
incorrect_val <- oxford_comma(incorrect_val)
msg <- paste0(msg, sprintf(', not "%s\".', incorrect_val))
}

msg
Expand Down
17 changes: 15 additions & 2 deletions tests/testthat/_snaps/arg.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@

`arg` must be length 1 or a permutation of `c("foo", "bar")`.

# arg_match() works well when multiple = TRUE (#1682)

Code
f("x", values = c("y", "z"))
Condition
Error in `f()`:
! `x` must be in "y" or "z", not "x".
Code
f(c("x", "y"), values = c("y", "z"))
Condition
Error in `f()`:
! `x` must be in "y" or "z", not "x".

# `arg_match()` has informative error messages

Code
Expand Down Expand Up @@ -138,14 +151,14 @@
Output
<error/rlang_error>
Error in `my_wrapper()`:
! `my_arg` must be one of "foo", "bar", or "baz", not "ba".
! `my_arg` must be in "foo", "bar", or "baz", not "ba".
i Did you mean "bar"?
Code
(expect_error(my_wrapper(c("foo", "ba"))))
Output
<error/rlang_error>
Error in `my_wrapper()`:
! `my_arg` must be one of "foo", "bar", or "baz", not "ba".
! `my_arg` must be in "foo", "bar", or "baz", not "ba".
i Did you mean "bar"?

# arg_match0() defuses argument
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/_snaps/cnd-abort.md
Original file line number Diff line number Diff line change
Expand Up @@ -1397,21 +1397,21 @@
Output
<error/rlang_error>
Error:
! `"f"` must be one of "foo", not "f".
! `"f"` must be "foo", not "f".
i Did you mean "foo"?
Code
(expect_error(eval_bare(quote(arg_match0("f", "foo")))))
Output
<error/rlang_error>
Error:
! `"f"` must be one of "foo", not "f".
! `"f"` must be "foo", not "f".
i Did you mean "foo"?
Code
(expect_error(eval_bare(quote(arg_match0("f", "foo")), env())))
Output
<error/rlang_error>
Error:
! `"f"` must be one of "foo", not "f".
! `"f"` must be "foo", not "f".
i Did you mean "foo"?

# error_call() and format_error_call() preserve special syntax ops
Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/test-arg.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ test_that("informative error message on partial match", {
)
})

test_that("arg_match() works well when multiple = TRUE (#1682)", {
f <- function(x, ...) {
arg_match(x, ..., multiple = TRUE)
}
expect_snapshot(error = TRUE, {
f("x", values = c("y", "z"))
f(c("x", "y"), values = c("y", "z"))

})
})

test_that("`arg_match()` has informative error messages", {
arg_match_wrapper <- function(...) {
arg_match0_wrapper(...)
Expand Down
Loading