lintr
lintr copied to clipboard
Reconsider `unnecessary_lambda_linter()` default behaviour
@MichaelChirico is this recommending you use `map_vec(x, foo, y = y)` instead?
We actually recommend using the anonymous function form rather than using ... to pass named arguments through to foo() now. Using the anonymous function form makes it a little easier to see which arguments belong to which function (i.e. does y belong to map_vec() or foo()?), and tends to give slightly better error messages. See the new documentation of ... at https://purrr.tidyverse.org/reference/map.html for more.
Originally posted by @DavisVaughan in https://github.com/r-lib/lintr/pull/1866#discussion_r1054319270
@DavisVaughan would you recommend going still further, and linting the opposite for purrr mappers, i.e. using ... throws a lint?
Yea I think that sounds good
Shouldn't the change be a bit more nuanced? I.e., in the lines of what I was proposing in https://github.com/r-lib/lintr/issues/1531.
The linter would still recommend the direct use of the relevant function rather than an anonymous function when no extra arguments are provided. It would switch to recommending an anonymous function when users want to pass extra arguments.
For example, it would recommend:
map_vec(x, sum)
over
map_vec(x, \(i) sum(i))
but still recommend
map_vec(x, \(i) sum(i, na.rm = TRUE))
Not sure what the official tidyverse position is here :thinking:
IIUC, that is what @MichaelChirico is suggesting
i.e. using ... throws a lint?
^ to me this implies that map_vec(x, sum) is fine but map_vec(x, sum, na.rm = TRUE) throws a lint because of the na.rm
I was reacting to the title since the change we're discussing would not result in the complete exclusion of purrr mappers from unnecessary_lambda_linter but in their handling in a subset of cases.
From https://github.com/r-lib/lintr/issues/2421:
Just want to flag pre 3.1.1 release that there is a "small" benefit to using lambdas (particularly in a more interactive/scripting setting) in terms of the error message displayed
testy <- function(x) lapply(x, sum) testy_lambda <- function(x) lapply(x, \(x) sum(x)) testy("bob") #> Error in FUN(X[[i]], ...): invalid 'type' (character) of argument testy_lambda("bob") #> Error in sum(x): invalid 'type' (character) of argumentOnly small and still slightly unhelpful due to
lapplynot being referenced but I wonder if it at least warrants discussion in the "details" section of the docs.