feat: as_tibble() calls as.data.frame() for objects that are not subclasses of "tbl_df" - #1557
Conversation
|
Failing on pre 4.0.0 release due to use of |
krlmlr
left a comment
There was a problem hiding this comment.
Thanks! Running revdepchecks now.
| x <- structure(mtcars, extra = "extra", class = c("ext_df_", "data.frame")) | ||
| y <- as_tibble(head(mtcars)) | ||
| fun <- function(x, row.names = NULL, optional = FALSE, ...) y | ||
| .S3method("as.data.frame", "ext_df_", fun) |
There was a problem hiding this comment.
We have local_bindings() for this: https://github.com/r-lib/waldo/blob/56819ce69801b4ff93f4c62e5572bf7bfdd4db4a/tests/testthat/test-compare.R#L390 .
There was a problem hiding this comment.
Now updated using with_mocked_bindings().
| test_that("as_tibble.data.frame coerces extended data.frames first", { | ||
| x <- structure(mtcars, extra = "extra", class = c("ext_df_", "data.frame")) | ||
| y <- as_tibble(head(mtcars)) | ||
| fun <- function(x, row.names = NULL, optional = FALSE, ...) y |
There was a problem hiding this comment.
I wonder if this method should be doing something more exciting to verify that it's actually called. Maybe set a flag in the test, or remove some attribute of the object?
There was a problem hiding this comment.
I thought it was pretty extreme as is (returning head of global variable with one less attribute) but I may be misunderstanding
|
I suspect it's worth understanding the dplyr failure -- the other failures could be a consequence of that. |
hmm - I hadn't seen that there was no explicit method for tbl_df. Now fixed (hopefully) with latest commit. I've rechecked dplyr and seems ok so 🤞 . Note the actions are currently failing but not due to package code. |
|
Perhaps we want a separate It's always amazing to see how an innocuous change can quickly turn into a deep rabbit hole. I can't spend too much time digging myself, but happy to assist. I'm seeing the GHA failure elsewhere, need to fix it. |
27f40c1 to
23dd204
Compare
|
Yeah - I wondered that too but didn't want to make the hole any bigger. I think one or two other packages (e.g. LexisNexisTools) may be genuine breakages (only due to an unneeded attribute that has been tested against). I'll dig in to these over the next few days and update with any info. No rush to get this sorted - we will get there in the end. |
|
I wonder what's wrong with the GHA checks. The main branch looks good. |
|
Happy to poke at the revdeps again if it's not too costly and they can be rerun? (unsure on your CI setup for the gazillion revdeps you have). |
This patch ensures as_tibble.data.frame first calls as.data.frame to allow for methods provided by extended data frame classes. The idea was proposed by Jan Gorecki in Rdatatable/data.table#5698.
2e64850 to
9a6911b
Compare
as_tibble() calls as.data.frame() for objects that are not subclasses of "tbl_df"
|
Thanks. I think it's good as is, I'll wait for a bit until the CRAN release. |
| y <- as_tibble(head(mtcars)) | ||
| with_mocked_bindings( | ||
| as.data.frame = function(x, row.names = NULL, optional = FALSE, ...) y, | ||
| code = expect_identical(as_tibble(x), y), |
There was a problem hiding this comment.
Just from looking at the code here it feels it should fail because y is only a head of mtcars while x is complete mtcars.
There was a problem hiding this comment.
Why should it fail? Are you expecting as_tibble(x) to be identical to mtcars converted to a tibble?
Would the test would be clearer if we used two completely unrelated data frames for x and y?
There was a problem hiding this comment.
Would the test would be clearer if we used two completely unrelated data frames for
xandy?
It's been a while since I wrote the test and it did take me a moment to grok what was going on when rereading so it may be worth changing to y <- airquality . I can also add some commentary and/or link to the issue/PR for context/clarity if useful.
There was a problem hiding this comment.
because y was created from already filtered out object, using head(mtcars) 3 lines above while x contains complete mtcars
There was a problem hiding this comment.
But the tests succeed, and the example should also work when running it line by line.
|
It's been a while. This produces a regression in opendataformat/r-package-opendataformat#2 . |
This patch ensures as_tibble.data.frame first calls as.data.frame
to allow for methods provided by extended data frame classes. The
idea was proposed by Jan Gorecki in
Rdatatable/data.table#5698.
Fixes #1556.