Skip to content

Commit 6ee0ee7

Browse files
CopilotkrlmlrCopilot
authored
fix: error call in with_vertex_()/with_edge_() should report make_(), not internal helper (#2609)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: krlmlr <1741643+krlmlr@users.noreply.github.com> Co-authored-by: Copilot <Copilot@users.noreply.github.com>
1 parent dc03b52 commit 6ee0ee7

3 files changed

Lines changed: 39 additions & 17 deletions

File tree

R/attributes.R

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -527,20 +527,23 @@ vertex_attr <- function(graph, name, index = V(graph)) {
527527
#' g
528528
#' plot(g)
529529
set_vertex_attr <- function(graph, name, index = V(graph), value) {
530+
call <- rlang::current_env()
530531
check_string(name)
531532
if (is_complete_iterator(index)) {
532533
return(i_set_vertex_attr(
533534
graph = graph,
534535
name = name,
535536
value = value,
536-
check = FALSE
537+
check = FALSE,
538+
call = call
537539
))
538540
} else {
539541
return(i_set_vertex_attr(
540542
graph = graph,
541543
name = name,
542544
index = index,
543-
value = value
545+
value = value,
546+
call = call
544547
))
545548
}
546549
graph
@@ -566,6 +569,7 @@ set_vertex_attr <- function(graph, name, index = V(graph), value) {
566569
#' # to set an attribute named "index" use `:=`
567570
#' set_vertex_attrs(g, color = "blue", index := 10, name = LETTERS[1:10])
568571
set_vertex_attrs <- function(graph, ..., index = V(graph)) {
572+
call <- rlang::current_env()
569573
dots <- rlang::list2(...)
570574

571575
if (!rlang::is_named(dots)) {
@@ -578,7 +582,8 @@ set_vertex_attrs <- function(graph, ..., index = V(graph)) {
578582
graph,
579583
name = attr_name,
580584
index = index,
581-
value = attr_value
585+
value = attr_value,
586+
call = call
582587
)
583588
}
584589

@@ -590,7 +595,8 @@ i_set_vertex_attr <- function(
590595
name,
591596
index = V(graph),
592597
value,
593-
check = TRUE
598+
check = TRUE,
599+
call = rlang::caller_env()
594600
) {
595601
ensure_igraph(graph)
596602
check_string(name)
@@ -632,7 +638,8 @@ i_set_vertex_attr <- function(
632638
value_in <- unname(value)
633639
} else {
634640
cli::cli_abort(
635-
"Length of new attribute value must be {if (length(index) != 1) '1 or '}{length(index)}, the number of target vertices, not {length(value)}."
641+
"Length of new attribute value must be {if (length(index) != 1) '1 or '}{length(index)}, the number of target vertices, not {length(value)}.",
642+
call = call
636643
)
637644
}
638645

@@ -830,11 +837,24 @@ edge_attr <- function(graph, name, index = E(graph)) {
830837
#' g
831838
#' plot(g)
832839
set_edge_attr <- function(graph, name, index = E(graph), value) {
840+
call <- rlang::current_env()
833841
check_string(name)
834842
if (is_complete_iterator(index)) {
835-
i_set_edge_attr(graph = graph, name = name, value = value, check = FALSE)
843+
i_set_edge_attr(
844+
graph = graph,
845+
name = name,
846+
value = value,
847+
check = FALSE,
848+
call = call
849+
)
836850
} else {
837-
i_set_edge_attr(graph = graph, name = name, index = index, value = value)
851+
i_set_edge_attr(
852+
graph = graph,
853+
name = name,
854+
index = index,
855+
value = value,
856+
call = call
857+
)
838858
}
839859
}
840860

@@ -843,7 +863,8 @@ i_set_edge_attr <- function(
843863
name,
844864
index = E(graph),
845865
value,
846-
check = TRUE
866+
check = TRUE,
867+
call = rlang::caller_env()
847868
) {
848869
ensure_igraph(graph)
849870
check_string(name)
@@ -885,7 +906,8 @@ i_set_edge_attr <- function(
885906
value_in <- unname(value)
886907
} else {
887908
cli::cli_abort(
888-
"Length of new attribute value must be {if (length(index) != 1) '1 or '}{length(index)}, the number of target edges, not {length(value)}."
909+
"Length of new attribute value must be {if (length(index) != 1) '1 or '}{length(index)}, the number of target edges, not {length(value)}.",
910+
call = call
889911
)
890912
}
891913

R/make.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,7 @@ graph.atlas <- function(n) {
841841
#' @param mods The modifiers to apply
842842
#' @return The modified graph
843843
#' @dev
844-
.apply_modifiers <- function(graph, mods) {
844+
.apply_modifiers <- function(graph, mods, call = rlang::caller_env()) {
845845
for (m in mods) {
846846
if (m$id == "without_attr") {
847847
## TODO: speed this up
@@ -870,7 +870,7 @@ graph.atlas <- function(n) {
870870
n <- names(m$args)[a]
871871
v <- m$args[[a]]
872872
stopifnot(!is.null(n))
873-
graph <- set_vertex_attr(graph, n, value = v)
873+
graph <- i_set_vertex_attr(graph, n, value = v, call = call)
874874
}
875875
} else if (m$id == "with_edge_") {
876876
m$args <- lapply(m$args, eval)
@@ -879,7 +879,7 @@ graph.atlas <- function(n) {
879879
n <- names(m$args)[a]
880880
v <- m$args[[a]]
881881
stopifnot(!is.null(n))
882-
graph <- set_edge_attr(graph, n, value = v)
882+
graph <- i_set_edge_attr(graph, n, value = v, call = call)
883883
}
884884
} else if (m$id == "with_graph_") {
885885
m$args <- lapply(m$args, eval)
@@ -949,7 +949,7 @@ make_ <- function(...) {
949949
}
950950

951951
res <- do_call(cons$fun, cons_args, extracted$args)
952-
.apply_modifiers(res, extracted$mods)
952+
.apply_modifiers(res, extracted$mods, call = rlang::current_env())
953953
}
954954

955955
#' Sample from a random graph model

tests/testthat/_snaps/attributes.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,31 @@
33
Code
44
make_(from_literal(A - A:B:C, B - A:B:C), with_vertex_(color = 1:2))
55
Condition
6-
Error in `i_set_vertex_attr()`:
6+
Error in `make_()`:
77
! Length of new attribute value must be 1 or 3, the number of target vertices, not 2.
88

99
# with_edge_
1010

1111
Code
1212
make_(from_literal(A - A:B:C, B - A:B:C), with_edge_(color = 1:2))
1313
Condition
14-
Error in `i_set_edge_attr()`:
14+
Error in `make_()`:
1515
! Length of new attribute value must be 1 or 3, the number of target edges, not 2.
1616

1717
# error messages work
1818

1919
Code
2020
set_vertex_attr(g, "test", value = c(1, 2))
2121
Condition
22-
Error in `i_set_vertex_attr()`:
22+
Error in `set_vertex_attr()`:
2323
! Length of new attribute value must be 1 or 5, the number of target vertices, not 2.
2424

2525
---
2626

2727
Code
2828
set_edge_attr(g, "test", value = c(1, 2))
2929
Condition
30-
Error in `i_set_edge_attr()`:
30+
Error in `set_edge_attr()`:
3131
! Length of new attribute value must be 1 or 10, the number of target edges, not 2.
3232

3333
---

0 commit comments

Comments
 (0)