Skip to content

Weird S7/S3 interaction #390

Closed
Closed

Description

@teunbrand

I found that defining a $-method for an S7 class negates another $-method for an S3 class.
I could not reproduce this in an interactive session, so I build a small package to demonstrate the weird interaction.
Please see the README of https://github.com/teunbrand/s7bugreport/ for details.

Activity

  1. hadley commented on Nov 27, 2023

    @hadley
    Member

    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.

  2. teunbrand commented on Nov 27, 2023

    @teunbrand
    Author

    I quickly tried that PR and it doesn't seem to solve the interaction.
    If I replace the .onLoad of that package to explicitly register the S3 method:

    .onLoad <- function(...) {
      methods_register()
      registerS3method("$", "bar", `$.bar`)
    }

    Then it behaves as it should, but I don't know enough about the innards of S7 to speculate why this works.

    the root cause is that the S3method() directive in the namespace ends up registering the method for the wrong generic.

    That does seem consistent with the observed behaviour.

  3. hadley commented on Nov 27, 2023

    @hadley
    Member

    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 😬

    Minimal reprex of the S3 issue:

    #' A foo object
    #' @export
    foo <- function(x) {
      structure(list(), class = "foo")
    }
    
    #' @export
    `mean.foo` <- function(...) {
      "FOO"
    }
    
    `mean` <- NULL

    That means back to the drawing board.

  4. t-kalinowski commented on Nov 27, 2023

    @t-kalinowski
    Member

    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:

    S3method(base::`@`, S7_object)
  5. added this to the v0.2.0 milestone on Nov 27, 2023
  6. modified the milestones: v0.2.0, v0.3.0 on Nov 5, 2024
  7. teunbrand commented on Mar 6, 2025

    @teunbrand
    Author

    FWIW it looks like I can work around this bug by using S7::method<- as usual, and using the following roxygen comment at the S3 method:

    #' @rawNamespace S3method(base::`$`, S3_class_name)
  8. changed the title [-]Weird S7/S3 interaction in `$()`[/-] [+]Weird S7/S3 interaction[/+] on Mar 14, 2025
  9. teunbrand commented on Mar 14, 2025

    @teunbrand
    Author

    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:

    https://github.com/teunbrand/s7bugreport/tree/demonstrate-print

  10. hadley commented on Mar 17, 2025

    @hadley
    Member

    @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.

  11. teunbrand commented on Jun 19, 2025

    @teunbrand
    Author

    @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.

  12. dgkf commented on Jul 1, 2025

    @dgkf
    Contributor

    @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.

  13. added
    bug
    compatibility 👬
    on May 21, 2026
  14. hadley commented on May 21, 2026

    @hadley
    Member

    I'm pretty sure #364 is the root cause here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcompatibility 👬registration 📓

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions