Skip to content

Commit 1481863

Browse files
maelleaviator-bot
authored and
aviator-bot
committed
refactor: add assert_named_list() helper to assert the value is a named list with no duplicate names
1 parent 36ad3c9 commit 1481863

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

R/attributes.R

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -349,10 +349,7 @@ graph.attributes <- function(graph) {
349349
#' @export
350350
"graph.attributes<-" <- function(graph, value) {
351351
ensure_igraph(graph)
352-
if (!is.list(value) || (length(value) > 0 && is.null(names(value))) ||
353-
any(names(value) == "") || any(duplicated(names(value)))) {
354-
stop("Value must be a named list with unique names")
355-
}
352+
assert_named_list(value)
356353

357354
.Call(R_igraph_mybracket2_set, graph, igraph_t_idx_attr, igraph_attr_idx_graph, value)
358355
}
@@ -535,10 +532,8 @@ vertex.attributes <- function(graph, index = V(graph)) {
535532
"vertex.attributes<-" <- function(graph, index = V(graph), value) {
536533
ensure_igraph(graph)
537534

538-
if (!is.list(value) || (length(value) > 0 && is.null(names(value))) ||
539-
any(names(value) == "") || any(duplicated(names(value)))) {
540-
stop("Value must be a named list with unique names")
541-
}
535+
assert_named_list(value)
536+
542537
if (any(sapply(value, length) != length(index))) {
543538
stop("Invalid attribute value length, must match number of vertices")
544539
}
@@ -744,10 +739,8 @@ edge.attributes <- function(graph, index = E(graph)) {
744739
"edge.attributes<-" <- function(graph, index = E(graph), value) {
745740
ensure_igraph(graph)
746741

747-
if (!is.list(value) || (length(value) > 0 && is.null(names(value))) ||
748-
any(names(value) == "") || any(duplicated(names(value)))) {
749-
stop("Value must be a named list with unique names")
750-
}
742+
assert_named_list(value)
743+
751744
if (any(sapply(value, length) != length(index))) {
752745
stop("Invalid attribute value length, must match number of edges")
753746
}
@@ -1189,3 +1182,20 @@ NULL
11891182
`$<-.igraph` <- function(x, name, value) {
11901183
set_graph_attr(x, name, value)
11911184
}
1185+
1186+
assert_named_list <- function(value) {
1187+
error_msg <- "{.arg value} must be a named list with unique names"
1188+
1189+
if (!is.list(value)) {
1190+
rlang::abort(error_msg)
1191+
}
1192+
1193+
if (length(value) == 0) {
1194+
return()
1195+
}
1196+
1197+
if (!rlang::is_named(value) || anyDuplicated(names(value)) > 0) {
1198+
1199+
rlang::abort(error_msg)
1200+
}
1201+
}

tests/testthat/test-attributes.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,3 +342,20 @@ test_that("edge attributes are destroyed when the graph is destroyed", {
342342
gc()
343343
expect_true(finalized)
344344
})
345+
346+
test_that("assert_named_list() works", {
347+
not_list <- 1:10
348+
expect_error(assert_named_list(not_list), "named list")
349+
350+
expect_silent(assert_named_list(list()))
351+
352+
unnamed_list <- as.list(1:10)
353+
expect_error(assert_named_list(unnamed_list), "named list")
354+
355+
empty_name <- rlang::set_names(unnamed_list, c(as.character(1:9), ""))
356+
expect_error(assert_named_list(empty_name), "named list")
357+
358+
dups <- rlang::set_names(unnamed_list, rep("bla", 10))
359+
expect_error(assert_named_list(dups), "named list")
360+
361+
})

0 commit comments

Comments
 (0)