Skip to content

Commit 8d0dfe7

Browse files
Copilotkrlmlr
andauthored
feat: vertices() errors on duplicate attribute names (#2430)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: krlmlr <[email protected]> Co-authored-by: Kirill Müller <[email protected]>
1 parent c7ae980 commit 8d0dfe7

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

R/operators.R

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,21 @@ edges <- edge
967967
#' g
968968
#' plot(g)
969969
vertex <- function(...) {
970-
structure(list(...), class = "igraph.vertex")
970+
args <- list(...)
971+
arg_names <- names(args)
972+
973+
# Check for duplicate named arguments
974+
if (!is.null(arg_names)) {
975+
named_args <- arg_names[arg_names != ""]
976+
if (anyDuplicated(named_args)) {
977+
duplicates <- unique(named_args[duplicated(named_args)])
978+
cli::cli_abort(
979+
"Duplicate attribute {cli::qty(duplicates)}name{?s} in {.fn vertices}: {.val {duplicates}}."
980+
)
981+
}
982+
}
983+
984+
structure(args, class = "igraph.vertex")
971985
}
972986

973987
#' @export

tests/testthat/_snaps/operators.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,19 @@
22

33
Can't recycle `name` (size 2) to match `foo` (size 3).
44

5+
# vertices() errors on duplicate attribute names
6+
7+
Duplicate attribute name in `vertices()`: "name".
8+
9+
---
10+
11+
Duplicate attribute name in `vertices()`: "blop".
12+
13+
---
14+
15+
Duplicate attribute name in `vertices()`: "name".
16+
17+
---
18+
19+
Duplicate attribute names in `vertices()`: "foo" and "bar".
20+

tests/testthat/test-operators.R

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,28 @@ test_that("vertices() works", {
252252
expect_snapshot_error(make_empty_graph(1) + vertices("a", "b", foo = 5:7))
253253
})
254254

255+
test_that("vertices() errors on duplicate attribute names", {
256+
# Test case from issue: vertices("a", name = "c")
257+
expect_snapshot_error(
258+
vertices("a", name = "c", name = "d")
259+
)
260+
261+
# Test case from issue: vertices("a", blop = "c", blop = 1)
262+
expect_snapshot_error(
263+
vertices("a", blop = "c", blop = 1)
264+
)
265+
266+
# Test with graph addition
267+
expect_snapshot_error(
268+
make_empty_graph(1) + vertices("a", "b", name = "c", name = "d")
269+
)
270+
271+
# Test multiple duplicates
272+
expect_snapshot_error(
273+
vertices(foo = 1, foo = 2, bar = 3, bar = 4)
274+
)
275+
})
276+
255277
test_that("infix operators work", {
256278
g <- make_ring(10)
257279
V(g)$name <- letters[1:10]

0 commit comments

Comments
 (0)