Skip to content

Fail on NULL engine #1248

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

Merged
merged 7 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -47,6 +47,8 @@

* The quantile regression prediction type was disabled for the deprecated `surv_reg()` model.

* `NULL` is no longer accepted as an engine (#1242).


# parsnip 1.2.1

Expand Down
2 changes: 1 addition & 1 deletion R/engines.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ set_engine <- function(object, engine, ...) {
set_engine.model_spec <- function(object, engine, ...) {
mod_type <- class(object)[1]

if (rlang::is_missing(engine)) {
if (rlang::is_missing(engine) | is.null(engine)) {
stop_missing_engine(mod_type, call = caller_env(0))
}
object$engine <- engine
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/_snaps/args_and_modes.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,16 @@
Code
linear_reg() %>% set_engine()
Condition
Error in `set_engine()`:
! Missing engine. Possible mode/engine combinations are: quantile regression {quantreg} and regression {lm, glm, glmnet, stan, spark, keras, brulee}.
Error in `set_engine.model_spec()`:
! argument "engine" is missing, with no default

---

Code
proportional_hazards() %>% set_engine()
Condition
Error in `set_engine()`:
! No known engines for `proportional_hazards()`.
Error in `set_engine.model_spec()`:
! argument "engine" is missing, with no default

# set_* functions error when input isn't model_spec

Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/engines.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# NULL engines

Code
set_engine(nearest_neighbor(), NULL)
Condition
Error in `set_engine()`:
! Missing engine. Possible mode/engine combinations are: classification {kknn} and regression {kknn}.

4 changes: 2 additions & 2 deletions tests/testthat/_snaps/mars.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
Code
translate(mars(mode = "regression") %>% set_engine())
Condition
Error in `set_engine()`:
! Missing engine. Possible mode/engine combinations are: classification {earth} and regression {earth}.
Error in `set_engine.model_spec()`:
! argument "engine" is missing, with no default

---

Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/_snaps/multinom_reg.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
Code
multinom_reg(penalty = 0.1) %>% set_engine()
Condition
Error in `set_engine()`:
! Missing engine. Possible mode/engine combinations are: classification {glmnet, spark, keras, nnet, brulee}.
Error in `set_engine.model_spec()`:
! argument "engine" is missing, with no default

# check_args() works

Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/_snaps/nullmodel.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
Code
translate(null_model(mode = "regression") %>% set_engine())
Condition
Error in `set_engine()`:
! Missing engine. Possible mode/engine combinations are: classification {parsnip} and regression {parsnip}.
Error in `set_engine.model_spec()`:
! argument "engine" is missing, with no default

---

Expand Down
5 changes: 3 additions & 2 deletions tests/testthat/_snaps/rand_forest.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@

Code
res <- translate(rand_forest(mode = "classification") %>% set_engine(NULL))
Message
Used `engine = 'ranger'` for translation.
Condition
Error in `set_engine()`:
! Missing engine. Possible mode/engine combinations are: classification {ranger, randomForest, spark} and regression {ranger, randomForest, spark}.

---

Expand Down
5 changes: 3 additions & 2 deletions tests/testthat/_snaps/surv_reg.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@

Code
res <- translate(surv_reg() %>% set_engine(NULL))
Message
Used `engine = 'survival'` for translation.
Condition
Error in `set_engine()`:
! Missing engine. Possible mode/engine combinations are: regression {flexsurv, survival}.

# deprecation warning

Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/_snaps/svm_linear.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
Code
translate(svm_linear(mode = "regression") %>% set_engine(NULL))
Condition
Error in `translate.default()`:
! Please set an engine.
Error in `set_engine()`:
! Missing engine. Possible mode/engine combinations are: classification {LiblineaR, kernlab} and regression {LiblineaR, kernlab}.

---

Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/_snaps/svm_poly.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
Code
svm_poly() %>% set_engine(NULL) %>% translate()
Condition
Error in `translate.default()`:
! Please set an engine.
Error in `set_engine()`:
! Missing engine. Possible mode/engine combinations are: classification {kernlab} and regression {kernlab}.

4 changes: 2 additions & 2 deletions tests/testthat/_snaps/svm_rbf.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@
Code
translate(svm_rbf(mode = "regression") %>% set_engine(NULL))
Condition
Error in `translate.default()`:
! Please set an engine.
Error in `set_engine()`:
! Missing engine. Possible mode/engine combinations are: classification {kernlab, liquidSVM} and regression {kernlab, liquidSVM}.

8 changes: 8 additions & 0 deletions tests/testthat/test-engines.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
test_that("NULL engines", {
# See issue https://github.com/tidymodels/parsnip/issues/1242

expect_snapshot(
set_engine(nearest_neighbor(), NULL),
error = TRUE
)
})
5 changes: 4 additions & 1 deletion tests/testthat/test-rand_forest.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ test_that('updating', {
})

test_that('bad input', {
expect_snapshot(res <- translate(rand_forest(mode = "classification") %>% set_engine(NULL)))
expect_snapshot(res <-
translate(rand_forest(mode = "classification") %>%
set_engine(NULL)),
error = TRUE)
expect_snapshot(error = TRUE, rand_forest(mode = "time series"))
expect_snapshot(error = TRUE, translate(rand_forest(mode = "classification") %>% set_engine("wat?")))
})
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-surv_reg.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test_that('bad input', {

expect_snapshot(error = TRUE, surv_reg(mode = ", classification"))
expect_snapshot(error = TRUE, translate(surv_reg() %>% set_engine("wat")))
expect_snapshot(res <- translate(surv_reg() %>% set_engine(NULL)))
expect_snapshot(res <- translate(surv_reg() %>% set_engine(NULL)), error = TRUE)
})

test_that("deprecation warning", {
Expand Down
Loading