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

Add an as_tibble method for class lints #1998

Merged
merged 2 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
)
})