Skip to content

Commit a4f9811

Browse files
authored
workshop protected arguments warning (#1216)
1 parent 58e4329 commit a4f9811

File tree

9 files changed

+89
-65
lines changed

9 files changed

+89
-65
lines changed

R/arguments.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ check_eng_args <- function(args, obj, core_args) {
2121
common_args <- intersect(protected_args, names(args))
2222
if (length(common_args) > 0) {
2323
args <- args[!(names(args) %in% common_args)]
24-
common_args <- paste0(common_args, collapse = ", ")
2524
cli::cli_warn(
26-
"The argument{?s} {.arg {common_args}} cannot be manually
27-
modified and {?was/were} removed."
25+
c(
26+
"The argument{?s} {.arg {common_args}} cannot be manually modified
27+
and {?was/were} removed."
28+
),
29+
class = "parsnip_protected_arg_warning"
2830
)
2931
}
3032
args

tests/testthat/_snaps/arguments.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# warns informatively about protected arguments
2+
3+
Code
4+
.res <- check_eng_args(args = list(a = 1, b = 2, c = 3, e = 5), obj = obj,
5+
core_args = core_args)
6+
Condition
7+
Warning:
8+
The arguments `a`, `b`, and `c` cannot be manually modified and were removed.
9+
10+
---
11+
12+
Code
13+
.res <- check_eng_args(args = list(b = 2, c = 3, e = 5), obj = obj, core_args = core_args)
14+
Condition
15+
Warning:
16+
The arguments `b` and `c` cannot be manually modified and were removed.
17+
18+
---
19+
20+
Code
21+
.res <- check_eng_args(args = list(c = 3, e = 5), obj = obj, core_args = core_args)
22+
Condition
23+
Warning:
24+
The argument `c` cannot be manually modified and was removed.
25+

tests/testthat/_snaps/mlp.md

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,6 @@
3232
x Engine "wat?" is not supported for `mlp()`
3333
i See `show_engines("mlp")`.
3434

35-
---
36-
37-
Code
38-
translate(mlp(mode = "regression") %>% set_engine("nnet", formula = y ~ x))
39-
Condition
40-
Warning:
41-
The argument `formula` cannot be manually modified and was removed.
42-
Output
43-
Single Layer Neural Network Model Specification (regression)
44-
45-
Main Arguments:
46-
hidden_units = 5
47-
48-
Computational engine: nnet
49-
50-
Model fit template:
51-
nnet::nnet(formula = missing_arg(), data = missing_arg(), size = 5,
52-
trace = FALSE, linout = TRUE)
53-
5435
# check_args() works
5536

5637
Code

tests/testthat/_snaps/multinom_reg.md

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,6 @@
4040
Error in `set_engine()`:
4141
! Missing engine. Possible mode/engine combinations are: classification {glmnet, spark, keras, nnet, brulee}.
4242

43-
---
44-
45-
Code
46-
translate(multinom_reg(penalty = 0.1) %>% set_engine("glmnet", x = hpc[, 1:3],
47-
y = hpc$class))
48-
Condition
49-
Warning:
50-
The argument `x, y` cannot be manually modified and was removed.
51-
Output
52-
Multinomial Regression Model Specification (classification)
53-
54-
Main Arguments:
55-
penalty = 0.1
56-
57-
Computational engine: glmnet
58-
59-
Model fit template:
60-
glmnet::glmnet(x = missing_arg(), y = missing_arg(), weights = missing_arg(),
61-
family = "multinomial")
62-
6343
# check_args() works
6444

6545
Code

tests/testthat/_snaps/nullmodel.md

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,6 @@
1515
x Engine "wat?" is not supported for `null_model()`
1616
i See `show_engines("null_model")`.
1717

18-
---
19-
20-
Code
21-
translate(null_model(mode = "regression") %>% set_engine("parsnip", x = hpc[, 1:
22-
3], y = hpc$class))
23-
Condition
24-
Warning:
25-
The argument `x, y` cannot be manually modified and was removed.
26-
Output
27-
Null Model Specification (regression)
28-
29-
Computational engine: parsnip
30-
31-
Model fit template:
32-
parsnip::nullmodel(x = missing_arg(), y = missing_arg())
33-
3418
# nullmodel execution
3519

3620
Code

tests/testthat/test-arguments.R

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
test_that("warns informatively about protected arguments", {
2+
obj <- list(protect = c("a", "b"))
3+
core_args <- c("c", "d")
4+
5+
expect_snapshot(
6+
.res <- check_eng_args(
7+
args = list(a = 1, b = 2, c = 3, e = 5),
8+
obj = obj,
9+
core_args = core_args
10+
)
11+
)
12+
13+
expect_named(.res, "e")
14+
15+
expect_snapshot(
16+
.res <- check_eng_args(
17+
args = list(b = 2, c = 3, e = 5),
18+
obj = obj,
19+
core_args = core_args
20+
)
21+
)
22+
23+
expect_named(.res, "e")
24+
25+
expect_snapshot(
26+
.res <- check_eng_args(
27+
args = list(c = 3, e = 5),
28+
obj = obj,
29+
core_args = core_args
30+
)
31+
)
32+
33+
expect_named(.res, "e")
34+
35+
expect_warning(
36+
check_eng_args(
37+
args = list(a = 1, b = 2, c = 3, e = 5),
38+
obj = obj,
39+
core_args = core_args
40+
),
41+
class = "parsnip_protected_arg_warning"
42+
)
43+
})

tests/testthat/test-mlp.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ test_that('updating', {
1111
test_that('bad input', {
1212
expect_snapshot(error = TRUE, mlp(mode = "time series"))
1313
expect_snapshot(error = TRUE, translate(mlp(mode = "classification") %>% set_engine("wat?")))
14-
expect_snapshot(translate(mlp(mode = "regression") %>% set_engine("nnet", formula = y ~ x)))
14+
expect_warning(
15+
translate(mlp(mode = "regression") %>% set_engine("nnet", formula = y ~ x)),
16+
class = "parsnip_protected_arg_warning"
17+
)
1518
})
1619

1720
test_that("nnet_softmax", {

tests/testthat/test-multinom_reg.R

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,21 @@ test_that('bad input', {
1313
expect_snapshot(error = TRUE, multinom_reg(mode = "regression"))
1414
expect_snapshot(error = TRUE, translate(multinom_reg(penalty = 0.1) %>% set_engine("wat?")))
1515
expect_snapshot(error = TRUE, multinom_reg(penalty = 0.1) %>% set_engine())
16-
expect_snapshot(translate(multinom_reg(penalty = 0.1) %>% set_engine("glmnet", x = hpc[,1:3], y = hpc$class)))
16+
expect_warning(
17+
translate(
18+
multinom_reg(penalty = 0.1) %>% set_engine("glmnet", x = hpc[,1:3], y = hpc$class)
19+
),
20+
class = "parsnip_protected_arg_warning"
21+
)
1722
})
1823

1924
test_that('check_args() works', {
2025
skip_if_not_installed("keras")
21-
26+
2227
expect_snapshot(
2328
error = TRUE,
2429
{
25-
spec <- multinom_reg(mixture = -1) %>%
30+
spec <- multinom_reg(mixture = -1) %>%
2631
set_engine("keras") %>%
2732
set_mode("classification")
2833
fit(spec, class ~ ., hpc)
@@ -31,7 +36,7 @@ test_that('check_args() works', {
3136
expect_snapshot(
3237
error = TRUE,
3338
{
34-
spec <- multinom_reg(penalty = -1) %>%
39+
spec <- multinom_reg(penalty = -1) %>%
3540
set_engine("keras") %>%
3641
set_mode("classification")
3742
fit(spec, class ~ ., hpc)

tests/testthat/test-nullmodel.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ hpc <- hpc_data[1:150, c(2:5, 8)] %>% as.data.frame()
33
test_that('bad input', {
44
expect_snapshot(error = TRUE, translate(null_model(mode = "regression") %>% set_engine()))
55
expect_snapshot(error = TRUE, translate(null_model() %>% set_engine("wat?")))
6-
expect_snapshot(
6+
expect_warning(
77
translate(
88
null_model(mode = "regression") %>% set_engine("parsnip", x = hpc[,1:3], y = hpc$class)
9-
)
9+
),
10+
class = "parsnip_protected_arg_warning"
1011
)
1112
})
1213

0 commit comments

Comments
 (0)