Skip to content

Commit

Permalink
Improve unchop() error for incompatible ptype (#1478)
Browse files Browse the repository at this point in the history
* Improve `unchop()` error for incompatible ptype

* Update snapshot

* Tweak test

* Tweak formatting

* Add a comment about something that confused me

---------

Co-authored-by: Davis Vaughan <[email protected]>
  • Loading branch information
mgirlich and DavisVaughan authored Aug 28, 2024
1 parent c252a61 commit 454a4c8
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# tidyr (development version)

* `unchop()` produces a more helpful error message when columns cannot be cast
to `ptype` (@mgirlich, #1477).

* `expand_grid()` gains a new `.vary` argument, allowing users to control
whether the first column varies fastest or slowest (#1543, @JamesHWade).

Expand Down
15 changes: 13 additions & 2 deletions R/chop.R
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,14 @@ df_unchop <- function(x, ..., ptype = NULL, keep_empty = FALSE, error_call = cal
col_sizes <- x_sizes[[i]]

if (!col_is_list) {
# Optimize rare non list-cols
if (!is_null(col_ptype)) {
col <- vec_cast(col, col_ptype, x_arg = col_name, call = error_call)
col <- vec_cast(
x = col,
to = col_ptype,
x_arg = col_name,
call = error_call
)
}
out_cols[[i]] <- vec_slice(col, out_loc)
next
Expand All @@ -267,7 +273,12 @@ df_unchop <- function(x, ..., ptype = NULL, keep_empty = FALSE, error_call = cal
row_recycle <- col_sizes != sizes
col[row_recycle] <- map2(col[row_recycle], sizes[row_recycle], vec_recycle, call = error_call)

col <- list_unchop(col, ptype = col_ptype)
col <- list_unchop(
x = col,
ptype = col_ptype,
error_arg = col_name,
error_call = error_call
)

if (is_null(col)) {
# This can happen when both of these are true:
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/chop.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
Error in `chop()`:
! `cols` is absent but must be supplied.

# incompatible ptype mentions the column (#1477)

Code
unnest(df, data, ptype = list(data = integer()))
Condition
Error in `unnest()`:
! Can't convert `data[[2]]` <character> to <integer>.

# incompatible sizes are caught

Code
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/unnest.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
(expect_error(unnest(df, x)))
Output
<error/vctrs_error_ptype2>
Error in `list_unchop()`:
Error in `unnest()`:
! Can't combine `x[[1]]` <double> and `x[[2]]` <tbl_df>.

# unnest() advises on outer / inner name duplication
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-chop.R
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,14 @@ test_that("`ptype = list()` uses list ptype", {
)
})

test_that("incompatible ptype mentions the column (#1477)", {
df <- tibble(data = list(1, "2"))

expect_snapshot(error = TRUE, {
unnest(df, data, ptype = list(data = integer()))
})
})

test_that("unchopping a bare empty list results in unspecified()", {
df <- tibble(x = integer(), y = list())
expect <- tibble(x = integer(), y = unspecified())
Expand Down

0 comments on commit 454a4c8

Please sign in to comment.