Skip to content
Merged
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
Expand Up @@ -8,6 +8,8 @@

* `parameters()` and the `grid_*()` functions give more information in the error message when non-parameter objects are passed in (#437, #438).

* Improved several type-checking error messages to include the actual type of the input (#423).

* `encode_unit()` now provides a helpful error when `x` is not a parameter object (#430).

* `value_validate()`, `value_transform()`, `value_inverse()`, and `value_set()` now produce more informative error messages when values contain unknowns (#445).
Expand Down
11 changes: 4 additions & 7 deletions R/encode_unit.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ encode_unit <- function(x, value, direction, ...) {

#' @export
encode_unit.default <- function(x, value, direction, ...) {
cli::cli_abort(
"{.arg x} should be a dials parameter object,
not {.obj_type_friendly x}."
)
stop_input_type(x, "a dials parameter object")
}

#' @rdname encode_unit
Expand All @@ -35,7 +32,7 @@ encode_unit.quant_param <- function(x, value, direction, original = TRUE, ...) {
}

if (!is.numeric(value) || is.matrix(value)) {
cli::cli_abort("{.arg value} should be a numeric vector.")
stop_input_type(value, "a numeric vector")
}

param_rng <- x$range$upper - x$range$lower
Expand Down Expand Up @@ -83,7 +80,7 @@ encode_unit.qual_param <- function(x, value, direction, ...) {
# convert to [0, 1]

if (!is.character(value) || is.matrix(value)) {
cli::cli_abort("{.arg value} should be a character vector.")
stop_input_type(value, "a character vector")
}

compl <- value[!is.na(value)]
Expand All @@ -107,7 +104,7 @@ encode_unit.qual_param <- function(x, value, direction, ...) {
}

if (!is.numeric(value) || is.matrix(value)) {
cli::cli_abort("{.arg value} should be a numeric vector.")
stop_input_type(value, "a numeric vector")
}

ind <- cut(
Expand Down
6 changes: 1 addition & 5 deletions R/grids.R
Original file line number Diff line number Diff line change
Expand Up @@ -407,11 +407,7 @@ make_random_grid <- function(
# ------------------------------------------------------------------------------

new_param_grid <- function(x = new_data_frame()) {
if (!is.data.frame(x)) {
cli::cli_abort(
"{.arg x} must be a data frame to construct a new grid from."
)
}
check_data_frame(x)

x <- vctrs::vec_unique(x)
size <- vctrs::vec_size(x)
Expand Down
2 changes: 1 addition & 1 deletion R/misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ check_values_quant <- function(
}

if (!is.numeric(x)) {
cli::cli_abort("{.arg {arg}} must be numeric.", call = call)
stop_input_type(x, "a numeric vector", arg = arg, call = call)
}

if (anyNA(x)) {
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/constructors.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@
new_quant_param(type = "integer", values = "not_numeric", label = c(foo = "Foo"))
Condition
Error:
! `values` must be numeric.
! `values` must be a numeric vector, not the string "not_numeric".

---

Expand Down
22 changes: 11 additions & 11 deletions tests/testthat/_snaps/encode_unit.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
encode_unit(prune_method(), 13, direction = "forward")
Condition
Error in `encode_unit()`:
! `value` should be a character vector.
! `value` must be a character vector, not the number 13.

# to [0, 1] for quantitative values

Code
encode_unit(penalty(), "penalty", direction = "forward")
Condition
Error in `encode_unit()`:
! `value` should be a numeric vector.
! `value` must be a numeric vector, not the string "penalty".

# encode_unit validates original argument

Expand All @@ -36,15 +36,15 @@
encode_unit("foo")
Condition
Error in `encode_unit()`:
! `x` should be a dials parameter object, not a string.
! `x` must be a dials parameter object, not the string "foo".

---

Code
encode_unit(2, prune_method()$values, direction = "forward")
Condition
Error in `encode_unit()`:
! `x` should be a dials parameter object, not a string.
! `x` must be a dials parameter object, not the number 2.

---

Expand All @@ -61,55 +61,55 @@
encode_unit(x, prune_method()$values, direction = "forward")
Condition
Error in `encode_unit()`:
! `value` should be a numeric vector.
! `value` must be a numeric vector, not a character vector.

---

Code
encode_unit(z, 1, direction = "forward")
Condition
Error in `encode_unit()`:
! `value` should be a character vector.
! `value` must be a character vector, not the number 1.

---

Code
encode_unit(x, matrix(letters[1:4], ncol = 2), direction = "forward")
Condition
Error in `encode_unit()`:
! `value` should be a numeric vector.
! `value` must be a numeric vector, not a character matrix.

---

Code
encode_unit(x, matrix(1:4, ncol = 2), direction = "forward")
Condition
Error in `encode_unit()`:
! `value` should be a numeric vector.
! `value` must be a numeric vector, not an integer matrix.

---

Code
encode_unit(z, matrix(1:4, ncol = 2), direction = "forward")
Condition
Error in `encode_unit()`:
! `value` should be a character vector.
! `value` must be a character vector, not an integer matrix.

---

Code
encode_unit(z, matrix(letters[1:4], ncol = 2), direction = "forward")
Condition
Error in `encode_unit()`:
! `value` should be a character vector.
! `value` must be a character vector, not a character matrix.

---

Code
encode_unit(x, prune_method()$values, direction = "backward")
Condition
Error in `encode_unit()`:
! `value` should be a numeric vector.
! `value` must be a numeric vector, not a character vector.

---

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/grids.md
Original file line number Diff line number Diff line change
Expand Up @@ -397,5 +397,5 @@
new_param_grid(as.matrix(x))
Condition
Error in `new_param_grid()`:
! `x` must be a data frame to construct a new grid from.
! `x` must be a data frame, not an integer matrix.

2 changes: 1 addition & 1 deletion tests/testthat/_snaps/misc.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
check_values_quant("should have been a numeric")
Condition
Error:
! `"should have been a numeric"` must be numeric.
! `"should have been a numeric"` must be a numeric vector, not the string "should have been a numeric".

---

Expand Down
Loading