`setGeneric` throws error


I want to ask a question about methods::setGeneric. First, I want to say that my use of this method is not common. However, I'm looking for a way to make it works, so any help would be greatly appreciated.

The minimum reproduce case I can think of is:

  1. Within one of my R package, expose a function like:
callSetGeneric <- function(fName, fDef) {
  setGeneric(name = fName, def = fDef)
  1. load this package, and call the function:
> callSetGeneric("square", function(x) x^2)
 Error in assign(name, fdef, where) : 
  cannot add bindings to a locked environment 

I have tried with defining the package param for setGeneric, but still get the same error.
In my actual use case, the method definition def is defined in the same environment, right before setGeneric is called.

More on why I am doing this: I am extending an R package to add the ability to auto generate R wrappers for any Python module (already installed and on search path).