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

Additional check functions #86

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open

Additional check functions #86

wants to merge 10 commits into from

Conversation

topepo
Copy link
Member

@topepo topepo commented Oct 20, 2024

The rlang type checkers lack a few functions. This PR has three: one for a single vector and two to check numeric vectors.

R/checks.R Outdated Show resolved Hide resolved
cl <- match.call()
arg <- as.character(cl$x)

for (i in x) {
Copy link
Member Author

Choose a reason for hiding this comment

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

The downside here is that we only catch one violation at a time.

Choose a reason for hiding this comment

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

I would imagine there's a pretty steep performance penalty here, too. This is the same kind of performance issue that led to tidymodels/parsnip#835.

Maybe an error could be conditional on rlang::is_integerish()?

Copy link
Member Author

Choose a reason for hiding this comment

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

Is it steep for checking arguments (not data)? We need some sort of (short length) numeric vector checks with min/max/na/null options.

R/checks.R Show resolved Hide resolved
)
}

# check_logical <- function(x,
Copy link
Member Author

Choose a reason for hiding this comment

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

brulee has its own check_logical(). The is temporary to avoid bringing in a lot of snapshots into this PR.

expect_silent(brulee:::check_number_whole_vec(variable))
expect_silent(brulee:::check_number_whole_vec(variable[1]))

variable <- seq(0, 1, length.out = 3)
Copy link
Member Author

Choose a reason for hiding this comment

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

This one demonstrates that you can get a whole number returned that is not an integer data type.

@topepo topepo marked this pull request as ready for review October 20, 2024 17:47
R/checks.R Outdated Show resolved Hide resolved
R/checks.R Outdated Show resolved Hide resolved
R/checks.R Show resolved Hide resolved
cl <- match.call()
arg <- as.character(cl$x)

for (i in x) {

Choose a reason for hiding this comment

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

I would imagine there's a pretty steep performance penalty here, too. This is the same kind of performance issue that led to tidymodels/parsnip#835.

Maybe an error could be conditional on rlang::is_integerish()?

invisible(x)
}

check_number_decimal_vec <- function(x, call = rlang::caller_env(), ...) {

Choose a reason for hiding this comment

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

Same edits to this function.

library(rlang)

variable <- "pie"
expect_snapshot(brulee:::check_single_logical(variable), error = TRUE)

Choose a reason for hiding this comment

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

Suggested change
expect_snapshot(brulee:::check_single_logical(variable), error = TRUE)
expect_snapshot(check_single_logical(variable), error = TRUE)

In general, don't need to namespace functions from the package being tested.

Copy link
Member Author

Choose a reason for hiding this comment

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

That was left over from testing (since we can source these standalone files)

Copy link
Member

Choose a reason for hiding this comment

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

Could you rename either this file or the corresponding R file so that they have the same slug? I.e. either checks or checkers but not both.

R/checks.R Outdated Show resolved Hide resolved
@@ -0,0 +1,50 @@
test_that("checking single logicals", {
library(rlang)
Copy link
Member

Choose a reason for hiding this comment

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

What do we need this library() call for? Could it be removed? (Same comment for the other occurrences of this pattern.)

Comment on lines 4 to 5
variable <- "pie"
expect_snapshot(brulee:::check_single_logical(variable), error = TRUE)
Copy link
Member

Choose a reason for hiding this comment

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

The inputs to the check function here and below are so short, I would inline them into the call inside of expect_snapshot() so that we can see it in the snapshot .md file. That makes reviewing those easier.

Copy link
Member Author

Choose a reason for hiding this comment

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

I want to make sure that the call includes variable and not the value of the argument. I added one exception here deliberately just to snapshot the in-lined variable (data).

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I see! We could combine both ideas like this:

Suggested change
variable <- "pie"
expect_snapshot(brulee:::check_single_logical(variable), error = TRUE)
expect_snapshot(error = TRUE, {
variable <- "pie"
brulee:::check_single_logical(variable)
})

expect_snapshot(brulee:::check_single_logical(variable), error = TRUE)

variable <- NA
expect_snapshot(brulee:::check_single_logical(variable), error = 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 don't see a need to namespace the check function. Could we remove the brulee:: prefix for readability?

R/checks.R Outdated
Comment on lines 16 to 18
check_number_whole_vec <- function(x, call = rlang::caller_env(), ...) {
cl <- match.call()
arg <- as.character(cl$x)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
check_number_whole_vec <- function(x, call = rlang::caller_env(), ...) {
cl <- match.call()
arg <- as.character(cl$x)
check_number_whole_vec <- function(x, arg = rlang::caller_arg(x), call = rlang::caller_env(), ...) {

R/checks.R Show resolved Hide resolved
R/checks.R Show resolved Hide resolved
@hfrick
Copy link
Member

hfrick commented Oct 21, 2024

Oh, we reviewed both! 👀

@hfrick
Copy link
Member

hfrick commented Oct 21, 2024

But at least we didn't contradict each other 😂

Co-authored-by: Simon P. Couch <simonpatrickcouch@gmail.com>
R/checks.R Outdated Show resolved Hide resolved
@EmilHvitfeldt
Copy link
Member

Worth noting, that a check_whole_numbers() function properly will get added to the standalone file eventually, once something implements it r-lib/rlang#1714 (comment)

topepo and others added 2 commits October 21, 2024 15:21
Co-authored-by: Emil Hvitfeldt <emilhhvitfeldt@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants