From 7359c575ddb71adf43324fe5ae26c976cdf008bd Mon Sep 17 00:00:00 2001 From: Mitchell O'Hara-Wild Date: Mon, 27 Apr 2026 20:12:49 +0200 Subject: [PATCH 1/3] Add special-case S7 class names for S7::class_any and S7::class_missing Resolves #1871 --- R/object-from-call.R | 4 ++++ tests/testthat/test-rd-usage.R | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/R/object-from-call.R b/R/object-from-call.R index dc4d5955..8bde28ae 100644 --- a/R/object-from-call.R +++ b/R/object-from-call.R @@ -289,6 +289,10 @@ s7_class_name <- function(cls, block) { unlist(lapply(cls$classes, s7_class_name, block = block)) } else if (inherits(cls, "S7_S3_class")) { cls$class + } else if (inherits(cls, "S7_any")) { + "any" + } else if (inherits(cls, "S7_missing")) { + "missing" } else { warn_roxy_block(block, "Unknown S7 class type") paste0(deparse(cls), collapse = " ") diff --git a/tests/testthat/test-rd-usage.R b/tests/testthat/test-rd-usage.R index 21aae62e..8f32fce8 100644 --- a/tests/testthat/test-rd-usage.R +++ b/tests/testthat/test-rd-usage.R @@ -542,6 +542,28 @@ test_that("S7 union method usage shows member classes", { ) }) +test_that("S7 class_any method usage shows any class", { + skip_unless_r(">= 4.3.0") + expect_equal( + call_to_usage({ + speak <- S7::new_generic("speak", "x") + S7::method(speak, S7::class_any) <- function(x) "hi" + }), + "## S7 method for class \nspeak(x)" + ) +}) + +test_that("S7 class_missing method usage shows missing class", { + skip_unless_r(">= 4.3.0") + expect_equal( + call_to_usage({ + speak <- S7::new_generic("speak", "x") + S7::method(speak, S7::class_missing) <- function(x) "hi" + }), + "## S7 method for class \nspeak(x)" + ) +}) + test_that("preserves non-breaking-space", { expect_equal( call_to_usage(f <- function(a = "\u{A0}") {}), From 8bf60e4809c44a435f196283c06b14e1dcd2ed84 Mon Sep 17 00:00:00 2001 From: Mitchell O'Hara-Wild Date: Mon, 27 Apr 2026 20:57:59 +0200 Subject: [PATCH 2/3] Simplify & combine tests for naming S7::class_any and S7::class_missing --- tests/testthat/_snaps/object-from-call.md | 14 ++++++++++++++ tests/testthat/test-object-from-call.R | 7 +++++++ tests/testthat/test-rd-usage.R | 22 ---------------------- 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/tests/testthat/_snaps/object-from-call.md b/tests/testthat/_snaps/object-from-call.md index 0766f960..a5590b20 100644 --- a/tests/testthat/_snaps/object-from-call.md +++ b/tests/testthat/_snaps/object-from-call.md @@ -6,6 +6,20 @@ x :3: `@docType "package"` is deprecated. i Please document "_PACKAGE" instead. +# S7 method with special classes any and missing + + Code + s7_class_name(S7::class_any, block) + Output + [1] "any" + +--- + + Code + s7_class_name(S7::class_missing, block) + Output + [1] "missing" + # S7 method with unknown class type warns Code diff --git a/tests/testthat/test-object-from-call.R b/tests/testthat/test-object-from-call.R index b10ec09d..07cc6520 100644 --- a/tests/testthat/test-object-from-call.R +++ b/tests/testthat/test-object-from-call.R @@ -327,6 +327,13 @@ test_that("S7 method on S3 generic includes package prefix in class name", { expect_equal(obj$value$generic, "print") }) +test_that("S7 method with special classes any and missing", { + skip_unless_r(">= 4.3.0") + block <- roxy_block(tags = list(), file = "test.R", line = 1, call = quote(x)) + expect_snapshot(s7_class_name(S7::class_any, block)) + expect_snapshot(s7_class_name(S7::class_missing, block)) +}) + test_that("S7 method with unknown class type warns", { skip_unless_r(">= 4.3.0") block <- roxy_block(tags = list(), file = "test.R", line = 1, call = quote(x)) diff --git a/tests/testthat/test-rd-usage.R b/tests/testthat/test-rd-usage.R index 8f32fce8..21aae62e 100644 --- a/tests/testthat/test-rd-usage.R +++ b/tests/testthat/test-rd-usage.R @@ -542,28 +542,6 @@ test_that("S7 union method usage shows member classes", { ) }) -test_that("S7 class_any method usage shows any class", { - skip_unless_r(">= 4.3.0") - expect_equal( - call_to_usage({ - speak <- S7::new_generic("speak", "x") - S7::method(speak, S7::class_any) <- function(x) "hi" - }), - "## S7 method for class \nspeak(x)" - ) -}) - -test_that("S7 class_missing method usage shows missing class", { - skip_unless_r(">= 4.3.0") - expect_equal( - call_to_usage({ - speak <- S7::new_generic("speak", "x") - S7::method(speak, S7::class_missing) <- function(x) "hi" - }), - "## S7 method for class \nspeak(x)" - ) -}) - test_that("preserves non-breaking-space", { expect_equal( call_to_usage(f <- function(a = "\u{A0}") {}), From 786335765d673098e938cfd34e868986e54e0ec7 Mon Sep 17 00:00:00 2001 From: Mitchell O'Hara-Wild Date: Mon, 27 Apr 2026 21:18:46 +0200 Subject: [PATCH 3/3] expect_equal() to test expected S7 names for class_any and class_missing --- tests/testthat/_snaps/object-from-call.md | 14 -------------- tests/testthat/test-object-from-call.R | 4 ++-- 2 files changed, 2 insertions(+), 16 deletions(-) diff --git a/tests/testthat/_snaps/object-from-call.md b/tests/testthat/_snaps/object-from-call.md index a5590b20..0766f960 100644 --- a/tests/testthat/_snaps/object-from-call.md +++ b/tests/testthat/_snaps/object-from-call.md @@ -6,20 +6,6 @@ x :3: `@docType "package"` is deprecated. i Please document "_PACKAGE" instead. -# S7 method with special classes any and missing - - Code - s7_class_name(S7::class_any, block) - Output - [1] "any" - ---- - - Code - s7_class_name(S7::class_missing, block) - Output - [1] "missing" - # S7 method with unknown class type warns Code diff --git a/tests/testthat/test-object-from-call.R b/tests/testthat/test-object-from-call.R index 07cc6520..253b97d6 100644 --- a/tests/testthat/test-object-from-call.R +++ b/tests/testthat/test-object-from-call.R @@ -330,8 +330,8 @@ test_that("S7 method on S3 generic includes package prefix in class name", { test_that("S7 method with special classes any and missing", { skip_unless_r(">= 4.3.0") block <- roxy_block(tags = list(), file = "test.R", line = 1, call = quote(x)) - expect_snapshot(s7_class_name(S7::class_any, block)) - expect_snapshot(s7_class_name(S7::class_missing, block)) + expect_equal(s7_class_name(S7::class_any, block), "any") + expect_equal(s7_class_name(S7::class_missing, block), "missing") }) test_that("S7 method with unknown class type warns", {