Skip to content

Commit

Permalink
Add an as_tibble method for class lints (#1998)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelChirico authored Jul 3, 2023
1 parent f9eb181 commit 6f1a924
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@
Thanks to Yihui and other developers for their helpful discussions around this issue (#797, @IndrajeetPatil).

* The output of `lint()` and `Lint()` gain S3 class `"list"` to assist with S3 dispatch (#1494, @MichaelChirico)
+ As a corollary, we now register an `as_tibble` method for class `lints`, conditional on {tibble} availability, to avoid dispatching to the `list` method which does not work with `lint()` output (#1997, @MichaelChirico)

* `object_usage_linter()` gives a more helpful warning when a `glue()` expression fails to evaluate (#1985, @MichaelChirico)

Expand Down
8 changes: 8 additions & 0 deletions R/methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ as.data.frame.lints <- function(x, row.names = NULL, optional = FALSE, ...) { #
)
}

as_tibble.lints <- function(x, ..., # nolint: object_name_linter.
.rows = NULL,
.name_repair = c("check_unique", "unique", "universal", "minimal"),
rownames = NULL) {
stopifnot(requireNamespace("tibble", quietly = TRUE))
tibble::as_tibble(as.data.frame(x), ..., .rows = .rows, .name_repair = .name_repair, rownames = rownames)
}

#' @export
`[.lints` <- function(x, ...) {
attrs <- attributes(x)
Expand Down
5 changes: 4 additions & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,9 @@ settings <- NULL
)

settings <<- list2env(default_settings, parent = emptyenv())
invisible()

if (requireNamespace("tibble", quietly = TRUE)) {
registerS3method("as_tibble", "lints", as_tibble.lints, asNamespace("tibble"))
}
}
# nocov end
8 changes: 8 additions & 0 deletions tests/testthat/test-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,11 @@ test_that("within.list is dispatched", {
})
expect_identical(vapply(l, `[[`, integer(1L), "line_number"), 2L:3L)
})

test_that("as_tibble.list is _not_ dispatched directly", {
skip_if_not_installed("tibble")
expect_identical(
nrow(tibble::as_tibble(lint(text = "a = 1", linters = assignment_linter()))),
1L
)
})

0 comments on commit 6f1a924

Please sign in to comment.