Skip to content

Commit a9128e9

Browse files
authored
Merge pull request #336 from cmu-delphi/ndefries/drop-nargs-num-args-check
Drop the `n_mandatory_f_args` placeholder arg from `assert_sufficient_f_args`
2 parents 62da4d5 + c936387 commit a9128e9

File tree

4 files changed

+26
-32
lines changed

4 files changed

+26
-32
lines changed

R/grouped_epi_archive.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ grouped_epi_archive =
232232

233233
# Check that `f` takes enough args
234234
if (!missing(f) && is.function(f)) {
235-
assert_sufficient_f_args(f, ..., n_mandatory_f_args = 3L)
235+
assert_sufficient_f_args(f, ...)
236236
}
237237

238238
# Validate and pre-process `before`:

R/slide.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ epi_slide = function(x, f, ..., before, after, ref_time_values,
170170

171171
# Check that `f` takes enough args
172172
if (!missing(f) && is.function(f)) {
173-
assert_sufficient_f_args(f, ..., n_mandatory_f_args = 3L)
173+
assert_sufficient_f_args(f, ...)
174174
}
175175

176176
if (missing(ref_time_values)) {

R/utils.R

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,14 @@ Warn = function(msg, ...) rlang::warn(break_str(msg, init = "Warning: "), ...)
106106
#' `epi_archive` in `epi_slide` or `epix_slide`.
107107
#' @param ... Dots that will be forwarded to `f` from the dots of `epi_slide` or
108108
#' `epix_slide`.
109-
#' @param n_mandatory_f_args Integer; specifies the number of arguments `f`
110-
#' is required to take before any `...` arg. Defaults to 2.
111109
#'
112110
#' @importFrom rlang is_missing
113111
#' @importFrom purrr map_lgl
114112
#' @importFrom utils tail
115113
#'
116114
#' @noRd
117-
assert_sufficient_f_args <- function(f, ..., n_mandatory_f_args = 2L) {
118-
mandatory_f_args_labels <- c("window data", "group key", "reference time value")[seq(n_mandatory_f_args)]
115+
assert_sufficient_f_args <- function(f, ...) {
116+
mandatory_f_args_labels <- c("window data", "group key", "reference time value")
119117
n_mandatory_f_args <- length(mandatory_f_args_labels)
120118
args = formals(args(f))
121119
args_names = names(args)

tests/testthat/test-utils.R

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -110,25 +110,25 @@ test_that("enlist works",{
110110
})
111111

112112
test_that("assert_sufficient_f_args alerts if the provided f doesn't take enough args", {
113-
f_xg = function(x, g) dplyr::tibble(value=mean(x$binary), count=length(x$binary))
114-
f_xg_dots = function(x, g, ...) dplyr::tibble(value=mean(x$binary), count=length(x$binary))
113+
f_xgt = function(x, g, t) dplyr::tibble(value=mean(x$binary), count=length(x$binary))
114+
f_xgt_dots = function(x, g, t, ...) dplyr::tibble(value=mean(x$binary), count=length(x$binary))
115115

116116
# If `regexp` is NA, asserts that there should be no errors/messages.
117-
expect_error(assert_sufficient_f_args(f_xg), regexp = NA)
118-
expect_warning(assert_sufficient_f_args(f_xg), regexp = NA)
119-
expect_error(assert_sufficient_f_args(f_xg_dots), regexp = NA)
120-
expect_warning(assert_sufficient_f_args(f_xg_dots), regexp = NA)
117+
expect_error(assert_sufficient_f_args(f_xgt), regexp = NA)
118+
expect_warning(assert_sufficient_f_args(f_xgt), regexp = NA)
119+
expect_error(assert_sufficient_f_args(f_xgt_dots), regexp = NA)
120+
expect_warning(assert_sufficient_f_args(f_xgt_dots), regexp = NA)
121121

122122
f_x_dots = function(x, ...) dplyr::tibble(value=mean(x$binary), count=length(x$binary))
123123
f_dots = function(...) dplyr::tibble(value=c(5), count=c(2))
124124
f_x = function(x) dplyr::tibble(value=mean(x$binary), count=length(x$binary))
125125
f = function() dplyr::tibble(value=c(5), count=c(2))
126126

127127
expect_warning(assert_sufficient_f_args(f_x_dots),
128-
regexp = ", the group key will be included",
128+
regexp = ", the group key and reference time value will be included",
129129
class = "epiprocess__assert_sufficient_f_args__mandatory_f_args_passed_to_f_dots")
130130
expect_warning(assert_sufficient_f_args(f_dots),
131-
regexp = ", the window data and group key will be included",
131+
regexp = ", the window data, group key, and reference time value will be included",
132132
class = "epiprocess__assert_sufficient_f_args__mandatory_f_args_passed_to_f_dots")
133133
expect_error(assert_sufficient_f_args(f_x),
134134
class = "epiprocess__assert_sufficient_f_args__f_needs_min_args")
@@ -142,46 +142,42 @@ test_that("assert_sufficient_f_args alerts if the provided f doesn't take enough
142142
expect_error(assert_sufficient_f_args(f_xs, setting="b"),
143143
class = "epiprocess__assert_sufficient_f_args__f_needs_min_args_plus_forwarded")
144144

145-
expect_error(assert_sufficient_f_args(f_xg, "b"),
145+
expect_error(assert_sufficient_f_args(f_xgt, "b"),
146146
class = "epiprocess__assert_sufficient_f_args__f_needs_min_args_plus_forwarded")
147147
})
148148

149149
test_that("assert_sufficient_f_args alerts if the provided f has defaults for the required args", {
150-
f_xg = function(x, g=1) dplyr::tibble(value=mean(x$binary), count=length(x$binary))
151-
f_xg_dots = function(x=1, g, ...) dplyr::tibble(value=mean(x$binary), count=length(x$binary))
150+
f_xgt = function(x, g=1, t) dplyr::tibble(value=mean(x$binary), count=length(x$binary))
151+
f_xgt_dots = function(x=1, g, t, ...) dplyr::tibble(value=mean(x$binary), count=length(x$binary))
152152
f_x_dots = function(x=1, ...) dplyr::tibble(value=mean(x$binary), count=length(x$binary))
153153

154-
expect_error(assert_sufficient_f_args(f_xg),
154+
expect_error(assert_sufficient_f_args(f_xgt),
155155
regexp = "pass the group key to `f`'s g argument,",
156156
class = "epiprocess__assert_sufficient_f_args__required_args_contain_defaults")
157-
expect_error(assert_sufficient_f_args(f_xg_dots),
157+
expect_error(assert_sufficient_f_args(f_xgt_dots),
158158
regexp = "pass the window data to `f`'s x argument,",
159159
class = "epiprocess__assert_sufficient_f_args__required_args_contain_defaults")
160160
expect_error(suppressWarnings(assert_sufficient_f_args(f_x_dots)),
161161
class = "epiprocess__assert_sufficient_f_args__required_args_contain_defaults")
162162

163-
f_xsg = function(x, setting="a", g) dplyr::tibble(value=mean(x$binary), count=length(x$binary))
164-
f_xsg_dots = function(x, setting="a", g, ...) dplyr::tibble(value=mean(x$binary), count=length(x$binary))
163+
f_xsgt = function(x, setting="a", g, t) dplyr::tibble(value=mean(x$binary), count=length(x$binary))
164+
f_xsgt_dots = function(x, setting="a", g, t, ...) dplyr::tibble(value=mean(x$binary), count=length(x$binary))
165165
f_xs_dots = function(x=1, setting="a", ...) dplyr::tibble(value=mean(x$binary), count=length(x$binary))
166166

167167
# forwarding named dots should prevent some complaints:
168-
expect_no_error(assert_sufficient_f_args(f_xsg, setting = "b"))
169-
expect_no_error(assert_sufficient_f_args(f_xsg_dots, setting = "b"))
168+
expect_no_error(assert_sufficient_f_args(f_xsgt, setting = "b"))
169+
expect_no_error(assert_sufficient_f_args(f_xsgt_dots, setting = "b"))
170170
expect_error(suppressWarnings(assert_sufficient_f_args(f_xs_dots, setting = "b")),
171-
regexp = "window data to `f`'s x argument",
171+
regexp = "pass the window data to `f`'s x argument",
172172
class = "epiprocess__assert_sufficient_f_args__required_args_contain_defaults")
173173

174174
# forwarding unnamed dots should not:
175-
expect_error(assert_sufficient_f_args(f_xsg, "b"),
175+
expect_error(assert_sufficient_f_args(f_xsgt, "b"),
176176
class = "epiprocess__assert_sufficient_f_args__required_args_contain_defaults")
177-
expect_error(assert_sufficient_f_args(f_xsg_dots, "b"),
177+
expect_error(assert_sufficient_f_args(f_xsgt_dots, "b"),
178178
class = "epiprocess__assert_sufficient_f_args__required_args_contain_defaults")
179-
expect_error(assert_sufficient_f_args(f_xs_dots, "b"),
180-
class = "epiprocess__assert_sufficient_f_args__required_args_contain_defaults")
181-
182-
# forwarding no dots should produce a different error message in some cases:
183-
expect_error(assert_sufficient_f_args(f_xs_dots),
184-
regexp = "window data and group key to `f`'s x and setting argument",
179+
expect_error(suppressWarnings(assert_sufficient_f_args(f_xs_dots, "b")),
180+
regexp = "pass the window data and group key to `f`'s x and setting argument",
185181
class = "epiprocess__assert_sufficient_f_args__required_args_contain_defaults")
186182
})
187183

0 commit comments

Comments
 (0)