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

Lexically-scoped verbosity #74

Open
lionel- opened this issue Jan 5, 2021 · 2 comments
Open

Lexically-scoped verbosity #74

lionel- opened this issue Jan 5, 2021 · 2 comments
Labels
feature a feature request or enhancement

Comments

@lionel-
Copy link
Member

lionel- commented Jan 5, 2021

The behaviour for soft-deprecation verbosity in unit tests was originally intented to depend on whether the soft-deprecated function is called from the tested package or downstack:

  • If called from the tested package, the default verbosity is set to "warning"
  • If called from a dependency, it is set to "quiet"

This conditional verbosity is implemented in deprecate_soft() via the envvar TESTTHAT_PKG which is set to the package being tested. Unfortunately, this default stopped working correctly with testthat 3e because it hardcodes verbosity to warning. The problem is that verbosity is currently set via a global option whereas we really want some sort of lexical scoping instead.

I think it's a good idea to let testthat set the lifecycle verbosity, but we need a lexically scoped mechanism. I see two options. The simplest is to allow defining verbosity with a local binding:

local_bindings(
  .lifecycle_verbosity = "warning"
)

This is not practical when the deprecation is signalled from helpers though, because the calling environment of these helpers will not have that binding in scope. What we really want is to set verbosity for the whole namespace. So we could provide a custom tool for this:

lifecycle::local_verbosity(
  rlang = "warning",
  purrr = "warning"
)

testthat would use this to enable warnings for the package being tested instead of setting the global option.

We could also frame the behaviour with the global environment in terms of this mechanism. You could disable this behaviour like this:

lifecycle::poke_verbosity(global = NULL)

This would be less risky than using the global options(lifecycle_verbosity = "quiet") which silences all warnings without distinction.

What do you think @hadley?

@hadley
Copy link
Member

hadley commented Jan 7, 2021

I like the lifecycle::local_verbosity() approach best

@lionel-
Copy link
Member Author

lionel- commented Jan 11, 2021

One issue with this approach is that snippets run from test files directly in the REPL will be scoped from global rather than the package. So direct evaluation and devtools::test() might produce different results.

A possible workaround would be for pkgload to record the last package that was loaded and treat the top env of the global env to be the namespace of that package.

@hadley hadley added the feature a feature request or enhancement label Nov 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants