Skip to content

Commit

Permalink
Rlang 1.0.0 (#337)
Browse files Browse the repository at this point in the history
* Depend on rlang >= 1.0.0

* Update snapshots

* Pass call to abort()

* Put warnings in `expect_snapshot()`

* Improve test for partial matching message

* Just suppress the warning...

* Suppress some more messages

* Replace `stopifnot()` for better error message
  • Loading branch information
mgirlich authored Feb 11, 2022
1 parent a8941f3 commit 87196d4
Show file tree
Hide file tree
Showing 23 changed files with 116 additions and 83 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Imports:
ellipsis,
glue,
lifecycle,
rlang,
rlang (>= 1.0.0),
tibble,
tidyselect,
vctrs
Expand Down
2 changes: 1 addition & 1 deletion R/count.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ check_name <- function(name, vars) {
))
}
} else if (!is_string(name)) {
abort("`name` must be a string")
abort("`name` must be a string", call = caller_env())
}

name
Expand Down
2 changes: 1 addition & 1 deletion R/replace_na.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,5 @@ check_replacement <- function(x, var) {
return()
}

abort(glue::glue("Replacement for `{var}` is length {n}, not length 1"))
abort(glue::glue("Replacement for `{var}` is length {n}, not length 1"), call = caller_env())
}
2 changes: 1 addition & 1 deletion R/step-subset-filter.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ check_filter <- function(...) {
x = glue::glue("Input `..{i}` is named."),
i = glue::glue("This usually means that you've used `=` instead of `==`."),
i = glue::glue("Did you mean `{name} == {as_label(expr)}`?", name = names(dots)[i])
))
), call = caller_env())
}

}
Expand Down
8 changes: 6 additions & 2 deletions R/step-subset-separate.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ separate.dtplyr_step <- function(data, col, into,
remove = TRUE,
convert = FALSE,
...) {
stopifnot(is.character(into))
stopifnot(is.character(sep))
if (!vctrs::vec_is(into, character())) {
abort("`into` must be a character vector.")
}
if (!vctrs::vec_is(sep, character())) {
abort("`sep` must be a character vector.")
}

col <- enexpr(col)

Expand Down
10 changes: 5 additions & 5 deletions R/step-subset-slice.R
Original file line number Diff line number Diff line change
Expand Up @@ -286,28 +286,28 @@ check_constant <- function(x, name, fn) {
})
}

check_slice_size <- function(n, prop, .slice_fn = "check_slice_size") {
check_slice_size <- function(n, prop, .slice_fn = "check_slice_size", call = caller_env()) {
if (missing(n) && missing(prop)) {
list(type = "n", n = 1L)
} else if (!missing(n) && missing(prop)) {
n <- check_constant(n, "n", .slice_fn)
if (!is.numeric(n) || length(n) != 1 || is.na(n)) {
abort("`n` must be a single number.")
abort("`n` must be a single number.", call = call)
}
list(type = "n", n = as.integer(n))
} else if (!missing(prop) && missing(n)) {
prop <- check_constant(prop, "prop", .slice_fn)
if (!is.numeric(prop) || length(prop) != 1 || is.na(prop)) {
abort("`prop` must be a single number.")
abort("`prop` must be a single number.", call = call)
}
list(type = "prop", prop = prop)
} else {
abort("Must supply exactly one of `n` and `prop` arguments.")
abort("Must supply exactly one of `n` and `prop` arguments.", call = call)
}
}

get_slice_size <- function(n, prop, .slice_fn = "get_slice_size") {
slice_input <- check_slice_size(n, prop, .slice_fn)
slice_input <- check_slice_size(n, prop, .slice_fn, call = caller_env())

if (slice_input$type == "n") {
if (slice_input$n < 0) {
Expand Down
4 changes: 2 additions & 2 deletions R/step-subset-summarise.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ check_summarise_vars <- function(dots) {
"`", names(dots)[[i]], "` ",
"refers to a variable created earlier in this summarise().\n",
"Do you need an extra mutate() step?"
))
), call = caller_env())
}
}
}
Expand All @@ -95,7 +95,7 @@ summarise_groups <- function(.data, .groups, env_caller) {
if (.groups == "rowwise") " in dtplyr"
),
i = 'Possible values are NULL (default), "drop_last", "drop", and "keep"'
))
), call = caller_env())
}

group_vars <- .data$groups
Expand Down
5 changes: 3 additions & 2 deletions tests/testthat/_snaps/count.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

Code
dt %>% count(name = 10) %>% collect()
Error <rlang_error>
`name` must be a string
Condition
Error in `tally()`:
! `name` must be a string

25 changes: 15 additions & 10 deletions tests/testthat/_snaps/step-call-pivot_longer.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,30 @@

