title: How to dynamically change the signature of a function without modifying the AST?
body: <p>I'm writing an equivalent of Python's <code>@dataclass</code> and I'm trying to monkeypatch the constructor of decorated classes. My decorator is currently as follows:</p>
  <pre class="lang-py prettyprint-override"><code>def decorator(decorated_class):
      def __init__(self, **kwargs):
          # do stuff
      decorated_class.__init__ = __init__
      return decorated_class
  </code></pre>
  <p>What I want to be able to do is dynamically set the arguments of the newly created <code>__init__</code> method based on the class attributes. For example say we have a class <code>Person</code>:</p>
  <pre class="lang-py prettyprint-override"><code>@decorator
  class Person:
      name: str
      age: int
  </code></pre>
  <p>My desired behaviour would be to dynamically generate the following constructor and monkey-patching it as I'm currently already doing in the decorator</p>
  <pre class="lang-py prettyprint-override"><code>def __init__(self, name, age):
      self.name = name
      self.age = age
  </code></pre>
  <p>This behaviour is probably achievable by messing around with the AST at runtime but it obviously comes at a performance price which I would like to avoid. Is there any way of doing this by (for example) interacting directly with the <code>function</code> object?</p>
  <p>Note: the solution provided by <a href="https://stackoverflow.com/questions/56546854/how-to-modify-the-signature-of-a-function-dynamically">this Stack Overflow post</a> is poorly described and doesn't run.</p>