-
Notifications
You must be signed in to change notification settings - Fork 47
Strip external generics on build #662
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
Open
hadley
wants to merge
5
commits into
main
Choose a base branch
from
strip-external-generics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| #' Package hooks for S7 methods | ||
| #' | ||
| #' @description | ||
| #' When using S7 in a package, add two hooks to your `zzz.R`: | ||
| #' | ||
| #' * Call `methods_register()` from `.onLoad()`. This is S7's way of | ||
| #' registering methods, rather than using `NAMESPACE` directives like S3 and | ||
| #' S4 do. It ensures that methods for generics (S3, S4, and S7) defined in | ||
| #' other packages are registered as needed when your package is loaded. This | ||
| #' is only strictly necessary if you register methods for generics in other | ||
| #' packages, but there's no harm in always including it and it ensures you | ||
| #' won't forget later. | ||
| #' | ||
| #' * Call `S7_on_build()` at the top level (i.e. *not* inside `.onLoad()`) | ||
| #' after all method registration is complete. This avoids embedding copies | ||
| #' of external generics in your package when you use `method<-`. | ||
| #' | ||
| #' See `vignette("packages")` for more details. | ||
| #' | ||
| #' @importFrom utils getFromNamespace packageName | ||
| #' @returns Nothing; both functions are called for their side-effects. | ||
| #' @examples | ||
| #' # In zzz.R: | ||
| #' .onLoad <- function(...) { | ||
| #' S7::methods_register() | ||
| #' } | ||
| #' S7::S7_on_build() | ||
| #' @export | ||
| methods_register <- function() { | ||
| package <- packageName(parent.frame()) | ||
| ns <- topenv(parent.frame()) | ||
| # TODO?: check/enforce that methods_register() is being called from .onLoad() | ||
|
|
||
| tbl <- S7_methods_table(package) | ||
|
|
||
| for (x in tbl) { | ||
| register <- registrar(x$generic, x$signature, x$method, ns) | ||
|
|
||
| if (isNamespaceLoaded(x$generic$package)) { | ||
| register() | ||
| } | ||
| setHook(packageEvent(x$generic$package, "onLoad"), register) | ||
| } | ||
|
|
||
| invisible() | ||
| } | ||
|
|
||
| #' @export | ||
| #' @rdname methods_register | ||
| S7_on_build <- function() { | ||
| strip_generic_sentinels(topenv(parent.frame())) | ||
| } | ||
|
|
||
| strip_generic_sentinels <- function(ns) { | ||
| for (name in ls(ns, all.names = TRUE)) { | ||
| if (is_generic_sentinel(get0(name, envir = ns, inherits = FALSE))) { | ||
| rm(list = name, envir = ns) | ||
| } | ||
| } | ||
| invisible() | ||
| } | ||
|
|
||
| generic_sentinel <- function(generic) { | ||
| external <- as_external_generic(generic) | ||
| class(external) <- c("S7_generic_sentinel", "S7_external_generic") | ||
| external | ||
| } | ||
|
|
||
| is_generic_sentinel <- function(x) inherits(x, "S7_generic_sentinel") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| Package: t3 | ||
| Title: Test Provider Package With S3 and S4 Generics | ||
| Version: 0.0.0.9000 | ||
| Authors@R: | ||
| person("Jim", "Hester", , "james.f.hester@gmail.com", role = c("aut", "cre")) | ||
| Description: Defines an S3 generic and an S4 generic for other packages to | ||
| register S7 methods against. | ||
| Imports: methods | ||
| License: MIT + file LICENSE | ||
| Encoding: UTF-8 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| export(t3_s3) | ||
| export(t3_s4) | ||
| exportMethods(t3_s4) | ||
| import(methods) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| #' @export | ||
| t3_s3 <- function(x) UseMethod("t3_s3") | ||
|
|
||
| #' @export | ||
| methods::setGeneric("t3_s4", function(x) standardGeneric("t3_s4")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| Package: t4 | ||
| Title: Test Consumer Registering Methods for Another Package's Generics | ||
| Version: 0.0.0.9000 | ||
| Authors@R: | ||
| person("Jim", "Hester", , "james.f.hester@gmail.com", role = c("aut", "cre")) | ||
| Description: Registers S7 methods for the S3 and S4 generics defined in t3, | ||
| exercising S7_on_build(). | ||
| Imports: S7, t3, methods | ||
| License: MIT + file LICENSE | ||
| Encoding: UTF-8 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| export(t4_class) | ||
| importFrom(t3, t3_s3) | ||
| importFrom(t3, t3_s4) | ||
| import(methods) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| #' @export | ||
| t4_class <- S7::new_class("t4_class", package = "t4") | ||
|
|
||
| # Register an S7 method for an S3 generic defined in another package (t3). | ||
| S7::method(t3_s3, t4_class) <- function(x) "s3-dispatch" | ||
|
|
||
| # Register an S7 method for an S4 generic defined in another package (t3). | ||
| S7::S4_register(t4_class) | ||
| S7::method(t3_s4, t4_class) <- function(x) "s4-dispatch" | ||
|
|
||
| .onLoad <- function(libname, pkgname) { | ||
| S7::S4_register(t4_class) | ||
| S7::methods_register() | ||
| } | ||
|
|
||
| S7::S7_on_build() | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lawremi Claude discovered this while writing this test. Does that seem correct to you? (i.e. that
S4_register(), which callssetOldClass()has to be called onLoad)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or maybe this is related to the environment fixes in #643?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't be necessary to call
S4_register()at runtime. All it does is callsetOldClass()which of course works at build time. Strangely, Gemini suggested the same thing when converting a package to use S7.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's hope that #663 fixes this. I'll hold off on merging until that's confirmed.