Code
pivot_longer(dt, everything(), names_to = c(".value", "id"), names_sep = "_")
Error <rlang_error>
`data.table::melt()` doesn't currently support melting of unbalanced datasets.
Condition
Error in `pivot_longer()`:
! `data.table::melt()` doesn't currently support melting of unbalanced datasets.

# informative errors on unsupported features

Code
dt %>% pivot_longer(names_ptypes = list())
Error <rlang_error>
`names_ptypes` is not supported by dtplyr
Condition
Error in `tidyr::pivot_longer()`:
! `names_ptypes` is not supported by dtplyr
Code
dt %>% pivot_longer(names_transform = list())
Error <rlang_error>
`names_transform` is not supported by dtplyr
Condition
Error in `tidyr::pivot_longer()`:
! `names_transform` is not supported by dtplyr
Code
dt %>% pivot_longer(values_ptypes = list())
Error <rlang_error>
`values_ptypes` is not supported by dtplyr
Condition
Error in `tidyr::pivot_longer()`:
! `values_ptypes` is not supported by dtplyr
Code
dt %>% pivot_longer(values_transform = list())
Error <rlang_error>
`values_transform` is not supported by dtplyr
Condition
Error in `tidyr::pivot_longer()`:
! `values_transform` is not supported by dtplyr

7 changes: 4 additions & 3 deletions tests/testthat/_snaps/step-call-pivot_wider.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@

Code
pivot_wider(df, names_from = lab, values_from = val)
Error <vctrs_error_names_must_be_unique>
Names must be unique.
Condition
Error in `stop_vctrs()`:
! Names must be unique.
x These names are duplicated:
* "x" at locations 1 and 2.
Code
pivot_wider(df, names_from = lab, values_from = val, names_repair = "unique")
Message <simpleMessage>
Message
New names:
* x -> x...1
* x -> x...2
Expand Down
10 changes: 6 additions & 4 deletions tests/testthat/_snaps/step-call.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

Code
dt %>% rename_with(1)
Error <rlang_error>
`.fn` must be a function name or formula
Condition
Error in `rename_with()`:
! `.fn` must be a function name or formula

# rename_with generates minimal spec

Expand All @@ -20,7 +21,8 @@

Code
collect(drop_na(dt, "z"))
Error <vctrs_error_subscript_oob>
Can't subset columns that don't exist.
Condition
Error in `stop_subscript()`:
! Can't subset columns that don't exist.
x Column `z` doesn't exist.

3 changes: 2 additions & 1 deletion tests/testthat/_snaps/step-group.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

Code
. <- dt %>% group_by(x) %>% group_by(y, add = TRUE)
Warning <lifecycle_warning_deprecated>
Condition
Warning:
The `add` argument of `group_by()` is deprecated as of dplyr 1.0.0.
Please use the `.add` argument instead.

5 changes: 3 additions & 2 deletions tests/testthat/_snaps/step-mutate.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

Code
mutate(dt, y)
Error <simpleError>
object 'y' not found
Condition
Error:
! object 'y' not found

10 changes: 6 additions & 4 deletions tests/testthat/_snaps/step-subset-filter.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

Code
filter(dt, x = 1)
Error <rlang_error>
Problem with `filter()` input `..1`.
Condition
Error in `filter()`:
! Problem with `filter()` input `..1`.
x Input `..1` is named.
i This usually means that you've used `=` instead of `==`.
i Did you mean `x == 1`?
Expand All @@ -12,8 +13,9 @@

Code
filter(dt, y > 1, x = 1)
Error <rlang_error>
Problem with `filter()` input `..2`.
Condition
Error in `filter()`:
! Problem with `filter()` input `..2`.
x Input `..2` is named.
i This usually means that you've used `=` instead of `==`.
i Did you mean `x == 1`?
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/step-subset-select.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

Code
out <- lz %>% group_by(x) %>% select()
Message <rlang_message>
Message
Adding missing grouping variables: `x`

10 changes: 6 additions & 4 deletions tests/testthat/_snaps/step-subset-separate.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

Code
separate(dt, x, "x", FALSE)
Error <simpleError>
is.character(sep) is not TRUE
Condition
Error in `separate()`:
! `sep` must be a character vector.

---

Code
separate(dt, x, FALSE)
Error <simpleError>
is.character(into) is not TRUE
Condition
Error in `separate()`:
! `into` must be a character vector.

35 changes: 20 additions & 15 deletions tests/testthat/_snaps/step-subset-slice.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
# check_slice_catches common errors

