Skip to content

Commit

Permalink
type check formula input to fit.model_spec() (tidymodels#952)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonpcouch authored Apr 25, 2023
1 parent 51b0cd7 commit 3ad7481
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
16 changes: 16 additions & 0 deletions R/fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,22 @@ fit.model_spec <-
control <- condense_control(control, control_parsnip())
check_case_weights(case_weights, object)

if (!inherits(formula, "formula")) {
msg <- "The {.arg formula} argument must be a formula, but it is a \\
{.cls {class(formula)[1]}}."

if (inherits(formula, "recipe")) {
msg <-
c(
msg,
"i" = "To fit a model with a recipe preprocessor, please use a \\
{.help [workflow](workflows::workflow)}."
)
}

cli::cli_abort(msg)
}

dots <- quos(...)

if (length(possible_engines(object)) == 0) {
Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/_snaps/fit_interfaces.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
# misspecified formula argument

Code
fit(linear_reg(), rec, mtcars)
Condition
Error in `fit()`:
! The `formula` argument must be a formula, but it is a <recipe>.
i To fit a model with a recipe preprocessor, please use a workflow (`?workflows::workflow()`).

---

Code
fit(linear_reg(), "boop", mtcars)
Condition
Error in `fit()`:
! The `formula` argument must be a formula, but it is a <character>.

# No loaded engines

! parsnip could not locate an implementation for `cubist_rules` model specifications.
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test_fit_interfaces.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ test_that('unknown modes', {
)
})

test_that("misspecified formula argument", {
rec <- structure(list(), class = "recipe")
expect_snapshot(error = TRUE,
fit(linear_reg(), rec, mtcars)
)
expect_snapshot(error = TRUE,
fit(linear_reg(), "boop", mtcars)
)
})

test_that("elapsed time parsnip mods", {
lm1 <-
linear_reg() %>%
Expand Down

0 comments on commit 3ad7481

Please sign in to comment.