You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Oooh I wonder if this is #389 — it might be but that PR alone is not enough (and also it seems to cause a problem when unregistering methods with pkgload).
But running rm(`$`) after the method<- call fixes the problem, so that's almostly certainly the root cause.
You also get the correct result with evalq(bar()$x, asNamespace("s7bugreport")), suggesting that the root cause is that the S3method() directive in the namespace ends up registering the method for the wrong generic.
Ok so it looks like the mere presence of $ (even though after #389 it's no longer a function) in the package causes S3method() to register the method in the wrong environment 😬
This is similar to the problem we encountered with @ and S3 methods for @ being in the S7 namespace. We worked around it by being more specific in NAMESPACE:
I've found that this issue is not limited to just $, but essentially when both S7 and S3 methods are written for an S3 generic in a package. For example using print(), I run into a similar bug:
@teunbrand if you need a short-term work around, you can use rm(print) etc after each method creation.
And maybe we can generalise this by having method()<- return a sentinel object that we later delete. Unfortunately I think .onLoad() will be too late, because IIRC there you can redirect existing bindings but you can't add or delete.
@schloerke attended us on the use of local() to avoid attaching the function in tidyverse/ggplot2#6522. The benefit over rm() is that it doesn't throw a warning when this bug would get fixed.
@schloerke attended us on the use of local() to avoid attaching the function in tidyverse/ggplot2#6522. The benefit over rm() is that it doesn't throw a warning when this bug would get fixed.
Running into this issue today and was happy to stumble upon this thread. Thanks @teunbrand for the great tip. Wrapping method<- calls in local({ }) blocks did the trick.
Activity
hadley commentedon Nov 27, 2023
Oooh I wonder if this is #389 — it might be but that PR alone is not enough (and also it seems to cause a problem when unregistering methods with pkgload).
But running
rm(`$`)after themethod<-call fixes the problem, so that's almostly certainly the root cause.You also get the correct result with
evalq(bar()$x, asNamespace("s7bugreport")), suggesting that the root cause is that theS3method()directive in the namespace ends up registering the method for the wrong generic.teunbrand commentedon Nov 27, 2023
I quickly tried that PR and it doesn't seem to solve the interaction.
If I replace the
.onLoadof that package to explicitly register the S3 method:Then it behaves as it should, but I don't know enough about the innards of S7 to speculate why this works.
That does seem consistent with the observed behaviour.
hadley commentedon Nov 27, 2023
Ok so it looks like the mere presence of
$(even though after #389 it's no longer a function) in the package causesS3method()to register the method in the wrong environment 😬Minimal reprex of the S3 issue:
That means back to the drawing board.
t-kalinowski commentedon Nov 27, 2023
This is similar to the problem we encountered with
@and S3 methods for@being in the S7 namespace. We worked around it by being more specific in NAMESPACE:teunbrand commentedon Mar 6, 2025
FWIW it looks like I can work around this bug by using
S7::as usual, and using the following roxygen comment at the S3 method:method<-#' @rawNamespace S3method(base::`$`, S3_class_name)[-]Weird S7/S3 interaction in `$()`[/-][+]Weird S7/S3 interaction[/+]teunbrand commentedon Mar 14, 2025
I've found that this issue is not limited to just
$, but essentially when both S7 and S3 methods are written for an S3 generic in a package. For example usingprint(), I run into a similar bug:https://github.com/teunbrand/s7bugreport/tree/demonstrate-print
hadley commentedon Mar 17, 2025
@teunbrand if you need a short-term work around, you can use
rm(print)etc after each method creation.And maybe we can generalise this by having
method()<-return a sentinel object that we later delete. Unfortunately I think.onLoad()will be too late, because IIRC there you can redirect existing bindings but you can't add or delete.teunbrand commentedon Jun 19, 2025
@schloerke attended us on the use of
local()to avoid attaching the function in tidyverse/ggplot2#6522. The benefit overrm()is that it doesn't throw a warning when this bug would get fixed.dgkf commentedon Jul 1, 2025
Running into this issue today and was happy to stumble upon this thread. Thanks @teunbrand for the great tip. Wrapping
method<-calls inlocal({ })blocks did the trick.hadley commentedon May 21, 2026
I'm pretty sure #364 is the root cause here
Explicitly register base generics to avoid S7 generic conflict