-
Notifications
You must be signed in to change notification settings - Fork 48
Class/generic/methods introspection #620
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
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
294e57c
Implement `S7_classes()` and `S7_generics()`
hadley cb67829
Draft `S7_methods()`
hadley 46442f9
News bullet
hadley 2c32ba2
Merge commit '0dc2e5dda5e755842705555bba30d4ea41d35b75'
hadley bcfc021
Restrict attached_envs
hadley a56e2e8
Reduce some duplication
hadley 7d22ebd
Style
hadley 39ba25d
More polishing
hadley 10dc991
Tweak columns
hadley 81dc2e0
Merge commit 'eb1b1b0dde406e213dad9740f920bf5fcd58509d'
hadley 3c990ba
Polish news
hadley 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| #' Find S7 classes and generics in an environment | ||
| #' | ||
| #' @description | ||
| #' * `S7_classes()` returns the names of S7 classes defined in `env`. | ||
| #' * `S7_generics()` returns the names of S7 generics defined in `env`. | ||
| #' | ||
| #' @param env An environment. Defaults to the caller's environment. | ||
| #' To inspect a package, pass `asNamespace("pkg")`; to inspect the global | ||
| #' environment, pass `globalenv()`. | ||
| #' @returns A character vector of names. | ||
| #' @export | ||
| #' @examples | ||
| #' # List S7 classes exported by the S7 package itself | ||
| #' S7_classes(asNamespace("S7")) | ||
| #' S7_generics(asNamespace("S7")) | ||
| S7_classes <- function(env = parent.frame()) { | ||
| find_objects(env, is_class) | ||
| } | ||
|
|
||
| #' @export | ||
| #' @rdname S7_classes | ||
| S7_generics <- function(env = parent.frame()) { | ||
| find_objects(env, is_S7_generic) | ||
| } | ||
|
|
||
| #' List S7 methods | ||
| #' | ||
| #' List the methods registered on an S7 `generic`, or the methods registered | ||
| #' for a given `class` across all S7 generics defined in attached packages. | ||
| #' | ||
| #' @param generic An S7 generic. | ||
| #' @param class A class specification (anything accepted by [as_class()]). | ||
| #' When supplied, every S7 generic in every attached package is searched | ||
| #' for methods with this class in their signature. | ||
| #' @returns A data frame with one row per matching method and columns: | ||
| #' | ||
| #' * `generic`: the generic's name. | ||
| #' * `package`: the package the generic is defined in, or `NA` for generics | ||
| #' found in the global environment (or when `generic` is supplied | ||
| #' directly). | ||
| #' * `signature`: a list column of `S7_signature` objects describing the | ||
| #' dispatch signature. `format()` them for a human-readable description. | ||
| #' @export | ||
| #' @examples | ||
| #' Foo <- new_class("Foo", package = NULL) | ||
| #' Bar <- new_class("Bar", package = NULL) | ||
| #' my_gen <- new_generic("my_gen", "x") | ||
| #' method(my_gen, Foo) <- function(x) "foo" | ||
| #' method(my_gen, Bar) <- function(x) "bar" | ||
| #' | ||
| #' S7_methods(generic = my_gen) | ||
| #' S7_methods(class = Foo) | ||
| S7_methods <- function(generic = NULL, class = NULL) { | ||
| if (!is.null(generic)) { | ||
| if (!is_S7_generic(generic)) { | ||
| stop("`generic` must be an S7 generic.") | ||
| } | ||
| generics <- list(list(generic = generic, package = NA_character_)) | ||
| } else { | ||
| generics <- attached_generics() | ||
| } | ||
|
|
||
| if (!is.null(class)) { | ||
| target <- class_register(as_class(class)) | ||
| } else { | ||
| target <- NULL | ||
| } | ||
|
|
||
| rows <- lapply(generics, function(g) { | ||
| generic_method_rows(g$generic, g$package, target) | ||
| }) | ||
| out <- do.call(rbind, rows) | ||
| out$signature <- new_signature_list(out$signature) | ||
| out | ||
| } | ||
|
|
||
| generic_method_rows <- function(generic, package, class) { | ||
| ms <- methods(generic) | ||
| if (!is.null(class)) { | ||
| has_class <- function(m) any(vcapply(m@signature, class_register) == class) | ||
| ms <- Filter(has_class, ms) | ||
| } | ||
|
|
||
| data.frame( | ||
| generic = rep(generic@name, length(ms)), | ||
| package = rep(package, length(ms)), | ||
| signature = I(lapply(ms, \(m) new_signature(m@signature))) | ||
| ) | ||
| } | ||
|
|
||
| # A list column of S7_signatures, used by S7_methods(). Needs its own class | ||
| # so that print.data.frame() formats it per-element: data frames format whole | ||
| # columns, so the scalar format.S7_signature() method is never reached. | ||
| new_signature_list <- function(x) { | ||
| class(x) <- "S7_signature_list" | ||
| x | ||
| } | ||
|
|
||
| #' @export | ||
| format.S7_signature_list <- function(x, ...) { | ||
| vcapply(unclass(x), format) | ||
| } | ||
|
|
||
| #' @export | ||
| print.S7_signature_list <- function(x, ...) { | ||
| print(format(x), quote = FALSE) | ||
| invisible(x) | ||
| } | ||
|
|
||
| # All S7 generics reachable from attached packages and the global env, | ||
| # each tagged with the package it was found in (`NA` for the global env). | ||
| attached_generics <- function() { | ||
| out <- list() | ||
| for (env in attached_envs()) { | ||
| package <- env_package(env) | ||
| for (generic in unname(find_matches(env, is_S7_generic))) { | ||
| out[[length(out) + 1L]] <- list(generic = generic, package = package) | ||
| } | ||
| } | ||
| out | ||
| } | ||
|
|
||
| env_package <- function(env) { | ||
| if (identical(env, globalenv())) { | ||
| NA_character_ | ||
| } else { | ||
| sub("^package:", "", environmentName(env)) | ||
| } | ||
| } | ||
|
|
||
| attached_envs <- function() { | ||
| envs <- search() | ||
| pkgs <- envs[grepl("^package:", envs)] | ||
| pkgs <- setdiff(pkgs, "package:base") | ||
|
|
||
| c(lapply(pkgs, as.environment), globalenv()) | ||
| } | ||
|
|
||
| find_objects <- function(env, predicate) { | ||
| names(find_matches(env, predicate)) | ||
| } | ||
|
|
||
| # Named list of objects in `env` satisfying `predicate`. | ||
| find_matches <- function(env, predicate) { | ||
| if (isNamespace(env)) { | ||
| # Not attached; use exported values | ||
| names <- getNamespaceExports(env) | ||
| } else { | ||
| # Attached or global; use all values | ||
| names <- ls(envir = env) | ||
| } | ||
|
|
||
| objs <- mget(names, envir = env, inherits = FALSE) | ||
| Filter(predicate, objs) | ||
| } | ||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # S7_methods() prints the signature column readably | ||
|
|
||
| Code | ||
| print(S7_methods(generic = gen)) | ||
| Output | ||
| generic package signature | ||
| 1 gen <NA> <Foo> | ||
| 2 gen <NA> <Bar> | ||
|
|
||
| # S7_methods() validates inputs | ||
|
|
||
| Code | ||
| S7_methods(generic = "not a generic") | ||
| Condition | ||
| Error in `S7_methods()`: | ||
| ! `generic` must be an S7 generic. | ||
|
|
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
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.
Should we also discover generics in loaded but not attached namespaces?
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.
My gut feeling is no? But that's a not particularly strongly held opinion.
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.
Agree with @hadley.