Skip to content

Documenting S7 methods raises code/documentation mismatch warnings #725

Open

Description

@mitchelloharawild

Originally posted in r-lib/roxygen2#1872

Documenting S7 methods with additional non-generic arguments triggers CRAN check warnings since the methods themselves are invisible to the checks. I don't see an elegant fix for this without R changes that make the CRAN checks aware of S7 method arguments. An inelegant solution could be to separately document the S7 method usage and arguments in an Rd section (e.g. Methods) which is outside the attention of CRAN checks.

MRE:

#' Size of an object
#'
#' @description
#' `size()` determines the size of an object.
#'
#' # Methods
#' `size()` is an S7 generic with methods available for the following
#' classes:
#'
#' `r doclisting::methods_list("size")`
#'
#' @param x An object.
#' @param ... Not used.
#' @returns A single number.
#' 
#' @export
size <- S7::new_generic("size", "x")

#' @rdname size
#' @param na.rm If TRUE, missing values are not included in the calculation of the size of an object.
S7::method(size, S7::class_any) <- function(x, ..., na.rm = FALSE) {
  if (na.rm) x <- x[!is.na(x)]
  length(x)
}

Produces the CRAN check warning:

❯ checking for code/documentation mismatches ... WARNING
  Codoc mismatches from Rd file 'size.Rd':
  size
    Code: function(x, ...)
    Docs: function(x, ..., na.rm = FALSE)
    Argument names in docs not in code:
      na.rm

The tools::codoc() function that detects these problems has special handling for finding S3 and S4 methods, and likely also needs special handling of S7 methods to allow them to be documented.

Activity

  1. t-kalinowski commented on Sep 30, 2026

    @t-kalinowski
    Member

    I looked into this. A complete fix still seems to need changes in R, though there may be a small workaround we could make in roxygen2.

    R already supports \special{...} inside \usage:

    \usage{
    size(x, ...)
    \special{size(x, ..., na.rm = FALSE)}
    }

    R displays the method usage normally but skips it when checking the documentation. I tried this on R 4.6.1, and it removed the warning. The generic's usage still gets checked.

    We could have roxygen2 emit this for S7 methods for now. The downside is that R would also miss incorrect method defaults and missing argument documentation.

    For a complete fix, we could propose a \S7method{size}{any} tag in R. That would tell R which method the usage belongs to, so it could compare the documentation with the method's actual arguments. Roxygen2 would emit the tag, and S7 would provide a way for R to find the registered methods.

    S7's existing registries seem to have the information we need. The main things to work out would be how to identify methods with multiple dispatch or classes from other packages, and what to do on older R versions. I don't think we need to tackle that now, but it seems like a reasonable direction for a fix.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions