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

Use a slightly altered isS3stdGeneric() to detect non-standard generics #857

Merged
merged 6 commits into from
May 23, 2022
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 @@ -180,6 +180,7 @@ function calls. (#850, #851, @renkun-ken)
* `object_usage_linter()` now detects functions exported by packages that are explicitly attached using `library()` or `require()` calls (#1127, @AshesITR)
* New helper `xml_nodes_to_lints()` for converting `xml_node` objects obtained using linter logic expressed in XPath into `Lint` objects (#1124, @michaelchirico)
* Added more explanation why certain functions and operators might be undesirable and what alternatives to use (#1133, #1146, #1159, @AshesITR)
* Improved S3 generic detection for non-standard S3 generics (#846, @jonkeane)

# lintr 2.0.1

Expand Down
15 changes: 8 additions & 7 deletions R/namespace.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ imported_s3_generics <- function(ns_imports) {
}

is_s3_generic <- function(fun) {
if (getRversion() >= "3.5.0") {
# Available in 3.4.0, but bugged there in multiple ways that cause errors
# throughout many base functions, e.g. `-`, `as.null.default`, `dontCheck`
utils::isS3stdGeneric(fun)
} else {
is.function(fun) # nocov
}
# Inspired by `utils::isS3stdGeneric`, though it will detect functions that
# have `useMethod()` in places other than the first expression.
bdexpr <- body(fun)
while (is.call(bdexpr) && bdexpr[[1L]] == "{") bdexpr <- bdexpr[[length(bdexpr)]]
ret <- is.call(bdexpr) && identical(bdexpr[[1L]], as.name("UseMethod"))
if (ret)
names(ret) <- bdexpr[[2L]]
ret
}

.base_s3_generics <- c(
Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/test-namespace.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
test_that("is_s3_generic", {
func <- function(x) {
MichaelChirico marked this conversation as resolved.
Show resolved Hide resolved
print(x)
UseMethod("func")
}
Comment on lines +2 to +5
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a valid use case for the other way around?
i.e. for

func <- function(x) {
  x <- UseMethod("func")
  common_postprocessing(x)
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's try & keep close to the base definition unless there's a compelling reason? i'm also a bit hesitant for this change if it's not being suggested upstream as well

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The base definition checks the first call, so would find my example iinm. That's why I'm asking.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't the call that's found <- though, not UseMethod?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right. I wonder why base R looks for the first call instead of the last, then...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't see anything about isS3stdGeneric() on r-devel in a quick search... maybe worth bringing up there.

cc @moodymudskipper who identified the bug fix at root in #1161


expect_true(is_s3_generic(func))
})

test_that("is_s3_generic doesn't error for namespace-qualified calls", {
func <- function(...) {
pkg::call()
}

expect_warning(result <- is_s3_generic(func), NA)
expect_false(result)
})