Skip to content

Commit 87b33e2

Browse files
committed
in obj_type(), isS4() takes precedence over has_S4_class(), since an S4 object is just that, even if it inherits from an S7 class and thus carries an S7_class slot.
1 parent 3c006a8 commit 87b33e2

3 files changed

Lines changed: 33 additions & 3 deletions

File tree

R/class-spec.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,10 @@ class_inherits <- function(x, what) {
322322
obj_type <- function(x) {
323323
if (identical(x, quote(expr = ))) {
324324
"missing"
325-
} else if (has_S7_class(x)) {
326-
"S7"
327325
} else if (isS4(x)) {
328326
"S4"
327+
} else if (has_S7_class(x)) {
328+
"S7"
329329
} else if (is.object(x)) {
330330
"S3"
331331
} else {

R/class.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,11 @@ S7_class <- function(object) {
427427
obj_type(object),
428428
missing = class_missing,
429429
S7 = attr(object, "S7_class", exact = TRUE),
430-
S4 = methods::getClass(class(object)),
430+
S4 = if (has_S7_class(object)) {
431+
methods::slot(object, "S7_class")
432+
} else {
433+
methods::getClass(class(object))
434+
},
431435
S3 = new_S3_class(class(object)),
432436
base = base_S7_class(object)
433437
)

tests/testthat/test-S4.R

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,32 @@ describe("S4_register", {
128128
expect_equal(prop(object, "x"), 1)
129129
expect_equal(prop(object, "y"), "a")
130130
expect_true(methods::validObject(object))
131+
expect_equal(S7_class(object), S4regContainsChild)
132+
expect_match(obj_desc(object), "^S4<")
133+
134+
expect_equal(
135+
S4_to_S7_class(getClass("S4regContainsS4Child")),
136+
getClass("S4regContainsS4Child")
137+
)
138+
expect_contains(
139+
obj_dispatch(object),
140+
c(
141+
S4_class_name(getClass("S4regContainsS4Child")),
142+
S4regContainsChild_old
143+
)
144+
)
145+
146+
S4regContainsDispatch <- new_generic("S4regContainsDispatch", "x")
147+
method(S4regContainsDispatch, S4regContainsChild) <- function(x) {
148+
"S7"
149+
}
150+
method(
151+
S4regContainsDispatch,
152+
getClass("S4regContainsS4Child")
153+
) <- function(x) {
154+
"S4"
155+
}
156+
expect_equal(S4regContainsDispatch(object), "S4")
131157

132158
invalid <- object
133159
methods::slot(invalid, "y") <- "bad"

0 commit comments

Comments
 (0)