Skip to content

Commit

Permalink
[ci] [R-package] enforce more {lintr} checks (#6130)
Browse files Browse the repository at this point in the history
  • Loading branch information
jameslamb committed Oct 8, 2023
1 parent b793cd8 commit 3d9ada7
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 13 deletions.
14 changes: 10 additions & 4 deletions .ci/lint_r_code.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

library(lintr)
loadNamespace("lintr")

args <- commandArgs(
trailingOnly = TRUE
Expand Down Expand Up @@ -33,30 +33,34 @@ LINTERS_TO_USE <- list(
, "any_duplicated" = lintr::any_duplicated_linter()
, "any_is_na" = lintr::any_is_na_linter()
, "assignment" = lintr::assignment_linter()
, "backport" = lintr::backport_linter()
, "boolean_arithmetic" = lintr::boolean_arithmetic_linter()
, "braces" = lintr::brace_linter()
, "class_equals" = lintr::class_equals_linter()
, "commas" = lintr::commas_linter()
, "conjunct_test" = lintr::conjunct_test_linter()
, "duplicate_argument" = lintr::duplicate_argument_linter()
, "empty_assignment" = lintr::empty_assignment_linter()
, "equals_na" = lintr::equals_na_linter()
, "fixed_regex" = lintr::fixed_regex_linter()
, "for_loop_index" = lintr::for_loop_index_linter()
, "function_left" = lintr::function_left_parentheses_linter()
, "function_return" = lintr::function_return_linter()
, "implicit_assignment" = lintr::implicit_assignment_linter()
, "implicit_integers" = lintr::implicit_integer_linter()
, "infix_spaces" = lintr::infix_spaces_linter()
, "inner_combine" = lintr::inner_combine_linter()
, "is_numeric" = lintr::is_numeric_linter()
, "fixed_regex" = lintr::fixed_regex_linter()
, "function_return" = lintr::function_return_linter()
, "lengths" = lintr::lengths_linter()
, "line_length" = lintr::line_length_linter(length = 120L)
, "literal_coercion" = lintr::literal_coercion_linter()
, "long_lines" = lintr::line_length_linter(length = 120L)
, "matrix" = lintr::matrix_apply_linter()
, "missing_argument" = lintr::missing_argument_linter()
, "non_portable_path" = lintr::nonportable_path_linter()
, "numeric_leading_zero" = lintr::numeric_leading_zero_linter()
, "outer_negation" = lintr::outer_negation_linter()
, "package_hooks" = lintr::package_hooks_linter()
, "paren_body" = lintr::paren_body_linter()
, "paste" = lintr::paste_linter()
, "quotes" = lintr::quotes_linter()
, "redundant_equals" = lintr::redundant_equals_linter()
Expand Down Expand Up @@ -100,13 +104,15 @@ LINTERS_TO_USE <- list(
"%>%" = pipe_text
, "%.%" = pipe_text
, "%..%" = pipe_text
, "|>" = pipe_text
, "?" = interactive_text
, "??" = interactive_text
)
)
, "unnecessary_concatenation" = lintr::unnecessary_concatenation_linter()
, "unnecessary_lambda" = lintr::unnecessary_lambda_linter()
, "unreachable_code" = lintr::unreachable_code_linter()
, "unused_import" = lintr::unused_import_linter()
, "vector_logic" = lintr::vector_logic_linter()
, "whitespace" = lintr::whitespace_linter()
)
Expand Down
1 change: 0 additions & 1 deletion R-package/demo/basic_walkthrough.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
library(lightgbm)
library(methods)

# We load in the agaricus dataset
# In this example, we are aiming to predict whether a mushroom is edible
Expand Down
1 change: 0 additions & 1 deletion R-package/demo/boost_from_prediction.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
library(lightgbm)
library(methods)

# Load in the agaricus dataset
data(agaricus.train, package = "lightgbm")
Expand Down
1 change: 0 additions & 1 deletion R-package/demo/early_stopping.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
library(lightgbm)
library(methods)

# Load in the agaricus dataset
data(agaricus.train, package = "lightgbm")
Expand Down
2 changes: 1 addition & 1 deletion R-package/tests/testthat.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
library(testthat)
library(lightgbm)
library(lightgbm) # nolint: [unused_import]

test_check(
package = "lightgbm"
Expand Down
4 changes: 3 additions & 1 deletion R-package/tests/testthat/test_dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,9 @@ test_that("lgb.Dataset: colnames", {
colnames(dtest) <- "asdf"
})
new_names <- make.names(seq_len(ncol(test_data)))
expect_silent(colnames(dtest) <- new_names)
expect_silent({
colnames(dtest) <- new_names
})
expect_equal(colnames(dtest), new_names)
})

Expand Down
9 changes: 6 additions & 3 deletions R-package/tests/testthat/test_learning_to_rank.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ test_that("learning-to-rank with lgb.train() works as expected", {
eval_results <- model$eval_train()
expect_equal(length(eval_results), length(eval_names))
for (result in eval_results) {
expect_true(result[["value"]] > 0.0 && result[["value"]] < 1.0)
expect_true(result[["value"]] > 0.0)
expect_true(result[["value"]] < 1.0)
expect_true(result[["higher_better"]])
expect_identical(result[["data_name"]], "training")
}
Expand Down Expand Up @@ -104,8 +105,10 @@ test_that("learning-to-rank with lgb.cv() works as expected", {
# check that best score and iter make sense (0.0 < nDCG < 1.0)
best_iter <- cv_bst$best_iter
best_score <- cv_bst$best_score
expect_true(best_iter > 0L && best_iter <= nrounds)
expect_true(best_score > 0.0 && best_score < 1.0)
expect_true(best_iter > 0L)
expect_true(best_iter <= nrounds)
expect_true(best_score > 0.0)
expect_true(best_score < 1.0)
expect_true(abs(best_score - 0.75) < .LGB_NUMERIC_TOLERANCE)

# best_score should be set for the first metric
Expand Down
4 changes: 3 additions & 1 deletion R-package/tests/testthat/test_lgb.Booster.R
Original file line number Diff line number Diff line change
Expand Up @@ -1275,7 +1275,9 @@ test_that("params (including dataset params) should be stored in .rds file for B
bst_file <- tempfile(fileext = ".rds")
expect_warning(saveRDS.lgb.Booster(bst, file = bst_file))

expect_warning(bst_from_file <- readRDS.lgb.Booster(file = bst_file))
expect_warning({
bst_from_file <- readRDS.lgb.Booster(file = bst_file)
})
expect_identical(
bst_from_file$params
, list(
Expand Down

0 comments on commit 3d9ada7

Please sign in to comment.