Code
check_slice_size(n = 1, prop = 1)
Error <rlang_error>
Must supply exactly one of `n` and `prop` arguments.
slice_head(dt, n = 1, prop = 1)
Condition
Error in `slice_head()`:
! Must supply exactly one of `n` and `prop` arguments.
Code
check_slice_size(n = "a")
Error <rlang_error>
`n` must be a single number.
slice_head(dt, n = "a")
Condition
Error in `slice_head()`:
! `n` must be a single number.
Code
check_slice_size(prop = "a")
Error <rlang_error>
`prop` must be a single number.
slice_head(dt, prop = "a")
Condition
Error in `slice_head()`:
! `prop` must be a single number.
Code
check_slice_size(n = NA)
Error <rlang_error>
`n` must be a single number.
slice_head(dt, n = NA)
Condition
Error in `slice_head()`:
! `n` must be a single number.
Code
check_slice_size(prop = NA)
Error <rlang_error>
`prop` must be a single number.
slice_head(dt, prop = NA)
Condition
Error in `slice_head()`:
! `prop` must be a single number.

2 changes: 1 addition & 1 deletion tests/testthat/_snaps/step-subset-summarise.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Code
eval_bare(expr(lazy_dt(data.frame(x = 1, y = 2), "DT") %>% group_by(x, y) %>%
dplyr::summarise() %>% show_query()), env(global_env()))
Message <rlang_message>
Message
`summarise()` has grouped output by 'x'. You can override using the `.groups` argument.
Output
unique(DT)
Expand Down
10 changes: 6 additions & 4 deletions tests/testthat/_snaps/tidyeval-across.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@

Code
capture_across(dt, across(a, 1))
Error <rlang_error>
`.fns` argument to dtplyr::across() must be a NULL, a function, formula, or list
Condition
Error in `across_funs()`:
! `.fns` argument to dtplyr::across() must be a NULL, a function, formula, or list
Code
capture_across(dt, across(a, list(1)))
Error <rlang_error>
.fns argument to dtplyr::across() must contain a function or a formula
Condition
Error in `FUN()`:
! .fns argument to dtplyr::across() must contain a function or a formula
x Problem with 1

# if_all() gives informative errors
Expand Down
5 changes: 3 additions & 2 deletions tests/testthat/_snaps/tidyeval.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

Code
capture_dot(df, desc(a, b))
Error <rlang_error>
`desc()` expects exactly one argument.
Condition
Error in `dt_squash_call()`:
! `desc()` expects exactly one argument.

12 changes: 7 additions & 5 deletions tests/testthat/test-step-subset-slice.R
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,14 @@ test_that("slice_*() checks for constant n= and prop=", {
})

test_that("check_slice_catches common errors", {
dt <- lazy_dt(data.frame(x = 1:10))

expect_snapshot(error = TRUE, {
check_slice_size(n = 1, prop = 1)
check_slice_size(n = "a")
check_slice_size(prop = "a")
check_slice_size(n = NA)
check_slice_size(prop = NA)
slice_head(dt, n = 1, prop = 1)
slice_head(dt, n = "a")
slice_head(dt, prop = "a")
slice_head(dt, n = NA)
slice_head(dt, prop = NA)
})
})

Expand Down
9 changes: 5 additions & 4 deletions tests/testthat/test-step-subset-summarise.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ test_that("summarise peels off layer of grouping", {
dt <- lazy_dt(data.table(x = 1, y = 1, z = 1))
gt <- group_by(dt, x, y)

expect_equal(summarise(gt)$groups, "x")
expect_equal(summarise(summarise(gt))$groups, character())
suppressMessages({
expect_equal(summarise(gt)$groups, "x")
expect_equal(summarise(summarise(gt))$groups, character())
})
})

test_that("summarises sorts groups", {
Expand Down Expand Up @@ -83,7 +85,6 @@ test_that("summarise(.groups=)", {
expr(lazy_dt(data.frame(x = 1, y = 2), "DT") %>% group_by(x, y) %>% dplyr::summarise() %>% show_query()),
env(global_env())
))
skip_if(utils::packageVersion("rlang") < "0.5.0")
expect_snapshot(eval_bare(
expr(lazy_dt(data.frame(x = 1, y = 2), "DT") %>% group_by(x, y) %>% dplyr::summarise() %>% show_query()),
env(global_env())
Expand All @@ -96,7 +97,7 @@ test_that("summarise(.groups=)", {
))

df <- lazy_dt(data.table(x = 1, y = 2), "DT") %>% group_by(x, y)
expect_equal(df %>% summarise() %>% group_vars(), "x")
suppressMessages(expect_equal(df %>% summarise() %>% group_vars(), "x"))
expect_equal(df %>% summarise(.groups = "drop_last") %>% group_vars(), "x")
expect_equal(df %>% summarise(.groups = "drop") %>% group_vars(), character())
expect_equal(df %>% summarise(.groups = "keep") %>% group_vars(), c("x", "y"))
Expand Down
Loading

0 comments on commit 87196d4

Please sign in to comment.