Skip to content

Commit d1afb47

Browse files
authored
Use "can't" phrasing in headers of subscript errors (#1736)
1 parent b15c99a commit d1afb47

File tree

7 files changed

+125
-117
lines changed

7 files changed

+125
-117
lines changed

R/subscript.R

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,20 @@ new_error_subscript_type <- function(i,
175175

176176
#' @export
177177
cnd_header.vctrs_error_subscript_type <- function(cnd) {
178-
action <- cnd_subscript_action(cnd)
178+
arg <- cnd[["subscript_arg"]]
179+
if (is_subscript_arg(arg)) {
180+
with <- glue::glue(" with {format_subscript_arg(arg)}")
181+
} else {
182+
with <- ""
183+
}
184+
185+
action <- cnd_subscript_action(cnd, assign_to = FALSE)
179186
elt <- cnd_subscript_element(cnd)
187+
180188
if (cnd_subscript_scalar(cnd)) {
181-
glue::glue("Must {action} {elt[[1]]} with a single valid subscript.")
189+
glue::glue("Can't {action} {elt[[1]]}{with}.")
182190
} else {
183-
glue::glue("Must {action} {elt[[2]]} with a valid subscript vector.")
191+
glue::glue("Can't {action} {elt[[2]]}{with}.")
184192
}
185193
}
186194
#' @export

tests/testthat/_snaps/error-call.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@
218218
Output
219219
<error/vctrs_error_subscript_type>
220220
Error in `my_function()`:
221-
! Must subset elements with a valid subscript vector.
221+
! Can't subset elements with `my_arg`.
222222
x Can't convert from `my_arg` <double> to <integer> due to loss of precision.
223223

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

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

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

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

332332
# list_sizes() reports error context

tests/testthat/_snaps/slice-assign.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
Output
1717
<error/vctrs_error_subscript_size>
1818
Error:
19-
! Must assign to elements with a valid subscript vector.
19+
! Can't assign elements.
2020
x Logical subscript must be size 1 or 2, not 3.
2121

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

3333
# must assign existing elements
@@ -72,7 +72,7 @@
7272
Output
7373
<error/vctrs_error_subscript_type>
7474
Error:
75-
! Must assign to elements with a valid subscript vector.
75+
! Can't assign elements.
7676
x Negative and positive locations can't be mixed.
7777
i Subscript has a positive value at location 2.
7878
Code
@@ -81,7 +81,7 @@
8181
Output
8282
<error/vctrs_error_subscript_type>
8383
Error:
84-
! Must assign to elements with a valid subscript vector.
84+
! Can't assign elements.
8585
x Negative locations can't have missing values.
8686
i Subscript has a missing value at location 2.
8787

tests/testthat/_snaps/slice-chop.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@
158158
Output
159159
<error/vctrs_error_subscript_type>
160160
Error:
161-
! Must subset elements with a valid subscript vector.
161+
! Can't subset elements.
162162
x Subscript can't contain `0` values.
163163
i It has a `0` value at location 1.
164164
Code
@@ -167,7 +167,7 @@
167167
Output
168168
<error/vctrs_error_subscript_type>
169169
Error:
170-
! Must subset elements with a valid subscript vector.
170+
! Can't subset elements.
171171
x Subscript can't contain negative locations.
172172

173173
# list_unchop() fails with complex foreign S3 classes
@@ -298,15 +298,15 @@
298298
Output
299299
<error/vctrs_error_subscript_type>
300300
Error:
301-
! Must subset elements with a valid subscript vector.
301+
! Can't subset elements.
302302
x Subscript must be numeric, not the string "x".
303303
Code
304304
(expect_error(list_unchop(list(1), indices = list(foobar(1L))), class = "vctrs_error_subscript_type")
305305
)
306306
Output
307307
<error/vctrs_error_subscript_type>
308308
Error:
309-
! Must subset elements with a valid subscript vector.
309+
! Can't subset elements.
310310
x Subscript must be numeric, not a <vctrs_foobar> object.
311311

312312
# can ignore names in `list_unchop()` by providing a `zap()` name-spec (#232)

tests/testthat/_snaps/slice.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
Output
66
<error/vctrs_error_subscript_type>
77
Error in `vec_slice()`:
8-
! Must subset elements with a valid subscript vector.
8+
! Can't subset elements with `i`.
99
x `i` must be logical, numeric, or character, not a <Date> object.
1010
Code
1111
(expect_error(vec_slice(1:3, matrix(TRUE, nrow = 1)), class = "vctrs_error_subscript_type")
1212
)
1313
Output
1414
<error/vctrs_error_subscript_type>
1515
Error in `vec_slice()`:
16-
! Must subset elements with a valid subscript vector.
16+
! Can't subset elements with `i`.
1717
x Subscript `i` must be a simple vector, not a matrix.
1818

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

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

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

0 commit comments

Comments
 (0)