Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove proxy check in vec_is_list() #1222

Merged
merged 4 commits into from
Aug 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

# vctrs (development version)

* The restriction that S3 lists must have a list-based proxy to be considered
lists by `vec_is_list()` has been removed (#1208).

* New performant `data_frame()` constructor for creating data frames in a way
that follows tidyverse semantics. Among other things, inputs are recycled
using tidyverse recycling rules, strings are never converted to factors,
Expand Down
2 changes: 0 additions & 2 deletions R/assert.R
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,6 @@ vec_is_vector <- function(x) {
#' Notably, data frames and S3 record style classes like POSIXlt are not
#' considered lists.
#'
#' If `x` inherits explicitly from `"list"`, it is also required that the
#' proxy returned by [vec_proxy()] is a list. If it is not, an error is thrown.
#' @export
#' @examples
#' vec_is_list(list())
Expand Down
3 changes: 0 additions & 3 deletions man/vec_is_list.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 1 addition & 39 deletions src/type-info.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ SEXP vctrs_is_list(SEXP x) {
return Rf_ScalarLogical(vec_is_list(x));
}

static bool vec_proxy_is_list(SEXP x);

// [[ include("vctrs.h") ]]
bool vec_is_list(SEXP x) {
// Require `x` to be a list internally
Expand All @@ -120,43 +118,7 @@ bool vec_is_list(SEXP x) {
}

// Classed VECSXP are only lists if the last class is explicitly `"list"`
if (class_type(x) != vctrs_class_list) {
return false;
}

// If `x` is an explicit list, also require that the proxy is a list
if (vec_proxy_is_list(x)) {
return true;
}

Rf_errorcall(
R_NilValue,
"`x` inherits explicitly from \"list\", "
"but the proxy returned by `vec_proxy()` is not a list."
);
}

static bool vec_proxy_is_list(SEXP x) {
SEXP proxy = PROTECT(vec_proxy(x));

// Require `proxy` to be a list internally
if (TYPEOF(proxy) != VECSXP) {
UNPROTECT(1);
return false;
}

// Unclassed VECSXP are lists
if (!OBJECT(proxy)) {
UNPROTECT(1);
return true;
}

// If the `proxy` has a class, only require
// that it is not a data frame
bool out = class_type(proxy) != vctrs_class_data_frame;

UNPROTECT(1);
return out;
return class_type(x) == vctrs_class_list;
}


Expand Down
24 changes: 22 additions & 2 deletions tests/testthat/test-assert.R
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,28 @@ test_that("data frames of all types are not lists", {
expect_false(vec_is_list(tibble::tibble()))
})

test_that("proxy of S3 lists must be a list", {
test_that("S3 list with non-list proxy is still a list (#1208)", {
x <- structure(list(), class = c("foobar", "list"))
local_methods(vec_proxy.foobar = function(x) 1)
expect_error(vec_is_list(x), "`x` inherits")
# This used to be an error (#1003)
# expect_error(vec_is_list(x), "`x` inherits")
expect_true(vec_is_list(x))
})

test_that("list-rcrds with data frame proxies are considered lists (#1208)", {
x <- structure(
list(1:2, "x"),
special = c("a", "b"),
class = c("list_rcrd", "list")
)

local_methods(
vec_proxy.list_rcrd = function(x) {
special <- attr(x, "special")
data <- unstructure(x)
new_data_frame(list(data = data, special = special))
}
)

expect_true(vec_is_list(x))
})