-
Notifications
You must be signed in to change notification settings - Fork 187
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
expect_s3_class_linter and expect_s4_class_linter #943
Changes from 3 commits
8897f0d
ea23ba3
333f3fa
5389735
8502c1c
0b6e369
413b620
6fdd77e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
#' Require usage of expect_s3_class() | ||
#' | ||
#' [testthat::expect_s3_class()] exists specifically for testing the class | ||
#' of S3 objects. [testthat::expect_equal()], [testthat::expect_identical()], | ||
#' and [testthat::expect_true()] can also be used for such tests, | ||
#' but it is better to use the tailored function instead. | ||
#' | ||
#' @evalRd rd_tags("expect_s3_class_linter") | ||
#' @seealso [linters] for a complete list of linters available in lintr. | ||
#' @export | ||
expect_s3_class_linter <- function() { | ||
Linter(function(source_file) { | ||
if (length(source_file$parsed_content) == 0L) { | ||
return(list()) | ||
} | ||
|
||
xml <- source_file$xml_parsed_content | ||
|
||
# (1) expect_{equal,identical}(class(x), C) | ||
# (2) expect_true(is.<class>(x)) and expect_true(inherits(x, C)) | ||
is_class_call <- xp_text_in_table(c(is_s3_class_calls, "inherits")) | ||
xpath <- glue::glue("//expr[ | ||
( | ||
SYMBOL_FUNCTION_CALL[text() = 'expect_equal' or text() = 'expect_identical'] | ||
and following-sibling::expr[ | ||
expr[SYMBOL_FUNCTION_CALL[text() = 'class']] | ||
and (position() = 1 or preceding-sibling::expr[STR_CONST]) | ||
AshesITR marked this conversation as resolved.
Show resolved
Hide resolved
|
||
] | ||
) or ( | ||
SYMBOL_FUNCTION_CALL[text() = 'expect_true'] | ||
and following-sibling::expr[1][expr[SYMBOL_FUNCTION_CALL[ {is_class_call} ]]] | ||
) | ||
]") | ||
|
||
bad_expr <- xml2::xml_find_all(xml, xpath) | ||
return(lapply(bad_expr, gen_expect_s3_class_lint, source_file)) | ||
}) | ||
} | ||
|
||
# NB: there is no easy way to make an exhaustive list of places where an | ||
# is.<x> call can be replaced by expect_s3_class(); this list was manually | ||
# populated from the default R packages by inspection. For example, | ||
# is.matrix(x) cannot be replaced by expect_s3_class(x, "matrix") because | ||
# it is not actually an S3 class (is.object(x) is not TRUE since there | ||
# is no class set for a matrix [I am not sure if this changes in R 4]. | ||
# Further, there are functions named is.<x> that have nothing to do with | ||
# object type, e.g. is.finite(), is.nan(), or is.R(). | ||
is_s3_class_calls <- paste0("is.", c( | ||
# base | ||
"data.frame", "factor", "numeric_version", | ||
"ordered", "package_version", "qr", "table", | ||
# utils grDevices tcltk tcltk grid grid | ||
"relistable", "raster", "tclObj", "tkwin", "grob", "unit", | ||
# stats | ||
"mts", "stepfun", "ts", "tskernel" | ||
)) | ||
|
||
gen_expect_s3_class_lint <- function(expr, source_file) { | ||
matched_function <- xml2::xml_text(xml2::xml_find_first(expr, "SYMBOL_FUNCTION_CALL")) | ||
if (matched_function %in% c("expect_equal", "expect_identical")) { | ||
lint_msg <- sprintf("expect_s3_class(x, k) is better than %s(class(x), k).", matched_function) | ||
} else { | ||
lint_msg <- "expect_s3_class(x, k) is better than expect_true(is.<k>(x)) or expect_true(inherits(x, k))." | ||
} | ||
lint_msg <- paste(lint_msg, "Note also expect_s4_class() available for testing S4 objects.") | ||
xml_nodes_to_lint(expr, source_file, lint_msg, type = "warning") | ||
} | ||
|
||
#' Require usage of expect_s4_class(x, k) over expect_true(is(x, k)) | ||
#' | ||
#' [testthat::expect_s4_class()] exists specifically for testing the class | ||
#' of S4 objects. [testthat::expect_true()] can also be used for such tests, | ||
#' but it is better to use the tailored function instead. | ||
#' | ||
#' @evalRd rd_tags("expect_s3_class_linter") | ||
#' @seealso [linters] for a complete list of linters available in lintr. | ||
#' @export | ||
expect_s4_class_linter <- function() { | ||
Linter(function(source_file) { | ||
if (length(source_file$parsed_content) == 0L) { | ||
return(list()) | ||
} | ||
|
||
xml <- source_file$xml_parsed_content | ||
|
||
# TODO(michaelchirico): also catch expect_{equal,identical}(methods::is(x), k). | ||
# there are no hits for this on google3 as of now. | ||
|
||
# require 2 expressions because methods::is(x) alone is a valid call, even | ||
# though the character output wouldn't make any sense for expect_true(). | ||
xpath <- "//expr[ | ||
SYMBOL_FUNCTION_CALL[text() = 'expect_true'] | ||
and following-sibling::expr[1][count(expr) = 3 and expr[SYMBOL_FUNCTION_CALL[text() = 'is']]] | ||
]" | ||
|
||
bad_expr <- xml2::xml_find_all(xml, xpath) | ||
return(lapply( | ||
bad_expr, | ||
xml_nodes_to_lint, | ||
source_file = source_file, | ||
message = paste( | ||
"expect_s4_class(x, k) is better than expect_true(is(x, k)).", | ||
"Note also expect_s3_class() available for testing S3 objects." | ||
), | ||
type = "warning" | ||
)) | ||
}) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
test_that("expect_s3_class_linter skips allowed usages", { | ||
# expect_s3_class doesn't have an inverted version | ||
expect_lint("expect_true(!inherits(x, 'class'))", NULL, expect_s3_class_linter()) | ||
# NB: also applies to tinytest, but it's sufficient to test testthat | ||
expect_lint("testthat::expect_s3_class(!inherits(x, 'class'))", NULL, expect_s3_class_linter()) | ||
MichaelChirico marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# other is.<x> calls are not suitable for expect_s3_class in particular | ||
expect_lint("expect_true(is.na(x))", NULL, expect_s3_class_linter()) | ||
|
||
# case where expect_s3_class() *could* be used but we don't enforce | ||
expect_lint("expect_true(is.data.table(x))", NULL, expect_s3_class_linter()) | ||
}) | ||
|
||
test_that("expect_s3_class_linter blocks simple disallowed usages", { | ||
expect_lint( | ||
"expect_equal(class(x), 'data.frame')", | ||
AshesITR marked this conversation as resolved.
Show resolved
Hide resolved
|
||
rex::rex("expect_s3_class(x, k) is better than expect_equal(class(x), k)"), | ||
expect_s3_class_linter() | ||
) | ||
|
||
# expect_identical is treated the same as expect_equal | ||
expect_lint( | ||
"testthat::expect_identical(class(x), 'lm')", | ||
rex::rex("expect_s3_class(x, k) is better than expect_identical(class(x), k)"), | ||
expect_s3_class_linter() | ||
) | ||
|
||
# yoda test with string literal in first arg also caught | ||
expect_lint( | ||
"expect_equal('data.frame', class(x))", | ||
rex::rex("expect_s3_class(x, k) is better than expect_equal(class(x), k)"), | ||
expect_s3_class_linter() | ||
) | ||
|
||
# different equivalent usages | ||
expect_lint( | ||
"expect_true(is.table(foo(x)))", | ||
rex::rex("expect_s3_class(x, k) is better than expect_true(is.<k>(x))"), | ||
expect_s3_class_linter() | ||
) | ||
expect_lint( | ||
"expect_true(inherits(x, 'table'))", | ||
rex::rex("expect_s3_class(x, k) is better than expect_true(is.<k>(x))"), | ||
expect_s3_class_linter() | ||
) | ||
|
||
# TODO(michaelchirico): consider more carefully which sorts of class(x) %in% . and | ||
# . %in% class(x) calls should be linted | ||
# expect_lint( | ||
# "expect_true('lm' %in% class(x))", | ||
# "expect_s3_class\\(x, k\\) is better than expect_equal\\(class\\(x\\), k", | ||
# expect_s3_class_linter | ||
# ) | ||
}) | ||
|
||
test_that("expect_s4_class_linter skips allowed usages", { | ||
# expect_s4_class doesn't have an inverted version | ||
expect_lint("expect_true(!is(x, 'class'))", NULL, expect_s4_class_linter()) | ||
# NB: also applies to tinytest, but it's sufficient to test testthat | ||
expect_lint("testthat::expect_s3_class(!is(x, 'class'))", NULL, expect_s4_class_linter()) | ||
}) | ||
|
||
test_that("expect_s4_class blocks simple disallowed usages", { | ||
expect_lint( | ||
"expect_true(is(x, 'data.frame'))", | ||
rex::rex("expect_s4_class(x, k) is better than expect_true(is(x, k))"), | ||
expect_s4_class_linter() | ||
) | ||
|
||
# namespace qualification is irrelevant | ||
expect_lint( | ||
"testthat::expect_true(methods::is(x, 'SpatialPolygonsDataFrame'))", | ||
rex::rex("expect_s4_class(x, k) is better than expect_true(is(x, k))"), | ||
expect_s4_class_linter() | ||
) | ||
}) | ||
|
||
skip_if_not_installed("patrick") | ||
local({ | ||
# test for lint errors appropriately raised for all is.<class> calls | ||
is_classes <- c( | ||
"data.frame", "factor", "numeric_version", | ||
"ordered", "package_version", "qr", "table", | ||
"relistable", "raster", "tclObj", "tkwin", "grob", "unit", | ||
"mts", "stepfun", "ts", "tskernel" | ||
) | ||
patrick::with_parameters_test_that( | ||
"expect_true(is.<base class>) is caught", | ||
expect_lint( | ||
sprintf("expect_true(is.%s(x))", is_class), | ||
rex::rex("expect_s3_class(x, k) is better than expect_true(is.<k>(x))"), | ||
expect_s3_class_linter() | ||
), | ||
.test_name = is_classes, | ||
is_class = is_classes | ||
) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
false positive lint caused by glue: local variable ‘is_class_call’ assigned but may not be used.