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

No longer require string what/with #84

Merged
merged 3 commits into from Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
@@ -1,5 +1,7 @@
# lifecycle (development version)

* The `what` and `with` arguments no longer need to be quoted with `""`.

* A character vector `details` is now converted into a bulleted list (#55).

* You can now deprecate an argument with `foo(arg)` instead of `foo(arg =)` (#78).
Expand Down
26 changes: 16 additions & 10 deletions R/deprecated.R
Expand Up @@ -26,10 +26,10 @@
#' @param when A string giving the version when the behaviour was deprecated.
#' @param what A string describing what is deprecated:
#'
#' * Deprecate a whole function with `"foo()"`.
#' * Deprecate an argument with `"foo(arg)"`.
#' * Deprecate a whole function with `foo()`.
#' * Deprecate an argument with `foo(arg)`.
#' * Partially deprecate an argument with
#' `"foo(arg = 'must be a scalar integer')"`.
#' `foo(arg = 'must be a scalar integer')`.
#'
#' You can optionally supply the namespace: `"ns::foo()"`, but this is
#' usually not needed as it will be inferred from the caller environment.
Expand Down Expand Up @@ -58,32 +58,32 @@
#'
#' @examples
#' # A deprecated function `foo`:
#' deprecate_warn("1.0.0", "foo()")
#' deprecate_warn("1.0.0", foo())
#'
#' # A deprecated argument `arg`:
#' deprecate_warn("1.0.0", "foo(arg = )")
#' deprecate_warn("1.0.0", foo(arg))
#'
#' # A partially deprecated argument `arg`:
#' deprecate_warn("1.0.0", "foo(arg = 'must be a scalar integer')")
#' deprecate_warn("1.0.0", foo(arg = 'must be a scalar integer'))
#'
#' # A deprecated function with a function replacement:
#' deprecate_warn("1.0.0", "foo()", "bar()")
#' deprecate_warn("1.0.0", foo(), bar())
#'
#' # A deprecated function with a function replacement from a
#' # different package:
#' deprecate_warn("1.0.0", "foo()", "otherpackage::bar()")
#' deprecate_warn("1.0.0", foo(), "otherpackage::bar()")
hadley marked this conversation as resolved.
Show resolved Hide resolved
#'
#' # A deprecated function with custom message:
#' deprecate_warn(
#' when = "1.0.0",
#' "foo()",
#' what = foo(),
#' details = "Please use `otherpackage::bar(foo = TRUE)` instead"
#' )
#'
#' # A deprecated function with custom bulleted list:
#' deprecate_warn(
#' when = "1.0.0",
#' "foo()",
#' what = foo(),
#' details = c(
#' x = "This is dangerous",
#' i = "Did you mean `safe_foo()` instead?"
Expand All @@ -96,6 +96,8 @@ deprecate_soft <- function(when,
details = NULL,
id = NULL,
env = caller_env(2)) {
what <- substitute(what)
with <- substitute(with)
msg <- lifecycle_message(when, what, with, details, env, "deprecate_soft")

verbosity <- lifecycle_verbosity()
Expand All @@ -121,6 +123,8 @@ deprecate_warn <- function(when,
details = NULL,
id = NULL,
env = caller_env(2)) {
what <- substitute(what)
with <- substitute(with)
msg <- lifecycle_message(when, what, with, details, env, "deprecate_warn")

verbosity <- lifecycle_verbosity()
Expand Down Expand Up @@ -157,6 +161,8 @@ deprecate_stop <- function(when,
what,
with = NULL,
details = NULL) {
what <- substitute(what)
with <- substitute(with)
msg <- lifecycle_message(when, what, with, details, env, "deprecate_stop")
deprecate_stop0(msg)
}
Expand Down
10 changes: 6 additions & 4 deletions R/spec.R
Expand Up @@ -21,12 +21,14 @@ spec <- function(spec, env = caller_env(), signaller = "signal_lifecycle") {
}

spec_what <- function(what, arg, signaller) {
if (!is_string(what)) {
lifecycle_abort("`what` must be a string")
if (is_call(what)) {
call <- what
} else if (is_string(what)) {
call <- parse_expr(what)
hadley marked this conversation as resolved.
Show resolved Hide resolved
} else {
lifecycle_abort("`what` must be a string or call")
hadley marked this conversation as resolved.
Show resolved Hide resolved
}

call <- parse_expr(what)

if (!is_call(call)) {
what <- as_string(what)
lifecycle_abort(
Expand Down
20 changes: 10 additions & 10 deletions man/deprecate_soft.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/testthat/_snaps/spec.md
Expand Up @@ -3,7 +3,7 @@
Code
spec(1)
Error <rlang_error>
Internal error in lifecycle: `what` must be a string
Internal error in lifecycle: `what` must be a string or call

---

Expand Down
33 changes: 20 additions & 13 deletions tests/testthat/test-deprecated.R
Expand Up @@ -4,35 +4,35 @@ test_that("default deprecations behave as expected", {
on.exit(env_unbind(deprecation_env, "test"))
local_options(lifecycle_verbosity = "default")

expect_condition(deprecate_soft("1.0.0", "foo()"), class = "lifecycle_soft_deprecated")
expect_condition(deprecate_soft("1.0.0", foo()), class = "lifecycle_soft_deprecated")
expect_warning(deprecate_warn("1.0.0", "foo()", id = "test"), class = "lifecycle_warning_deprecated")
hadley marked this conversation as resolved.
Show resolved Hide resolved
expect_warning(deprecate_warn("1.0.0", "foo()", id = "test"), NA)
expect_defunct(deprecate_stop("1.0.0", "foo()"))
expect_warning(deprecate_warn("1.0.0", foo(), id = "test"), NA)
expect_defunct(deprecate_stop("1.0.0", foo()))
})

test_that("quiet suppresses _soft and _warn", {
local_options(lifecycle_verbosity = "quiet")

expect_warning(deprecate_soft("1.0.0", "foo()"), NA)
expect_warning(deprecate_warn("1.0.0", "foo()"), NA)
expect_defunct(deprecate_stop("1.0.0", "foo()"))
expect_warning(deprecate_soft("1.0.0", foo()), NA)
expect_warning(deprecate_warn("1.0.0", foo()), NA)
expect_defunct(deprecate_stop("1.0.0", foo()))
})

test_that("warning always warns in _soft and _warn", {

local_options(lifecycle_verbosity = "warning")

expect_deprecated(deprecate_soft("1.0.0", "foo()"))
expect_deprecated(deprecate_warn("1.0.0", "foo()"))
expect_defunct(deprecate_stop("1.0.0", "foo()"))
expect_deprecated(deprecate_soft("1.0.0", foo()))
expect_deprecated(deprecate_warn("1.0.0", foo()))
expect_defunct(deprecate_stop("1.0.0", foo()))
})

test_that("error coverts _soft and _warn to errors", {
local_options(lifecycle_verbosity = "error")

expect_defunct(deprecate_soft("1.0.0", "foo()"))
expect_defunct(deprecate_warn("1.0.0", "foo()"))
expect_defunct(deprecate_stop("1.0.0", "foo()"))
expect_defunct(deprecate_soft("1.0.0", foo()))
expect_defunct(deprecate_warn("1.0.0", foo()))
expect_defunct(deprecate_stop("1.0.0", foo()))
})

test_that("warning conditions are signaled only once if warnings are suppressed", {
Expand All @@ -41,12 +41,19 @@ test_that("warning conditions are signaled only once if warnings are suppressed"
x <- 0L
suppressWarnings(withCallingHandlers(
warning = function(...) x <<- x + 1L,
deprecate_warn("1.0.0", "foo()")
deprecate_warn("1.0.0", foo())
))

expect_identical(x, 1L)
})

test_that("can use quoted or unquoted what/with", {
cnd1 <- catch_cnd(deprecate_warn("1.0.0", foo()))
cnd2 <- catch_cnd(deprecate_warn("1.0.0", "foo()"))

expect_equal(cnd1, cnd2)
})

# messaging ---------------------------------------------------------------

test_that("what deprecation messages are readable", {
Expand Down