From 722271c8ac3b98400352d2dfaf27fc424736c552 Mon Sep 17 00:00:00 2001 From: Mike Du <58779940+ilovemane@users.noreply.github.com> Date: Thu, 15 Aug 2024 19:46:02 +0100 Subject: [PATCH 1/4] update --- R/attr.R | 9 ++++++--- tests/testthat/test-attr.R | 5 +++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/R/attr.R b/R/attr.R index 04e3bf10d..658b03925 100644 --- a/R/attr.R +++ b/R/attr.R @@ -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: @@ -113,8 +113,11 @@ is_dictionaryish <- function(x) { if (!length(x)) { return(!is.null(x)) } - - is_named(x) && !any(duplicated(names(x))) + if (is.null(names(x))) { + TRUE + } else { + is_named(x) && !any(duplicated(names(x))) + } } diff --git a/tests/testthat/test-attr.R b/tests/testthat/test-attr.R index 79e00b755..48a318abd 100644 --- a/tests/testthat/test-attr.R +++ b/tests/testthat/test-attr.R @@ -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 names(x) is NULL", { + x <- "x" + expect_true(is_dictionaryish(x)) +}) From 8a55490f455dbfe8c59ab3427e0a30af2c74b7f6 Mon Sep 17 00:00:00 2001 From: Mike Du <58779940+ilovemane@users.noreply.github.com> Date: Thu, 15 Aug 2024 20:53:31 +0100 Subject: [PATCH 2/4] fixes --- R/attr.R | 10 ++++++---- tests/testthat/test-attr.R | 6 +++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/R/attr.R b/R/attr.R index 658b03925..d605ed754 100644 --- a/R/attr.R +++ b/R/attr.R @@ -110,13 +110,15 @@ 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(names(x))) { + if (is.null(x)) { TRUE } else { + if (!length(x)) { + return(!is.null(x)) + } + is_named(x) && !any(duplicated(names(x))) + } } diff --git a/tests/testthat/test-attr.R b/tests/testthat/test-attr.R index 48a318abd..cd5496e31 100644 --- a/tests/testthat/test-attr.R +++ b/tests/testthat/test-attr.R @@ -187,7 +187,7 @@ test_that("zap_srcref() works on calls", { expect_true("srcref" %in% names(attributes(call))) }) -test_that("is_dictionaryish return true if names(x) is NULL", { - x <- "x" - expect_true(is_dictionaryish(x)) +test_that("is_dictionaryish return true if is NULL", { + + expect_true(is_dictionaryish(NULL)) }) From cde98bc2d4d94295d4d9ab81188913860d52fd8b Mon Sep 17 00:00:00 2001 From: Mike Du <58779940+ilovemane@users.noreply.github.com> Date: Thu, 15 Aug 2024 22:16:16 +0100 Subject: [PATCH 3/4] Update attr.R --- R/attr.R | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/R/attr.R b/R/attr.R index d605ed754..f3b42290e 100644 --- a/R/attr.R +++ b/R/attr.R @@ -111,18 +111,18 @@ is_dictionaryish <- function(x) { # 2022-01: Used in many packages. Don't deprecate without a # replacement. if (is.null(x)) { - TRUE - } else { - if (!length(x)) { - return(!is.null(x)) - } - - is_named(x) && !any(duplicated(names(x))) + return(TRUE) + } + if (!length(x)) { + return(!is.null(x)) } + + is_named(x) && !any(duplicated(names(x))) } + #' Does an object have an element with this name? #' #' This function returns a logical value that indicates if a data From c2d177840f1fb6f47af85ee5177dd947f3a722c3 Mon Sep 17 00:00:00 2001 From: Mike Du <58779940+ilovemane@users.noreply.github.com> Date: Thu, 15 Aug 2024 22:49:42 +0100 Subject: [PATCH 4/4] Update NEWS.md --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index d9e0aabae..af45d9517 100644 --- a/NEWS.md +++ b/NEWS.md @@ -6,6 +6,8 @@ * `env_unlock()` is now defunct because recent versions of R no long make it possible to unlock an environment (#1705). Make sure to use an up-to-date version of pkgload (>= 1.4.0) following this change. + +* `is_dictionaryish()` now will return TRUE for NULL (@ilovemane, #1712). # rlang 1.1.4