Skip to content

Commit 39bcd64

Browse files
authored
Merge pull request #446 from tidymodels/more-stop_input_type
Make use of `stop_input_type()`
2 parents 3dd3261 + c64c618 commit 39bcd64

8 files changed

Lines changed: 22 additions & 27 deletions

File tree

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

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

11+
* Improved several type-checking error messages to include the actual type of the input (#423).
12+
1113
* `encode_unit()` now provides a helpful error when `x` is not a parameter object (#430).
1214

1315
* `value_validate()`, `value_transform()`, `value_inverse()`, and `value_set()` now produce more informative error messages when values contain unknowns (#445).

R/encode_unit.R

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ encode_unit <- function(x, value, direction, ...) {
1919

2020
#' @export
2121
encode_unit.default <- function(x, value, direction, ...) {
22-
cli::cli_abort(
23-
"{.arg x} should be a dials parameter object,
24-
not {.obj_type_friendly x}."
25-
)
22+
stop_input_type(x, "a dials parameter object")
2623
}
2724

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

3734
if (!is.numeric(value) || is.matrix(value)) {
38-
cli::cli_abort("{.arg value} should be a numeric vector.")
35+
stop_input_type(value, "a numeric vector")
3936
}
4037

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

8582
if (!is.character(value) || is.matrix(value)) {
86-
cli::cli_abort("{.arg value} should be a character vector.")
83+
stop_input_type(value, "a character vector")
8784
}
8885

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

109106
if (!is.numeric(value) || is.matrix(value)) {
110-
cli::cli_abort("{.arg value} should be a numeric vector.")
107+
stop_input_type(value, "a numeric vector")
111108
}
112109

113110
ind <- cut(

R/grids.R

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,7 @@ make_random_grid <- function(
407407
# ------------------------------------------------------------------------------
408408

409409
new_param_grid <- function(x = new_data_frame()) {
410-
if (!is.data.frame(x)) {
411-
cli::cli_abort(
412-
"{.arg x} must be a data frame to construct a new grid from."
413-
)
414-
}
410+
check_data_frame(x)
415411

416412
x <- vctrs::vec_unique(x)
417413
size <- vctrs::vec_size(x)

R/misc.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ check_values_quant <- function(
146146
}
147147

148148
if (!is.numeric(x)) {
149-
cli::cli_abort("{.arg {arg}} must be numeric.", call = call)
149+
stop_input_type(x, "a numeric vector", arg = arg, call = call)
150150
}
151151

152152
if (anyNA(x)) {

tests/testthat/_snaps/constructors.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@
316316
new_quant_param(type = "integer", values = "not_numeric", label = c(foo = "Foo"))
317317
Condition
318318
Error:
319-
! `values` must be numeric.
319+
! `values` must be a numeric vector, not the string "not_numeric".
320320

321321
---
322322

tests/testthat/_snaps/encode_unit.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
encode_unit(prune_method(), 13, direction = "forward")
1313
Condition
1414
Error in `encode_unit()`:
15-
! `value` should be a character vector.
15+
! `value` must be a character vector, not the number 13.
1616

1717
# to [0, 1] for quantitative values
1818

1919
Code
2020
encode_unit(penalty(), "penalty", direction = "forward")
2121
Condition
2222
Error in `encode_unit()`:
23-
! `value` should be a numeric vector.
23+
! `value` must be a numeric vector, not the string "penalty".
2424

2525
# encode_unit validates original argument
2626

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

4141
---
4242

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

4949
---
5050

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

6666
---
6767

6868
Code
6969
encode_unit(z, 1, direction = "forward")
7070
Condition
7171
Error in `encode_unit()`:
72-
! `value` should be a character vector.
72+
! `value` must be a character vector, not the number 1.
7373

7474
---
7575

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

8282
---
8383

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

9090
---
9191

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

9898
---
9999

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

106106
---
107107

108108
Code
109109
encode_unit(x, prune_method()$values, direction = "backward")
110110
Condition
111111
Error in `encode_unit()`:
112-
! `value` should be a numeric vector.
112+
! `value` must be a numeric vector, not a character vector.
113113

114114
---
115115

tests/testthat/_snaps/grids.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,5 +397,5 @@
397397
new_param_grid(as.matrix(x))
398398
Condition
399399
Error in `new_param_grid()`:
400-
! `x` must be a data frame to construct a new grid from.
400+
! `x` must be a data frame, not an integer matrix.
401401

tests/testthat/_snaps/misc.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
check_values_quant("should have been a numeric")
2121
Condition
2222
Error:
23-
! `"should have been a numeric"` must be numeric.
23+
! `"should have been a numeric"` must be a numeric vector, not the string "should have been a numeric".
2424

2525
---
2626

0 commit comments

Comments
 (0)