vctrs icon indicating copy to clipboard operation
vctrs copied to clipboard

Can we improve on the hardcoded `arg` name in this error?

Open DavisVaughan opened this issue 3 years ago • 5 comments

vctrs::vec_cast(1, "x", x_arg = 1)
#> Error in `vctrs::vec_cast()`:
#> ! `arg` must be a string.

Ideally it would reference x_arg here.

This happens because arg is hardcoded here: https://github.com/r-lib/vctrs/blob/79f5318b886a8316b594477a84d1dd8ab3373ece/src/arg.c#L136

But a lot of times (*data).x is a symbol with the right arg name, so maybe we can check to see if that is a symbol, and if so use r_sym_c_string() to turn it into a name we can use in the error?

DavisVaughan avatar Jun 30 '22 18:06 DavisVaughan

We should use x_arg = caller_arg(x) and thread that down on the C side using a lazy vctrs-arg.

lionel- avatar Jul 01 '22 05:07 lionel-

oh the problem was that caller_arg() may create non-string args. We should convert these to string with as_label().

lionel- avatar Oct 03 '22 10:10 lionel-

@lionel- I'm not sure that #1711 actually closes this issue.

This is purely about the fact that we should reference the correct argument name, x_arg, in the error message when x_arg itself is validated.

DavisVaughan avatar Oct 03 '22 18:10 DavisVaughan

Oh wait, you think that 1 should be an allowed value for x_arg?? That seems so strange to me 😆 I was expecting that it would be something that passes check_string(), like any other place we'd probably validate a string-like argument.

> vctrs::vec_cast(1, "x", x_arg = 1)
Error:
! Can't convert `1` <double> to <character>.

In other words, I thought the current check was right, I just thought the error should reference x_arg not arg.

DavisVaughan avatar Oct 03 '22 18:10 DavisVaughan

yup we allow literals and any kind of R objects in error args. This way, checking functions do not fail when used interactively. Also it's useful to allow calls for some use cases, such as tibble errors in subset operators. Since operators like [[ don't have argument names, we show the user input instead.

lionel- avatar Oct 04 '22 06:10 lionel-