mllg / checkmate

Fast and versatile argument checks
https://mllg.github.io/checkmate/
Other
261 stars 30 forks source link

Fixes `matchArg` var name problem and retrieves `choices` from formals when missing #252

Open averissimo opened 9 months ago

averissimo commented 9 months ago

Closes #251

Changes description

Example (when on PR branch)

šŸ”Ž note double problem in error message:

f1 <- function(x = c("aa", "bb", "cc")) {
  checkmate::matchArg(x, choices = eval(formals(f1)[["x"]]))
}
f1("dd")
#> Error in checkmate::matchArg(x, choices = eval(formals(f1)[["x"]])):
#> Assertion on 'character(0)' failed: Must be element of set {'aa','bb','cc'}, but is not atomic scalar.

checkmate::matchArg("xx", choices = c("pearson", "kendall", "spearman"))
#> Error in checkmate::matchArg("xx", choices = c("pearson", "kendall", "spearman")):
#> Assertion on 'character(0)' failed: Must be element of set {'pearson','kendall','spearman'}, but is not atomic scalar.

šŸŸ¢ Corrected

pkgload::load_all("/path/to/checkmate/with/branch/from/pr")
#> ā„¹ Loading checkmate
f1("dd")
#> Error in f1("dd"):
#> Assertion on 'x' failed: Must be element of set {'aa','bb','cc'}, but is 'dd'.

checkmate::matchArg("xx", choices = c("pearson", "kendall", "spearman"))
#> Error in eval(expr, envir, enclos):
#> Assertion on '"xx"' failed: Must be element of set {'pearson','kendall','spearman'}, but is 'xx'.

šŸŸ¢ With missing choices

f2 <- function(x = c("aa", "bb", "cc")) {
  res <- checkmate::matchArg(x)
  res
}

f2("dd")
#> Error in f2("dd"):
#> Assertion on 'x' failed: Must be element of set {'aa','bb','cc'}, but is 'dd'.

f2("aa")
#> [1] "aa"

Created on 2024-02-28 with reprex v2.0.2