Skip to content

Commit

Permalink
fix: error if slice_sample() receives unknown or unsupported args
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennebacher committed Dec 19, 2024
1 parent c2912d4 commit 7a2637a
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 2 deletions.
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

* Added support for `stringr::str_replace_na()` (#153).

## Bug fixes

* `slice_sample()` now errors when unknown or unsupported arguments are
passed (thanks @fkohrt for the report).

# tidypolars 0.12.0

`tidypolars` requires `polars` >= 0.21.0.
Expand Down
5 changes: 4 additions & 1 deletion R/slice.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ slice_head.RPolarsLazyFrame <- slice_head.RPolarsDataFrame
#' @rdname slice_tail.RPolarsDataFrame
#' @export

slice_sample.RPolarsDataFrame <- function(.data, ..., n = NULL, prop = NULL, replace = FALSE, by = NULL) {
slice_sample.RPolarsDataFrame <- function(.data, ..., n = NULL, prop = NULL, by = NULL, replace = FALSE) {
check_unsupported_arg("weight_by", ...)
check_dots_empty0(...)

grps <- get_grps(.data, rlang::enquo(by), env = rlang::current_env())
mo <- attributes(.data)$maintain_grp_order
is_grouped <- !is.null(grps)
Expand Down
12 changes: 11 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ get_dots <- function(...) {
eval(substitute(alist(...)))
}

safe_deparse <- function (x, ...) {
safe_deparse <- function(x, ...) {
if (is.null(x)) {
return(NULL)
}
Expand Down Expand Up @@ -110,3 +110,13 @@ select_by_name_or_position <- function(expr, name, position, default, env) {
default
}
}

check_unsupported_arg <- function(arg_name, ...) {
dots <- enquos(...)
if (arg_name %in% names(dots)) {
abort(
paste("Argument", arg_name, "is not supported by tidypolars yet."),
call = caller_env()
)
}
}
18 changes: 18 additions & 0 deletions tests/testthat/_snaps/slice.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,21 @@
Error in `slice_sample()`:
! Cannot take more rows than the total number of rows when `replace = FALSE`.

# slice_sample errors on unknown args

Code
slice_sample(pl_mtcars, weight_by = cyl > 5, n = 5)
Condition
Error in `slice_sample()`:
! Argument weight_by is not supported by tidypolars yet.

---

Code
slice_sample(pl_mtcars, foo = 1, n = 5)
Condition
Error in `slice_sample()`:
! `...` must be empty.
x Problematic argument:
* foo = 1

15 changes: 15 additions & 0 deletions tests/testthat/test-slice-lazy.R
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,19 @@ test_that("slice_sample works with grouped data", {
)
})

test_that("slice_sample errors on unknown args", {
pl_mtcars <- as_polars_lf(mtcars)
skip_if_not(inherits(pl_mtcars, "RPolarsDataFrame"))
expect_snapshot_lazy(
pl_mtcars |>
slice_sample(weight_by = cyl > 5, n = 5),
error = TRUE
)
expect_snapshot_lazy(
pl_mtcars |>
slice_sample(foo = 1, n = 5),
error = TRUE
)
})

Sys.setenv('TIDYPOLARS_TEST' = FALSE)
15 changes: 15 additions & 0 deletions tests/testthat/test-slice.R
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,18 @@ test_that("slice_sample works with grouped data", {
NULL
)
})

test_that("slice_sample errors on unknown args", {
pl_mtcars <- as_polars_df(mtcars)
skip_if_not(inherits(pl_mtcars, "RPolarsDataFrame"))
expect_snapshot(
pl_mtcars |>
slice_sample(weight_by = cyl > 5, n = 5),
error = TRUE
)
expect_snapshot(
pl_mtcars |>
slice_sample(foo = 1, n = 5),
error = TRUE
)
})

0 comments on commit 7a2637a

Please sign in to comment.