Skip to content

Commit bcde5a2

Browse files
authored
Deploy a targeted fix for all.equal() methods (#1829)
Fixes #1587
1 parent 5af968d commit bcde5a2

3 files changed

Lines changed: 13 additions & 3 deletions

File tree

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
* roxygen2 no longer depends on purrr.
3636
* roxygen2 now requires R 4.1 (#1632).
3737
* S3 method handling improvements:
38-
* Method parsing now prefers the longest matching generic name, so e.g. `all.equal.numeric` is correctly identified as a method of `all.equal` rather than `all` (#1587).
38+
* Methods of `all.equal()` (e.g. `all.equal.numeric`) are now correctly identified as methods of `all.equal()`, `all()` (#1587).
3939
* The warning about undocumented methods no longer errors when the function lacks a srcref, e.g. because a debugger breakpoint is set (#1589, #1710).
4040
* The warning about undocumented methods no longer incorrectly flags S4 methods of S3 generics as unexported (#1715).
4141
* `@description` no longer errors when the markdown text starts with a heading (#1705).

R/object-s3.R

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ find_generic <- function(name, env = parent.frame()) {
8080
return(NULL)
8181
}
8282

83+
# special case all.equal() methods to avoid treating as all() methods (#1587)
84+
if (startsWith(name, "all.equal.") && is_s3_generic("all.equal", env)) {
85+
method <- substr(name, nchar("all.equal.") + 1, nchar(name))
86+
return(c("all.equal", method))
87+
}
88+
8389
pieces <- str_split(name, fixed("."))[[1]]
8490
n <- length(pieces)
8591

@@ -88,7 +94,7 @@ find_generic <- function(name, env = parent.frame()) {
8894
return(NULL)
8995
}
9096

91-
for (i in rev(seq_len(n - 1))) {
97+
for (i in seq_len(n - 1)) {
9298
generic <- paste0(pieces[seq_len(i)], collapse = ".")
9399
class <- paste0(pieces[(i + 1):n], collapse = ".")
94100

tests/testthat/test-object-s3.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,12 @@ test_that("S4 generics are not treated as S3 methods", {
5757
expect_false(is_s3_method("all.equal", env))
5858
})
5959

60-
test_that("find_generic prefers longest generic name", {
60+
test_that("all.equal is matched correctly", {
6161
expect_equal(find_generic("all.equal.numeric"), c("all.equal", "numeric"))
62+
expect_equal(
63+
find_generic("all.equal.data.frame"),
64+
c("all.equal", "data.frame")
65+
)
6266
})
6367

6468
test_that("user defined functions override primitives", {

0 commit comments

Comments
 (0)