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

Allow NULL dimnames to propagate through new_table() #1889

Merged
merged 3 commits into from
Nov 2, 2023
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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# vctrs (development version)

* Fixed tests related to changes to `dim<-()` in R-devel (#1889).

# vctrs 0.6.4

* Fixed a performance issue with `vec_c()` and ALTREP vectors (in particular,
Expand Down
2 changes: 0 additions & 2 deletions R/type-table.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ new_table <- function(x = integer(), dim = NULL, dimnames = NULL) {
abort("`dim` must be an integer vector.")
}

dimnames <- dimnames %||% vec_init(list(), length(dim))

n_elements <- prod(dim)
n_x <- length(x)

Expand Down
5 changes: 0 additions & 5 deletions tests/testthat/helper-size.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,3 @@
expect_size <- function(object, n) {
expect_identical(vec_size(object), vec_cast(n, int()))
}

zap_dimnames <- function(x) {
attr(x, "dimnames") <- NULL
x
}
22 changes: 15 additions & 7 deletions tests/testthat/test-type-table.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,42 @@ test_that("can find a common type among tables with identical dimensions", {
tab1 <- new_table()
tab2 <- new_table(1:2, dim = c(1L, 2L, 1L))

expect_identical(vec_ptype2(tab1, tab1), zap_dimnames(new_table()))
expect_identical(vec_ptype2(tab2, tab2), zap_dimnames(new_table(dim = c(0L, 2L, 1L))))
expect_identical(vec_ptype2(tab1, tab1), new_table())
expect_identical(vec_ptype2(tab2, tab2), new_table(dim = c(0L, 2L, 1L)))
})

test_that("size is not considered in the ptype", {
x <- new_table(1:2, dim = 2L)
y <- new_table(1:3, dim = 3L)

expect_identical(vec_ptype2(x, y), zap_dimnames(new_table()))
expect_identical(vec_ptype2(x, y), new_table())
})

test_that("vec_ptype2() can broadcast table shapes", {
x <- new_table(dim = c(0L, 1L))
y <- new_table(dim = c(0L, 2L))

expect_identical(vec_ptype2(x, y), zap_dimnames(new_table(dim = c(0L, 2L))))
expect_identical(vec_ptype2(x, y), new_table(dim = c(0L, 2L)))

x <- new_table(dim = c(0L, 1L, 3L))
y <- new_table(dim = c(0L, 2L, 1L))

expect_identical(vec_ptype2(x, y), zap_dimnames(new_table(dim = c(0L, 2L, 3L))))
expect_identical(vec_ptype2(x, y), new_table(dim = c(0L, 2L, 3L)))
})

test_that("vec_ptype2() never propagates dimnames", {
x <- new_table(dim = c(0L, 1L), dimnames = list(character(), "x1"))
y <- new_table(dim = c(0L, 2L), dimnames = list(character(), c("y1", "y2")))

expect_null(dimnames(vec_ptype2(x, x)))
expect_null(dimnames(vec_ptype2(x, y)))
})

test_that("implicit axes are broadcast", {
x <- new_table(dim = c(0L, 2L))
y <- new_table(dim = c(0L, 1L, 3L))

expect_identical(vec_ptype2(x, y), zap_dimnames(new_table(dim = c(0L, 2L, 3L))))
expect_identical(vec_ptype2(x, y), new_table(dim = c(0L, 2L, 3L)))
})

test_that("errors on non-broadcastable dimensions", {
Expand Down Expand Up @@ -80,7 +88,7 @@ test_that("common types have symmetry when mixed with unspecified input", {
test_that("`table` delegates coercion", {
expect_identical(
vec_ptype2(new_table(1), new_table(FALSE)),
zap_dimnames(new_table(double()))
new_table(double())
)
expect_error(
vec_ptype2(new_table(1), new_table("")),
Expand Down
Loading