Skip to content

Commit e564926

Browse files
authored
Merge pull request #438 from tidymodels/grid-default-method
add `.default()` method for better errors
2 parents edd71f7 + c58af37 commit e564926

10 files changed

Lines changed: 103 additions & 10 deletions

File tree

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ S3method(grid_latin_hypercube,parameters)
1717
S3method(grid_max_entropy,list)
1818
S3method(grid_max_entropy,param)
1919
S3method(grid_max_entropy,parameters)
20+
S3method(grid_random,default)
2021
S3method(grid_random,list)
2122
S3method(grid_random,param)
2223
S3method(grid_random,parameters)
24+
S3method(grid_regular,default)
2325
S3method(grid_regular,list)
2426
S3method(grid_regular,param)
2527
S3method(grid_regular,parameters)
28+
S3method(grid_space_filling,default)
2629
S3method(grid_space_filling,list)
2730
S3method(grid_space_filling,param)
2831
S3method(grid_space_filling,parameters)

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
* Parameters were added for the `tab_pfn` model: `num_estimators()`, `softmax_temperature()`, `balance_probabilities()`, `average_before_softmax()`, and `training_set_limit()`.
88

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

1111

1212
# dials 1.4.2

R/grids.R

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,24 @@ grid_regular <- function(x, ..., levels = 3, original = TRUE, filter = NULL) {
5959
UseMethod("grid_regular")
6060
}
6161

62+
#' @export
63+
#' @rdname grid_regular
64+
grid_regular.default <- function(
65+
x,
66+
...,
67+
levels = 3,
68+
original = TRUE,
69+
filter = NULL
70+
) {
71+
if (missing(x)) {
72+
cli::cli_abort("At least one parameter object is required.")
73+
}
74+
cli::cli_abort(
75+
"{.arg x} must be a {.cls param} object, list, or {.cls parameters} object,
76+
not {.obj_type_friendly {x}}."
77+
)
78+
}
79+
6280
#' @export
6381
#' @rdname grid_regular
6482
grid_regular.parameters <- function(
@@ -239,6 +257,24 @@ grid_random <- function(x, ..., size = 5, original = TRUE, filter = NULL) {
239257
UseMethod("grid_random")
240258
}
241259

260+
#' @export
261+
#' @rdname grid_regular
262+
grid_random.default <- function(
263+
x,
264+
...,
265+
size = 5,
266+
original = TRUE,
267+
filter = NULL
268+
) {
269+
if (missing(x)) {
270+
cli::cli_abort("At least one parameter object is required.")
271+
}
272+
cli::cli_abort(
273+
"{.arg x} must be a {.cls param} object, list, or {.cls parameters} object,
274+
not {.obj_type_friendly {x}}."
275+
)
276+
}
277+
242278
#' @export
243279
#' @rdname grid_regular
244280
grid_random.parameters <- function(

R/space_filling.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,24 @@ grid_space_filling <- function(
107107
UseMethod("grid_space_filling")
108108
}
109109

110+
#' @export
111+
#' @rdname grid_space_filling
112+
grid_space_filling.default <- function(
113+
x,
114+
...,
115+
size = 5,
116+
type = "any",
117+
original = TRUE
118+
) {
119+
if (missing(x)) {
120+
cli::cli_abort("At least one parameter object is required.")
121+
}
122+
cli::cli_abort(
123+
"{.arg x} must be a {.cls param} object, list, or {.cls parameters} object,
124+
not {.obj_type_friendly {x}}."
125+
)
126+
}
127+
110128
#' @export
111129
#' @rdname grid_space_filling
112130
grid_space_filling.parameters <- function(

man/grid_regular.Rd

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/grid_space_filling.Rd

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/grids.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,16 @@
9696
Code
9797
grid_random()
9898
Condition
99-
Error in `UseMethod()`:
100-
! no applicable method for 'grid_random' applied to an object of class "NULL"
99+
Error in `grid_random()`:
100+
! At least one parameter object is required.
101+
102+
---
103+
104+
Code
105+
grid_random("not a param")
106+
Condition
107+
Error in `grid_random()`:
108+
! `x` must be a <param> object, list, or <parameters> object, not a string.
101109

102110
---
103111

@@ -250,8 +258,16 @@
250258
Code
251259
grid_regular()
252260
Condition
253-
Error in `UseMethod()`:
254-
! no applicable method for 'grid_regular' applied to an object of class "NULL"
261+
Error in `grid_regular()`:
262+
! At least one parameter object is required.
263+
264+
---
265+
266+
Code
267+
grid_regular("not a param")
268+
Condition
269+
Error in `grid_regular()`:
270+
! `x` must be a <param> object, list, or <parameters> object, not a string.
255271

256272
---
257273

tests/testthat/_snaps/space_filling.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,16 @@
118118
Code
119119
grid_space_filling()
120120
Condition
121-
Error in `UseMethod()`:
122-
! no applicable method for 'grid_space_filling' applied to an object of class "NULL"
121+
Error in `grid_space_filling()`:
122+
! At least one parameter object is required.
123+
124+
---
125+
126+
Code
127+
grid_space_filling("not a param")
128+
Condition
129+
Error in `grid_space_filling()`:
130+
! `x` must be a <param> object, list, or <parameters> object, not a string.
123131

124132
---
125133

tests/testthat/test-grids.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,9 @@ test_that("grid_random validates inputs", {
109109
})
110110

111111
test_that("grid_random() errors with non-param inputs", {
112-
# no input at all
112+
# default method
113113
expect_snapshot(error = TRUE, grid_random())
114+
expect_snapshot(error = TRUE, grid_random("not a param"))
114115

115116
# param method
116117
expect_snapshot(error = TRUE, grid_random(penalty(), "min_n"))
@@ -157,8 +158,9 @@ test_that("grid_regular validates inputs", {
157158
})
158159

159160
test_that("grid_regular() errors with non-param inputs", {
160-
# no input at all
161+
# default method
161162
expect_snapshot(error = TRUE, grid_regular())
163+
expect_snapshot(error = TRUE, grid_regular("not a param"))
162164

163165
# param method
164166
expect_snapshot(error = TRUE, grid_regular(penalty(), "min_n"))

tests/testthat/test-space_filling.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,9 @@ test_that("grid_space_filling validates inputs", {
343343
})
344344

345345
test_that("grid_space_filling() errors with non-param inputs", {
346-
# no input at all
346+
# default method
347347
expect_snapshot(error = TRUE, grid_space_filling())
348+
expect_snapshot(error = TRUE, grid_space_filling("not a param"))
348349

349350
# param method
350351
expect_snapshot(error = TRUE, grid_space_filling(penalty(), "min_n"))

0 commit comments

Comments
 (0)