Skip to content

Commit b5cf389

Browse files
t-kalinowskihadley
andauthored
Automatically detect package name in new_class(package=) (#459)
* add `topNamespaceName()` * fixes to prevent `S7::` class prefix in package code * fixes to prevent `S7::` class prefix in tests * add NEWS * use explicit `package=NULL` in snapshot tests --------- Co-authored-by: Hadley Wickham <h.wickham@gmail.com>
1 parent 6e2e582 commit b5cf389

28 files changed

Lines changed: 92 additions & 65 deletions

NEWS.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
# S7 (development version)
22

3+
* `new_class()` now automatically infers the package name when called from
4+
within an R package (#459).
5+
36
* Improved error message when custom validators return invalid values (#454, #457).
47

58
* New `nameOfClass()` method exported for S7 base classes, to enable usage like
69
`inherits("foo", S7::class_character)` (#432, #458)
710

811
* Added support for more base/S3 classes (#434):
9-
`class_POSIXlt`, `class_POSIXt`, `class_matrix`, `class_array`,
10-
`class_formula`, `class_call`, `class_language`, `class_name`
12+
`class_POSIXlt`, `class_POSIXt`, `class_formula`,
13+
`class_call`, `class_language`, `class_name`
1114

1215
* Fixed S3 methods registration across packages (#422).
1316

R/aaa.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ new_function <- function(args = NULL,
2222
x
2323
}
2424

25+
26+
topNamespaceName <- function(env = parent.frame()) {
27+
env <- topenv(env)
28+
if (isNamespace(env))
29+
getNamespaceName(env)
30+
}
31+
2532
is_string <- function(x) {
2633
identical(class(x), "character") && length(x) == 1L && !is.na(x) && x != ""
2734
}

R/class.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
new_class <- function(
101101
name,
102102
parent = S7_object,
103-
package = NULL,
103+
package = topNamespaceName(parent.frame()),
104104
properties = list(),
105105
abstract = FALSE,
106106
constructor = NULL,

R/method-register.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ register_S3_method <- function(generic, signature, method, envir = parent.frame(
107107
}
108108

109109
class <- S7_class_name(signature[[1]])
110-
# dbg(generic$name, class, method, envir)
111110
registerS3method(generic$name, class, method, envir)
112111
}
113112

R/super.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#' For example, imagine that you have made a subclass of "integer":
2727
#'
2828
#' ```{r}
29-
#' myint <- new_class("myint", parent = class_integer)
29+
#' myint <- new_class("myint", parent = class_integer, package = NULL)
3030
#' ```
3131
#'
3232
#' Now you go to write a custom print method:

R/zzz.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#' S7_object
1111
S7_object <- new_class(
1212
name = "S7_object",
13+
package = NULL,
1314
parent = NULL,
1415
constructor = function() {
1516
.Call(S7_object_)
@@ -96,6 +97,7 @@ on_load_define_S7_generic <- function() {
9697
# errors if `@` is not usable.
9798
S7_generic <<- new_class(
9899
name = "S7_generic",
100+
package = NULL,
99101
properties = list(
100102
name = class_character,
101103
methods = class_environment,
@@ -114,6 +116,7 @@ S7_method <- NULL
114116
on_load_define_S7_method <- function() {
115117
S7_method <<- new_class(
116118
"S7_method",
119+
package = NULL,
117120
parent = class_function,
118121
properties = list(generic = S7_generic, signature = class_list)
119122
)

man/new_class.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/super.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/class.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@
174174
# S7 object: displays nicely
175175

176176
Code
177-
foo <- new_class("foo", properties = list(x = class_double, y = class_double))
177+
foo <- new_class("foo", properties = list(x = class_double, y = class_double),
178+
package = NULL)
178179
foo()
179180
Output
180181
<foo>
@@ -191,7 +192,7 @@
191192
# S7 object: displays objects with data nicely
192193

193194
Code
194-
text <- new_class("text", class_character)
195+
text <- new_class("text", class_character, package = NULL)
195196
text("x")
196197
Output
197198
<text> chr "x"

tests/testthat/_snaps/inherits.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
# throws informative error
1010

1111
Code
12-
foo1 <- new_class("foo1")
13-
foo2 <- new_class("foo2")
12+
foo1 <- new_class("foo1", package = NULL)
13+
foo2 <- new_class("foo2", package = NULL)
1414
check_is_S7(foo1(), foo2)
1515
Condition
1616
Error:

0 commit comments

Comments
 (0)