Skip to content

Commit 16ec884

Browse files
authored
Merge pull request #270 from tidymodels/update-type-checkers
Type checkers refresh
2 parents 24cae41 + dfbdf9a commit 16ec884

File tree

8 files changed

+52
-32
lines changed

8 files changed

+52
-32
lines changed

R/import-standalone-obj-type.R

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
# Standalone file: do not edit by hand
2-
# Source: <https://github.com/r-lib/rlang/blob/main/R/standalone-obj-type.R>
2+
# Source: https://github.com/r-lib/rlang/blob/HEAD/R/standalone-obj-type.R
3+
# Generated by: usethis::use_standalone("r-lib/rlang", "obj-type")
34
# ----------------------------------------------------------------------
45
#
56
# ---
67
# repo: r-lib/rlang
78
# file: standalone-obj-type.R
8-
# last-updated: 2022-10-04
9+
# last-updated: 2024-02-14
910
# license: https://unlicense.org
1011
# imports: rlang (>= 1.1.0)
1112
# ---
1213
#
1314
# ## Changelog
1415
#
16+
# 2024-02-14:
17+
# - `obj_type_friendly()` now works for S7 objects.
18+
#
19+
# 2023-05-01:
20+
# - `obj_type_friendly()` now only displays the first class of S3 objects.
21+
#
22+
# 2023-03-30:
23+
# - `stop_input_type()` now handles `I()` input literally in `arg`.
24+
#
1525
# 2022-10-04:
1626
# - `obj_type_friendly(value = TRUE)` now shows numeric scalars
1727
# literally.
@@ -65,7 +75,7 @@ obj_type_friendly <- function(x, value = TRUE) {
6575
if (inherits(x, "quosure")) {
6676
type <- "quosure"
6777
} else {
68-
type <- paste(class(x), collapse = "/")
78+
type <- class(x)[[1L]]
6979
}
7080
return(sprintf("a <%s> object", type))
7181
}
@@ -261,19 +271,19 @@ vec_type_friendly <- function(x, length = FALSE) {
261271
#' Return OO type
262272
#' @param x Any R object.
263273
#' @return One of `"bare"` (for non-OO objects), `"S3"`, `"S4"`,
264-
#' `"R6"`, or `"R7"`.
274+
#' `"R6"`, or `"S7"`.
265275
#' @noRd
266276
obj_type_oo <- function(x) {
267277
if (!is.object(x)) {
268278
return("bare")
269279
}
270280

271-
class <- inherits(x, c("R6", "R7_object"), which = TRUE)
281+
class <- inherits(x, c("R6", "S7_object"), which = TRUE)
272282

273283
if (class[[1]]) {
274284
"R6"
275285
} else if (class[[2]]) {
276-
"R7"
286+
"S7"
277287
} else if (isS4(x)) {
278288
"S4"
279289
} else {
@@ -315,10 +325,15 @@ stop_input_type <- function(x,
315325
if (length(what)) {
316326
what <- oxford_comma(what)
317327
}
328+
if (inherits(arg, "AsIs")) {
329+
format_arg <- identity
330+
} else {
331+
format_arg <- cli$format_arg
332+
}
318333

319334
message <- sprintf(
320335
"%s must be %s, not %s.",
321-
cli$format_arg(arg),
336+
format_arg(arg),
322337
what,
323338
obj_type_friendly(x, value = show_value)
324339
)

R/import-standalone-types-check.R

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Standalone file: do not edit by hand
2-
# Source: <https://github.com/r-lib/rlang/blob/main/R/standalone-types-check.R>
2+
# Source: https://github.com/r-lib/rlang/blob/HEAD/R/standalone-types-check.R
3+
# Generated by: usethis::use_standalone("r-lib/rlang", "types-check")
34
# ----------------------------------------------------------------------
45
#
56
# ---
@@ -13,6 +14,9 @@
1314
#
1415
# ## Changelog
1516
#
17+
# 2024-08-15:
18+
# - `check_character()` gains an `allow_na` argument (@martaalcalde, #1724)
19+
#
1620
# 2023-03-13:
1721
# - Improved error messages of number checkers (@teunbrand)
1822
# - Added `allow_infinite` argument to `check_number_whole()` (@mgirlich).
@@ -461,15 +465,28 @@ check_formula <- function(x,
461465

462466
# Vectors -----------------------------------------------------------------
463467

468+
# TODO: Figure out what to do with logical `NA` and `allow_na = TRUE`
469+
464470
check_character <- function(x,
465471
...,
472+
allow_na = TRUE,
466473
allow_null = FALSE,
467474
arg = caller_arg(x),
468475
call = caller_env()) {
476+
469477
if (!missing(x)) {
470478
if (is_character(x)) {
479+
if (!allow_na && any(is.na(x))) {
480+
abort(
481+
sprintf("`%s` can't contain NA values.", arg),
482+
arg = arg,
483+
call = call
484+
)
485+
}
486+
471487
return(invisible(NULL))
472488
}
489+
473490
if (allow_null && is_null(x)) {
474491
return(invisible(NULL))
475492
}
@@ -479,7 +496,6 @@ check_character <- function(x,
479496
x,
480497
"a character vector",
481498
...,
482-
allow_na = FALSE,
483499
allow_null = allow_null,
484500
arg = arg,
485501
call = call

R/table.R

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,7 @@ weighted_table <- function(..., weights, na_remove = FALSE) {
119119
weights <- vec_cast(weights, to = double())
120120
vec_assert(weights, size = size)
121121

122-
if (!is_bool(na_remove)) {
123-
abort("`na_remove` must be a single `TRUE` or `FALSE`.")
124-
}
122+
check_bool(na_remove)
125123

126124
tapply(
127125
X = weights,

R/tune.R

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,7 @@
2525
#' step_normalize(all_numeric_predictors()) %>%
2626
#' step_pca(all_numeric_predictors, num_comp = tune())
2727
tune <- function(id = "") {
28-
if (!is.character(id) || length(id) != 1) {
29-
abort("The `id` should be a single character string.")
30-
}
31-
if (is.na(id)) {
32-
abort("The `id` can't be missing.")
33-
}
28+
check_string(id)
3429

3530
if (id == "") {
3631
call("tune")

R/use.R

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ create_modeling_package <- function(path,
5959
check_installed("withr")
6060

6161
# Avoid creating files if a bad model is supplied
62-
if (!is_string(model)) {
63-
abort("`model` must be a single string.")
64-
}
62+
check_string(model)
6563

6664
if (has_spaces(model)) {
6765
abort("`model` must not contain any spaces.")
@@ -126,10 +124,8 @@ use_modeling_files <- function(model) {
126124
use_modeling_files_impl <- function(model, prompt_document = TRUE) {
127125
check_installed("usethis")
128126

129-
if (!is_string(model)) {
130-
abort("`model` must be a string.")
131-
}
132-
127+
check_string(model)
128+
133129
if (has_spaces(model)) {
134130
abort("`model` must not contain any spaces.")
135131
}

tests/testthat/_snaps/table.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
weighted_table(x, y, weights = w, na_remove = c(TRUE, FALSE))
55
Condition
66
Error in `weighted_table()`:
7-
! `na_remove` must be a single `TRUE` or `FALSE`.
7+
! `na_remove` must be `TRUE` or `FALSE`, not a logical vector.
88

99
---
1010

1111
Code
1212
weighted_table(x, y, weights = w, na_remove = 1)
1313
Condition
1414
Error in `weighted_table()`:
15-
! `na_remove` must be a single `TRUE` or `FALSE`.
15+
! `na_remove` must be `TRUE` or `FALSE`, not the number 1.
1616

1717
# requires at least one `...`
1818

tests/testthat/_snaps/tune.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@
44
tune(1)
55
Condition
66
Error in `tune()`:
7-
! The `id` should be a single character string.
7+
! `id` must be a single string, not the number 1.
88

99
---
1010

1111
Code
1212
tune(c("x", "y"))
1313
Condition
1414
Error in `tune()`:
15-
! The `id` should be a single character string.
15+
! `id` must be a single string, not a character vector.
1616

1717
---
1818

1919
Code
2020
tune(NA_character_)
2121
Condition
2222
Error in `tune()`:
23-
! The `id` can't be missing.
23+
! `id` must be a single string, not a character `NA`.
2424

tests/testthat/_snaps/use.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@
2020
create_modeling_package(path = "my/path", model = c("model1", "model2"))
2121
Condition
2222
Error in `create_modeling_package()`:
23-
! `model` must be a single string.
23+
! `model` must be a single string, not a character vector.
2424

2525
---
2626

2727
Code
2828
create_modeling_package(path = "my/path", model = 1)
2929
Condition
3030
Error in `create_modeling_package()`:
31-
! `model` must be a single string.
31+
! `model` must be a single string, not the number 1.
3232

3333
---
3434

0 commit comments

Comments
 (0)