Skip to content

Commit

Permalink
Merge pull request #315 from tidymodels/use-cli
Browse files Browse the repository at this point in the history
Start using {cli}
  • Loading branch information
hfrick authored Dec 4, 2023
2 parents 3e42198 + 4ad189e commit 6519f7e
Show file tree
Hide file tree
Showing 14 changed files with 124 additions and 102 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# dials (development version)

* Improving error messages by switching to cli (#315).


# dials 1.2.0

## New parameters
Expand Down
4 changes: 1 addition & 3 deletions R/aaa_values.R
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,7 @@ value_set <- function(object, values) {
if (length(values) == 0) {
rlang::abort("`values` should at least one element.")
}
if (!inherits(object, "param")) {
rlang::abort("`object` should be a 'param' object")
}
check_param(object)

if (inherits(object, "quant_param")) {
object <-
Expand Down
45 changes: 21 additions & 24 deletions R/constructors.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,13 @@ new_quant_param <- function(type = c("double", "integer"),
}

if (is.null(range)) {
rlang::abort("`range` must be supplied if `values` is `NULL`.", call = call)
cli::cli_abort(
"{.arg range} must be supplied if {.arg values} is `NULL`.",
call = call
)
}
if (is.null(inclusive)) {
rlang::abort(
cli::cli_abort(
"`inclusive` must be supplied if `values` is `NULL`.",
call = call
)
Expand All @@ -129,16 +132,14 @@ new_quant_param <- function(type = c("double", "integer"),

check_inclusive(inclusive)

if (!is.null(trans)) {
if (!is.trans(trans)) {
rlang::abort(
c(
"`trans` must be a 'trans' class object (or `NULL`).",
i = "See `?scales::trans_new`."
),
call = call
)
}
if (!is.null(trans) && !is.trans(trans)) {
cli::cli_abort(
c(
x = "{.arg trans} must be a {.cls trans} class object (or `NULL`).",
i = "See {.fn scales::trans_new}."
),
call = call
)
}

check_label(label, call = call)
Expand All @@ -158,20 +159,16 @@ new_quant_param <- function(type = c("double", "integer"),

if (!is.null(values)) {
ok_vals <- value_validate(res, values, call = call)
if (all(ok_vals)) {
res$values <- values
} else {
msg <- paste0(
"Some values are not valid: ",
glue_collapse(
values[!ok_vals],
sep = ", ",
last = " and ",
width = min(options()$width - 30, 10)
)

if (!all(ok_vals)) {
offenders <- values[!ok_vals]
cli::cli_abort(
"Some values are not valid: {.val {offenders}}.",
call = call
)
rlang::abort(msg, call = call)
}

res$values <- values
}

res
Expand Down
9 changes: 3 additions & 6 deletions R/finalize.R
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,7 @@ finalize.default <- function(object, x, force = TRUE, ...) {
#' @export
#' @rdname finalize
get_p <- function(object, x, log_vals = FALSE, ...) {
if (!inherits(object, "param")) {
rlang::abort("`object` should be a 'param' object.")
}
check_param(object)

rngs <- range_get(object, original = FALSE)
if (!is_unknown(rngs$upper)) {
Expand Down Expand Up @@ -188,9 +186,8 @@ get_log_p <- function(object, x, ...) {
#' @export
#' @rdname finalize
get_n_frac <- function(object, x, log_vals = FALSE, frac = 1/3, ...) {
if (!inherits(object, "param")) {
rlang::abort("`object` should be a 'param' object.")
}
check_param(object)

rngs <- range_get(object, original = FALSE)
if (!is_unknown(rngs$upper)) {
return(object)
Expand Down
20 changes: 20 additions & 0 deletions R/misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,23 @@ check_inclusive <- function(x, ..., call = caller_env()) {
invisible(x)
}

check_param <- function(x,
...,
allow_na = FALSE,
allow_null = FALSE,
arg = caller_arg(x),
call = caller_env()) {
if (!missing(x) && inherits(x, "param")) {
return(invisible(NULL))
}

stop_input_type(
x,
c("a single parameter object"),
...,
allow_na = allow_na,
allow_null = allow_null,
arg = arg,
call = call
)
}
10 changes: 5 additions & 5 deletions R/parameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -281,12 +281,12 @@ update.parameters <- function(object, ...) {
not_null <- !purrr::map_lgl(args, ~ all(is.na(.x)))
bad_input <- not_param & not_null
if (any(bad_input)) {
msg <- paste0("'", nms[bad_input], "'", collapse = ", ")
msg <- paste(
"At least one parameter is not a dials parameter object",
"or NA:", msg
offenders <- nms[bad_input]

cli::cli_abort(
"At least one parameter is not a dials parameter object or NA: \\
{offenders}."
)
rlang::abort(msg)
}

for (p in nms) {
Expand Down
22 changes: 9 additions & 13 deletions R/validation.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,26 @@ validate_params <- function(..., call = caller_env()) {
param_quos <- quos(...)
param_expr <- purrr::map_chr(param_quos, rlang::quo_text)
if (length(param_quos) == 0) {
rlang::abort("At least one parameter object is required.", call = call)
cli::cli_abort("At least one parameter object is required.", call = call)
}
params <- map(param_quos, eval_tidy)
is_param <- map_lgl(params, function(x) inherits(x, "param"))
if (!all(is_param)) {
rlang::abort(
paste0(
"These arguments must have class 'param': ",
paste0("`", param_expr[!is_param], "`", collapse = ",")
),
offenders <- param_expr[!is_param]
cli::cli_abort(
"Th{?is/ese} argument{?s} must have class {.cls param}: \\
{.arg {offenders}}.",
call = call
)
}
bad_param <- has_unknowns(params)
if (any(bad_param)) {
bad_param <- names(bad_param)[bad_param]
rlang::abort(
cli::cli_abort(
c(
paste0(
"These arguments contain unknowns: ",
paste0("`", bad_param, "`", collapse = ","),
"."
),
i = "See the `finalize()` function."
x = "Th{?is/ese} argument{?s} contain{?s/} unknowns: \\
{.arg {bad_param}}.",
i = "See the {.fn dials::finalize} function."
),
call = call
)
Expand Down
73 changes: 41 additions & 32 deletions tests/testthat/_snaps/aaa_unknown.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,141 +4,150 @@
grid_regular(p1)
Condition
Error in `grid_regular()`:
! These arguments contain unknowns: `q`.
i See the `finalize()` function.
x This argument contains unknowns: `q`.
i See the `dials::finalize()` function.

---

Code
grid_regular(p2)
Condition
Error in `grid_regular()`:
! These arguments contain unknowns: `mtry`.
i See the `finalize()` function.
x This argument contains unknowns: `mtry`.
i See the `dials::finalize()` function.

---

Code
grid_regular(p3)
Condition
Error in `grid_regular()`:
x These arguments contain unknowns: `mtry` and `q`.
i See the `dials::finalize()` function.

---

Code
grid_random(p1)
Condition
Error in `grid_random()`:
! These arguments contain unknowns: `q`.
i See the `finalize()` function.
x This argument contains unknowns: `q`.
i See the `dials::finalize()` function.

---

Code
grid_random(p2)
Condition
Error in `grid_random()`:
! These arguments contain unknowns: `mtry`.
i See the `finalize()` function.
x This argument contains unknowns: `mtry`.
i See the `dials::finalize()` function.

---

Code
grid_latin_hypercube(p1)
Condition
Error in `grid_latin_hypercube()`:
! These arguments contain unknowns: `q`.
i See the `finalize()` function.
x This argument contains unknowns: `q`.
i See the `dials::finalize()` function.

---

Code
grid_latin_hypercube(p2)
Condition
Error in `grid_latin_hypercube()`:
! These arguments contain unknowns: `mtry`.
i See the `finalize()` function.
x This argument contains unknowns: `mtry`.
i See the `dials::finalize()` function.

---

Code
grid_max_entropy(p1)
Condition
Error in `grid_max_entropy()`:
! These arguments contain unknowns: `q`.
i See the `finalize()` function.
x This argument contains unknowns: `q`.
i See the `dials::finalize()` function.

---

Code
grid_max_entropy(p2)
Condition
Error in `grid_max_entropy()`:
! These arguments contain unknowns: `mtry`.
i See the `finalize()` function.
x This argument contains unknowns: `mtry`.
i See the `dials::finalize()` function.

---

Code
grid_regular(min_n(), q = mtry())
Condition
Error in `grid_regular()`:
! These arguments contain unknowns: `q`.
i See the `finalize()` function.
x This argument contains unknowns: `q`.
i See the `dials::finalize()` function.

---

Code
grid_regular(mtry())
Condition
Error in `grid_regular()`:
! These arguments contain unknowns: `mtry`.
i See the `finalize()` function.
x This argument contains unknowns: `mtry`.
i See the `dials::finalize()` function.

---

Code
grid_random(min_n(), q = mtry())
Condition
Error in `grid_random()`:
! These arguments contain unknowns: `q`.
i See the `finalize()` function.
x This argument contains unknowns: `q`.
i See the `dials::finalize()` function.

---

Code
grid_random(mtry())
Condition
Error in `grid_random()`:
! These arguments contain unknowns: `mtry`.
i See the `finalize()` function.
x This argument contains unknowns: `mtry`.
i See the `dials::finalize()` function.

---

Code
grid_regular(min_n(), q = mtry())
Condition
Error in `grid_regular()`:
! These arguments contain unknowns: `q`.
i See the `finalize()` function.
x This argument contains unknowns: `q`.
i See the `dials::finalize()` function.

---

Code
grid_latin_hypercube(mtry())
Condition
Error in `grid_latin_hypercube()`:
! These arguments contain unknowns: `mtry`.
i See the `finalize()` function.
x This argument contains unknowns: `mtry`.
i See the `dials::finalize()` function.

---

Code
grid_max_entropy(min_n(), q = mtry())
Condition
Error in `grid_max_entropy()`:
! These arguments contain unknowns: `q`.
i See the `finalize()` function.
x This argument contains unknowns: `q`.
i See the `dials::finalize()` function.

---

Code
grid_max_entropy(mtry())
Condition
Error in `grid_max_entropy()`:
! These arguments contain unknowns: `mtry`.
i See the `finalize()` function.
x This argument contains unknowns: `mtry`.
i See the `dials::finalize()` function.

Loading

0 comments on commit 6519f7e

Please sign in to comment.