Skip to content

Commit 4bb12a7

Browse files
authored
Retain "ordering" attribute of chunk_size and scheduling (#309)
* Explicitly retain `"ordering"` attribute of `chunk_size` * Explicitly retain `"ordering"` attribute of `scheduling`
1 parent 377b7cf commit 4bb12a7

3 files changed

Lines changed: 83 additions & 17 deletions

File tree

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
* When a furrr function errors, purrr's error index is no longer confusingly
2020
reported (#250).
2121

22+
* `furrr_options()` no longer drops the `"ordering"` attribute when casting
23+
non-integer `chunk_size` or `scheduling` to integer (#289, #290).
24+
2225
* furrr now looks up the purrr mapping function on the worker itself, rather
2326
than sending over its own copy of the function. This avoids possible issues
2427
when you have, say, purrr 1.0.0 locally but purrr 0.3.5 on the worker, where

R/furrr-options.R

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -366,27 +366,28 @@ validate_seed_list <- function(x) {
366366
}
367367

368368
validate_scheduling <- function(x) {
369-
if (length(x) != 1L) {
370-
abort("`scheduling` must be length 1.")
369+
vctrs::obj_check_vector(x, arg = "scheduling")
370+
vctrs::vec_check_size(x, size = 1L, arg = "scheduling")
371+
372+
if (is.na(x)) {
373+
abort("`scheduling` can't be `NA`.")
371374
}
372375

373-
if (identical(x, Inf)) {
376+
if (is_bool(x)) {
374377
return(x)
375378
}
376379

377-
if (is.logical(x)) {
378-
if (!is_bool(x)) {
379-
abort("A logical `scheduling` value can't be `NA`.")
380-
}
380+
if (x < 0L) {
381+
abort("`scheduling` must be greater than or equal to zero.")
382+
}
381383

384+
if (is.infinite(x)) {
382385
return(x)
383386
}
384387

388+
ordering <- attr(x, "ordering")
385389
x <- vctrs::vec_cast(x, integer(), x_arg = "scheduling")
386-
387-
if (x < 0L) {
388-
abort("`scheduling` must be greater than or equal to zero.")
389-
}
390+
attr(x, "ordering") <- ordering
390391

391392
x
392393
}
@@ -396,12 +397,8 @@ validate_chunk_size <- function(x) {
396397
return(x)
397398
}
398399

399-
if (identical(x, Inf)) {
400-
return(x)
401-
}
402-
403-
vctrs::vec_assert(x, size = 1L, arg = "chunk_size")
404-
x <- vctrs::vec_cast(x, integer(), x_arg = "chunk_size")
400+
vctrs::obj_check_vector(x, arg = "chunk_size")
401+
vctrs::vec_check_size(x, size = 1L, arg = "chunk_size")
405402

406403
if (is.na(x)) {
407404
abort("`chunk_size` can't be `NA`.")
@@ -411,6 +408,14 @@ validate_chunk_size <- function(x) {
411408
abort("`chunk_size` must be greater than zero.")
412409
}
413410

411+
if (is.infinite(x)) {
412+
return(x)
413+
}
414+
415+
ordering <- attr(x, "ordering")
416+
x <- vctrs::vec_cast(x, integer(), x_arg = "chunk_size")
417+
attr(x, "ordering") <- ordering
418+
414419
x
415420
}
416421

tests/testthat/test-furrr-options.R

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,49 @@ test_that("validates `seed`", {
262262
# ------------------------------------------------------------------------------
263263
# furrr_options(scheduling =)
264264

265+
test_that("can specify `scheduling`", {
266+
x <- furrr_options(scheduling = TRUE)
267+
expect_identical(x$scheduling, TRUE)
268+
269+
x <- furrr_options(scheduling = FALSE)
270+
expect_identical(x$scheduling, FALSE)
271+
272+
x <- furrr_options(scheduling = 0)
273+
expect_identical(x$scheduling, 0L)
274+
275+
x <- furrr_options(scheduling = 5)
276+
expect_identical(x$scheduling, 5L)
277+
278+
x <- furrr_options(scheduling = Inf)
279+
expect_identical(x$scheduling, Inf)
280+
})
281+
265282
test_that("validates `scheduling`", {
266283
expect_error(furrr_options(scheduling = c(1, 2)))
284+
expect_error(furrr_options(scheduling = c(TRUE, FALSE)))
267285
expect_error(furrr_options(scheduling = "x"))
268286
expect_error(furrr_options(scheduling = 1.5))
269287
expect_error(furrr_options(scheduling = NA))
288+
expect_error(furrr_options(scheduling = -Inf))
289+
expect_error(furrr_options(scheduling = lm(1 ~ 1)))
290+
})
291+
292+
test_that("`scheduling` supports an `ordering` attribute (#289)", {
293+
# Integer `scheduling`
294+
x <- furrr_options(scheduling = structure(2L, ordering = "random"))
295+
expect_identical(x$scheduling, structure(2L, ordering = "random"))
296+
297+
# Double `scheduling`
298+
x <- furrr_options(scheduling = structure(2, ordering = "random"))
299+
expect_identical(x$scheduling, structure(2L, ordering = "random"))
300+
301+
# Inf `scheduling`
302+
x <- furrr_options(scheduling = structure(Inf, ordering = "random"))
303+
expect_identical(x$scheduling, structure(Inf, ordering = "random"))
304+
305+
# Logical `scheduling`
306+
x <- furrr_options(scheduling = structure(TRUE, ordering = "random"))
307+
expect_identical(x$scheduling, structure(TRUE, ordering = "random"))
270308
})
271309

272310
# ------------------------------------------------------------------------------
@@ -275,13 +313,33 @@ test_that("validates `scheduling`", {
275313
test_that("can specify `chunk_size`", {
276314
x <- furrr_options(chunk_size = 2)
277315
expect_identical(x$chunk_size, 2L)
316+
317+
x <- furrr_options(chunk_size = Inf)
318+
expect_identical(x$chunk_size, Inf)
278319
})
279320

280321
test_that("validates `chunk_size`", {
322+
expect_error(furrr_options(chunk_size = 0))
281323
expect_error(furrr_options(chunk_size = c(1, 2)))
282324
expect_error(furrr_options(chunk_size = "x"))
283325
expect_error(furrr_options(chunk_size = 1.5))
284326
expect_error(furrr_options(chunk_size = NA))
327+
expect_error(furrr_options(chunk_size = -Inf))
328+
expect_error(furrr_options(chunk_size = lm(1 ~ 1)))
329+
})
330+
331+
test_that("`chunk_size` supports an `ordering` attribute (#290)", {
332+
# Integer `chunk_size`
333+
x <- furrr_options(chunk_size = structure(2L, ordering = "random"))
334+
expect_identical(x$chunk_size, structure(2L, ordering = "random"))
335+
336+
# Double `chunk_size`
337+
x <- furrr_options(chunk_size = structure(2, ordering = "random"))
338+
expect_identical(x$chunk_size, structure(2L, ordering = "random"))
339+
340+
# Inf `chunk_size`
341+
x <- furrr_options(chunk_size = structure(Inf, ordering = "random"))
342+
expect_identical(x$chunk_size, structure(Inf, ordering = "random"))
285343
})
286344

287345
# ------------------------------------------------------------------------------

0 commit comments

Comments
 (0)