Skip to content

Commit

Permalink
Revert "Don't allow casting to non-bare data frames"
Browse files Browse the repository at this point in the history
This reverts commit b4f1c40.
  • Loading branch information
lionel- committed Oct 21, 2022
1 parent 3509722 commit f7a7815
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 32 deletions.
11 changes: 5 additions & 6 deletions R/cast.R
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,12 @@ vec_default_cast <- function(x,
return(out)
}

if (is_bare_df(to)) {
# Bare-class fallback for data frames
if (inherits(to, "tbl_df")) {
out <- df_as_tibble(out)
}
return(out)
# Bare-class fallback for data frames.
# FIXME: Should we only allow it when target is a bare df?
if (inherits(to, "tbl_df")) {
out <- df_as_tibble(out)
}
return(out)
}

if (is_same_type(x, to)) {
Expand Down
9 changes: 0 additions & 9 deletions tests/testthat/_snaps/cast.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,6 @@
Error:
! Can't combine `..1$a$b` <factor<c1562>> and `..2$a$b` <double>.

# vec_cast() only falls back when casting to base type

Code
(expect_error(vec_cast(mtcars, foobar(mtcars))))
Output
<error/vctrs_error_cast>
Error:
! Can't convert `mtcars` <data.frame> to <vctrs_foobar>.

# vec_cast() only attempts to fall back if `to` is a data frame (#1568)

Code
Expand Down
9 changes: 0 additions & 9 deletions tests/testthat/_snaps/type-misc.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
# data.table and tibble do not have a common type

Code
(expect_error(vec_cast(tibble(y = 2), data.table(x = TRUE, y = 1L))))
Output
<error/vctrs_error_cast>
Error:
! Can't convert `tibble(y = 2)` <tibble> to <data.table>.

# data table has formatting methods

Code
Expand Down
27 changes: 23 additions & 4 deletions tests/testthat/test-cast.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,9 @@ test_that("unspecified can be cast to shaped vectors", {
expect_identical(out, exp)
})

test_that("vec_cast() only falls back when casting to base type", {
test_that("vec_cast() falls back to base class even when casting to non-base type", {
expect_equal(vec_cast(foobar(mtcars), mtcars), mtcars)
expect_snapshot({
(expect_error(vec_cast(mtcars, foobar(mtcars))))
})
expect_equal(vec_cast(mtcars, foobar(mtcars)), mtcars)
})

test_that("vec_cast() only attempts to fall back if `to` is a data frame (#1568)", {
Expand Down Expand Up @@ -284,3 +282,24 @@ test_that("df-fallback for cast is not sensitive to attributes order", {

expect_identical(vec_cast(x, ptype), x)
})

test_that("bare-type fallback for df-cast works", {
# NOTE: Not sure why this was necessary. The cubble and yamlet
# packages fail without this.

df <- data_frame(x = 1, y = yamlet::classified("foo"))
gdf <- dplyr::new_grouped_df(
df,
data_frame(x = 1, .rows = list(1L)),
class = "vctrs_foobar"
)

exp_df <- data_frame(x = c(1, 1), y = yamlet::classified(c("foo", "foo")))
exp_gdf <- dplyr::new_grouped_df(
exp_df,
data_frame(x = 1, .rows = list(1:2)),
class = "vctrs_foobar"
)

expect_error(vec_rbind(gdf, gdf), NA)
})
8 changes: 4 additions & 4 deletions tests/testthat/test-type-misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ test_that("data.table and tibble do not have a common type", {
vec_cast(data.table(y = 2), tibble(x = TRUE, y = 1L)),
tibble(x = lgl(NA), y = 2L)
)

expect_snapshot({
(expect_error(vec_cast(tibble(y = 2), data.table(x = TRUE, y = 1L))))
})
expect_identical(
vec_cast(tibble(y = 2), data.table(x = TRUE, y = 1L)),
data_frame(x = lgl(NA), y = 2L)
)
})

test_that("data table has formatting methods", {
Expand Down

0 comments on commit f7a7815

Please sign in to comment.