Skip to content

Commit

Permalink
Show generic signature in error message (#482)
Browse files Browse the repository at this point in the history
* Show generic signature in error message

closes #480

* show method formals in error message

* Update R/method-register.R

Co-authored-by: Hadley Wickham <[email protected]>

* update snapshot

---------

Co-authored-by: Hadley Wickham <[email protected]>
  • Loading branch information
t-kalinowski and hadley authored Oct 29, 2024
1 parent 70d1c85 commit 5935df1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
7 changes: 6 additions & 1 deletion R/method-register.R
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,14 @@ check_method <- function(method, generic, name = paste0(generic@name, "(???)"))

if (!"..." %in% generic_args && !identical(generic_formals, method_formals)) {
msg <- sprintf(
"%s() lacks `...` so method formals must match generic formals exactly",
"%s() generic lacks `...` so method formals must match generic formals exactly.",
generic@name
)
bullets <- c(
sprintf("- generic formals: %s", show_args(generic_formals, name = generic@name)),
sprintf("- method formals: %s", show_args(method_formals, name = generic@name))
)
msg <- paste0(c(msg, bullets), collapse = "\n")
stop(msg, call. = FALSE)
}

Expand Down
4 changes: 3 additions & 1 deletion tests/testthat/_snaps/method-register.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@
check_method(function(x, y) { }, foo)
Condition
Error:
! foo() lacks `...` so method formals must match generic formals exactly
! foo() generic lacks `...` so method formals must match generic formals exactly.
- generic formals: foo(x)
- method formals: foo(x, y)

# check_method warn if default arguments don't match

Expand Down

0 comments on commit 5935df1

Please sign in to comment.