Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved input validation #1407

Merged
merged 26 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
ad19eb1
Add rlang check_ helpers
hadley Oct 18, 2022
56fe3e6
Improve chop/unchop input validation
hadley Oct 18, 2022
baf48ef
Improve complete input validation
hadley Oct 18, 2022
610999c
Improve extract() input validation
hadley Oct 18, 2022
7b8258d
Tweak fill input validation
hadley Oct 18, 2022
b4e9713
Add missing snapshot
hadley Oct 18, 2022
c681020
Improve hoist input validation
hadley Oct 18, 2022
8e2109a
Fix bug in check_data_frame()
hadley Oct 18, 2022
03fa1fd
Improve input validation for nest, pack, and unpack
hadley Oct 18, 2022
6f19821
Use check helpers in check_pivot_spec
hadley Oct 18, 2022
42e176c
Polish replace_na argument validation
hadley Oct 18, 2022
cffa70d
Improve separate_rows input validation
hadley Oct 18, 2022
ed74466
Improve separate() input validation
hadley Oct 18, 2022
e22ac9d
Add input validation to full_seq()
hadley Oct 18, 2022
58e0bf9
Improve uncount() input validation
hadley Oct 18, 2022
937ff71
Combine full_seq() tests
hadley Oct 18, 2022
b26fd2a
Improve unite() input validation
hadley Oct 18, 2022
665a593
Improve input validation for unnest_longer()
hadley Oct 18, 2022
f214d61
Improve unnest_wider() input validation
hadley Oct 18, 2022
d489200
Add cli
hadley Oct 18, 2022
b5646ca
Polish chop errors
hadley Oct 18, 2022
8239b37
Polish complete errors
hadley Oct 18, 2022
2952738
Minor polishing
hadley Oct 18, 2022
680549b
Remove last use of stop()
hadley Oct 18, 2022
8a5524e
Add missing default
hadley Oct 18, 2022
1e64914
Final replacements for abort() calls
hadley Oct 18, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ BugReports: https://github.com/tidyverse/tidyr/issues
Depends:
R (>= 3.1)
Imports:
cli (>= 3.4.1),
dplyr (>= 1.0.10),
glue,
lifecycle (>= 1.0.3),
Expand Down Expand Up @@ -51,5 +52,5 @@ LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.1
SystemRequirements: C++11
Remotes:
Remotes:
r-lib/vctrs
2 changes: 1 addition & 1 deletion R/append.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ append_df <- function(x, y, after = length(x), remove = FALSE) {
if (is.character(after)) {
after <- match(after, dplyr::tbl_vars(x))
} else if (!is.integer(after)) {
stop("`after` must be character or integer", call. = FALSE)
cli::cli_abort("{.arg after} must be character or integer", .internal = TRUE)
}

# Replace duplicated variables
Expand Down
24 changes: 14 additions & 10 deletions R/chop.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
#' df %>% unchop(y)
#' df %>% unchop(y, keep_empty = TRUE)
chop <- function(data, cols) {
check_data_frame(data)
check_required(cols)
cols <- tidyselect::eval_select(enquo(cols), data, allow_rename = FALSE)

Expand Down Expand Up @@ -98,6 +99,10 @@ col_chop <- function(x, indices) {
#' @export
#' @rdname chop
unchop <- function(data, cols, keep_empty = FALSE, ptype = NULL) {
check_data_frame(data)
check_required(cols)
check_bool(keep_empty)

sel <- tidyselect::eval_select(enquo(cols), data)

size <- vec_size(data)
Expand Down Expand Up @@ -143,14 +148,7 @@ unchop <- function(data, cols, keep_empty = FALSE, ptype = NULL) {
df_unchop <- function(x, ..., ptype = NULL, keep_empty = FALSE, error_call = caller_env()) {
check_dots_empty()

if (!is.data.frame(x)) {
abort("`x` must be a data frame.", call = error_call)
}
if (!is_bool(keep_empty)) {
abort("`keep_empty` must be a single `TRUE` or `FALSE`.", call = error_call)
}

ptype <- check_list_of_ptypes(ptype, names = names(x), arg = "ptype", call = error_call)
ptype <- check_list_of_ptypes(ptype, names = names(x), call = error_call)

size <- vec_size(x)

Expand Down Expand Up @@ -245,7 +243,10 @@ df_unchop <- function(x, ..., ptype = NULL, keep_empty = FALSE, error_call = cal
# - `col` was an empty list(), or a list of all `NULL`s.
# - No ptype was specified for `col`, either by the user or by a list-of.
if (out_size != 0L) {
abort("Internal error: `NULL` column generated, but output size is not `0`.")
cli::cli_abort(
"`NULL` column generated, but output size is not `0`.",
.internal = TRUE
)
}

col <- unspecified(0L)
Expand Down Expand Up @@ -285,7 +286,10 @@ unchop_sizes2 <- function(x, y, error_call) {
row <- which(incompatible)[[1]]
x <- x[[row]]
y <- y[[row]]
abort(glue("In row {row}, can't recycle input of size {x} to size {y}."), call = error_call)
cli::cli_abort(
"In row {row}, can't recycle input of size {x} to size {y}.",
call = error_call
)
}

x
Expand Down
Loading