Skip to content

dictionaryish(NULL) return true #1741

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

Merged
merged 4 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
15 changes: 10 additions & 5 deletions R/attr.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#' a logical vector as long as the input.
#'
#' @details
#' `is_named()` always returns `TRUE` for empty vectors because
#' `is_named()` always returns `TRUE` for empty vectors because
#'
#' @examples
#' # is_named() is a scalar predicate about the whole vector of names:
Expand Down Expand Up @@ -110,11 +110,16 @@ detect_void_name <- function(x) {
is_dictionaryish <- function(x) {
# 2022-01: Used in many packages. Don't deprecate without a
# replacement.
if (!length(x)) {
return(!is.null(x))
}
if (is.null(x)) {
TRUE
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd do this with an early return so that the rest of the function is not nested in a branch. So something like:

if (is.null(x)) {
  return(TRUE)
}
if (!length(x)) {
  return(!is.null(x))
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

} else {
if (!length(x)) {
return(!is.null(x))
}

is_named(x) && !any(duplicated(names(x)))
is_named(x) && !any(duplicated(names(x)))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove this new unnecessary whitespace please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

}
}


Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test-attr.R
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,8 @@ test_that("zap_srcref() works on calls", {
expect_null(attributes(zap_srcref(call)))
expect_true("srcref" %in% names(attributes(call)))
})

test_that("is_dictionaryish return true if is NULL", {

expect_true(is_dictionaryish(NULL))
})
Loading