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

Use "can't" phrasing in headers of subscript errors #1736

Merged
merged 1 commit into from
Oct 21, 2022
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
14 changes: 11 additions & 3 deletions R/subscript.R
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,20 @@ new_error_subscript_type <- function(i,

#' @export
cnd_header.vctrs_error_subscript_type <- function(cnd) {
action <- cnd_subscript_action(cnd)
arg <- cnd[["subscript_arg"]]
if (is_subscript_arg(arg)) {
with <- glue::glue(" with {format_subscript_arg(arg)}")
} else {
with <- ""
}

action <- cnd_subscript_action(cnd, assign_to = FALSE)
elt <- cnd_subscript_element(cnd)

if (cnd_subscript_scalar(cnd)) {
glue::glue("Must {action} {elt[[1]]} with a single valid subscript.")
glue::glue("Can't {action} {elt[[1]]}{with}.")
} else {
glue::glue("Must {action} {elt[[2]]} with a valid subscript vector.")
glue::glue("Can't {action} {elt[[2]]}{with}.")
}
}
#' @export
Expand Down
10 changes: 5 additions & 5 deletions tests/testthat/_snaps/error-call.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@
Output
<error/vctrs_error_subscript_type>
Error in `my_function()`:
! Must subset elements with a valid subscript vector.
! Can't subset elements with `my_arg`.
x Can't convert from `my_arg` <double> to <integer> due to loss of precision.

---
Expand All @@ -228,7 +228,7 @@
Output
<error/vctrs_error_subscript_type>
Error in `my_function()`:
! Must subset elements with a valid subscript vector.
! Can't subset elements.
x Can't convert from <double> to <integer> due to loss of precision.

---
Expand All @@ -238,7 +238,7 @@
Output
<error/vctrs_error_subscript_type>
Error in `my_function()`:
! Must subset elements with a valid subscript vector.
! Can't subset elements with `my_arg`.
x `my_arg` must be logical, numeric, or character, not an empty list.

---
Expand All @@ -258,7 +258,7 @@
Output
<error/vctrs_error_subscript_type>
Error in `my_function()`:
! Must subset elements with a valid subscript vector.
! Can't subset elements.
x Subscript can't contain missing values.
x It has a missing value at location 1.

Expand Down Expand Up @@ -326,7 +326,7 @@
Output
<error/vctrs_error_subscript_type>
Error in `vec_slice()`:
! Must subset elements with a valid subscript vector.
! Can't subset elements with `i`.
x `i` must be logical, numeric, or character, not an environment.

# list_sizes() reports error context
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/_snaps/slice-assign.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
Output
<error/vctrs_error_subscript_size>
Error:
! Must assign to elements with a valid subscript vector.
! Can't assign elements.
x Logical subscript must be size 1 or 2, not 3.

---
Expand All @@ -27,7 +27,7 @@
Output
<error/vctrs_error_subscript_size>
Error:
! Must assign to elements with a valid subscript vector.
! Can't assign elements.
x Logical subscript must be size 1 or 32, not 2.

# must assign existing elements
Expand Down Expand Up @@ -72,7 +72,7 @@
Output
<error/vctrs_error_subscript_type>
Error:
! Must assign to elements with a valid subscript vector.
! Can't assign elements.
x Negative and positive locations can't be mixed.
i Subscript has a positive value at location 2.
Code
Expand All @@ -81,7 +81,7 @@
Output
<error/vctrs_error_subscript_type>
Error:
! Must assign to elements with a valid subscript vector.
! Can't assign elements.
x Negative locations can't have missing values.
i Subscript has a missing value at location 2.

Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/_snaps/slice-chop.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
Output
<error/vctrs_error_subscript_type>
Error:
! Must subset elements with a valid subscript vector.
! Can't subset elements.
x Subscript can't contain `0` values.
i It has a `0` value at location 1.
Code
Expand All @@ -167,7 +167,7 @@
Output
<error/vctrs_error_subscript_type>
Error:
! Must subset elements with a valid subscript vector.
! Can't subset elements.
x Subscript can't contain negative locations.

# list_unchop() fails with complex foreign S3 classes
Expand Down Expand Up @@ -298,15 +298,15 @@
Output
<error/vctrs_error_subscript_type>
Error:
! Must subset elements with a valid subscript vector.
! Can't subset elements.
x Subscript must be numeric, not the string "x".
Code
(expect_error(list_unchop(list(1), indices = list(foobar(1L))), class = "vctrs_error_subscript_type")
)
Output
<error/vctrs_error_subscript_type>
Error:
! Must subset elements with a valid subscript vector.
! Can't subset elements.
x Subscript must be numeric, not a <vctrs_foobar> object.

# can ignore names in `list_unchop()` by providing a `zap()` name-spec (#232)
Expand Down
10 changes: 5 additions & 5 deletions tests/testthat/_snaps/slice.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
Output
<error/vctrs_error_subscript_type>
Error in `vec_slice()`:
! Must subset elements with a valid subscript vector.
! Can't subset elements with `i`.
x `i` must be logical, numeric, or character, not a <Date> object.
Code
(expect_error(vec_slice(1:3, matrix(TRUE, nrow = 1)), class = "vctrs_error_subscript_type")
)
Output
<error/vctrs_error_subscript_type>
Error in `vec_slice()`:
! Must subset elements with a valid subscript vector.
! Can't subset elements with `i`.
x Subscript `i` must be a simple vector, not a matrix.

# can't index beyond the end of a vector
Expand Down Expand Up @@ -42,7 +42,7 @@
Output
<error/vctrs_error_subscript_type>
Error:
! Must subset elements with a valid subscript vector.
! Can't subset elements with `2^31`.
x Can't convert from `2^31` <double> to <integer> due to loss of precision.

# Unnamed vector with character subscript is caught
Expand All @@ -59,7 +59,7 @@
vec_slice(1:3, -c(1L, NA))
Condition
Error in `vec_slice()`:
! Must subset elements with a valid subscript vector.
! Can't subset elements with `i`.
x Negative locations can't have missing values.
i Subscript `i` has a missing value at location 2.

Expand All @@ -69,7 +69,7 @@
vec_slice(1:3, c(-1L, 1L))
Condition
Error in `vec_slice()`:
! Must subset elements with a valid subscript vector.
! Can't subset elements with `i`.
x Negative and positive locations can't be mixed.
i Subscript `i` has a positive value at location 2.

Expand Down
Loading