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

friendly_type_of() recognizes S7 objects #1688

Merged
merged 2 commits into from
Jul 10, 2024
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
11 changes: 7 additions & 4 deletions R/standalone-obj-type.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
# ---
# repo: r-lib/rlang
# file: standalone-obj-type.R
# last-updated: 2023-05-01
# last-updated: 2024-02-14
# license: https://unlicense.org
# imports: rlang (>= 1.1.0)
# ---
#
# ## Changelog
#
# 2024-02-14:
# - `obj_type_friendly()` now works for S7 objects.
#
# 2023-05-01:
# - `obj_type_friendly()` now only displays the first class of S3 objects.
#
Expand Down Expand Up @@ -263,19 +266,19 @@ vec_type_friendly <- function(x, length = FALSE) {
#' Return OO type
#' @param x Any R object.
#' @return One of `"bare"` (for non-OO objects), `"S3"`, `"S4"`,
#' `"R6"`, or `"R7"`.
#' `"R6"`, or `"S7"`.
#' @noRd
obj_type_oo <- function(x) {
if (!is.object(x)) {
return("bare")
}

class <- inherits(x, c("R6", "R7_object"), which = TRUE)
class <- inherits(x, c("R6", "S7_object"), which = TRUE)

if (class[[1]]) {
"R6"
} else if (class[[2]]) {
"R7"
"S7"
} else if (isS4(x)) {
"S4"
} else {
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-standalone-obj-type.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ test_that("obj_type_oo() works", {
r6 <- R6Class("r6")$new()
expect_equal(obj_type_oo(r6), "R6")

import_or_skip("R7", "new_class")
r7 <- new_class("r7")()
expect_equal(obj_type_oo(r7), "R7")
import_or_skip("S7", "new_class")
s7 <- new_class("s7")()
expect_equal(obj_type_oo(s7), "S7")
})

test_that("stop_input_type() handles I() in `arg` (#1607)", {
Expand Down
Loading