diff --git a/R/cast.R b/R/cast.R index 3eab09067..d32e4fb17 100644 --- a/R/cast.R +++ b/R/cast.R @@ -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)) { diff --git a/tests/testthat/_snaps/cast.md b/tests/testthat/_snaps/cast.md index 170a26aba..d77ca4507 100644 --- a/tests/testthat/_snaps/cast.md +++ b/tests/testthat/_snaps/cast.md @@ -62,15 +62,6 @@ Error: ! Can't combine `..1$a$b` > and `..2$a$b` . -# vec_cast() only falls back when casting to base type - - Code - (expect_error(vec_cast(mtcars, foobar(mtcars)))) - Output - - Error: - ! Can't convert `mtcars` to . - # vec_cast() only attempts to fall back if `to` is a data frame (#1568) Code diff --git a/tests/testthat/_snaps/type-misc.md b/tests/testthat/_snaps/type-misc.md index dc44c3fa8..ba0833783 100644 --- a/tests/testthat/_snaps/type-misc.md +++ b/tests/testthat/_snaps/type-misc.md @@ -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: - ! Can't convert `tibble(y = 2)` to . - # data table has formatting methods Code diff --git a/tests/testthat/test-cast.R b/tests/testthat/test-cast.R index 163e9ca68..af5b6a4ca 100644 --- a/tests/testthat/test-cast.R +++ b/tests/testthat/test-cast.R @@ -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)", { @@ -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) +}) diff --git a/tests/testthat/test-type-misc.R b/tests/testthat/test-type-misc.R index 8b4905467..e1c61c7d4 100644 --- a/tests/testthat/test-type-misc.R +++ b/tests/testthat/test-type-misc.R @@ -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", {