title: get function signature from FrameInfo or frame in python
body: <p>While writing my own exception-hook in python, I came to the idea of using the <code>inspect</code>-module to provide myselfe more information about how the function got called.</p>
  
  <p>This means the <strong>signature</strong> of the function as well as the <strong>arguments</strong> passed to it.</p>
  
  <pre><code>import inspect
  
  frame_infos = inspect.trace() # get the FrameInfos
  
  for f_idx, f_info in enumerate(frame_infos):
      frame_dict.update(f_info.frame.f_locals) # update namespace with deeper frame levels
      #Output basic Error-Information
      print(f'  File "{f_info.filename}", line {f_info.lineno}, in {f_info.function}')
      for line in f_info.code_context:
          print(f'    {line.strip()}')
  
      ########################################################
      # show signature and arguments 1 level deeper
      if f_idx+1 &lt; len(frame_infos): 
          func_name = frame_infos[f_idx+1].function #name of the function
          try:
              func_ref = frame_dict[func_name] # look up in namespace
              sig = inspect.signature(func_ref) # call signature for function_reference
          except KeyError: sig = '(signature unknown)'
          print(f'    {func_name} {sig}\n')
          print(f'    {frame_infos[f_idx+1].frame.f_locals}\n')
  </code></pre>
  
  <p>This works pretty fine with a basic example like this:</p>
  
  <pre><code>def test1 ( x: int, y: tuple = 0 )-&gt;list: # the types obviously dont match
      return test2(y, b=x, help=0)
  
  def test2 ( a, *args, b, **kwargs ):
      return a + b / 0
  
  try: 
      test1(5) 
  except: ...
  </code></pre>
  
  <p>output:</p>
  
  <pre><code>File "C:/test/errorHandler.py", line 136, in &lt;module&gt;
      test1(5)
      test1 (x:int, y:tuple=0) -&gt; list
      {'y': 0, 'x': 5}
  File "C:/test/errorHandler.py", line 130, in test1
      return test2(y, b=x, help=0)
      test2 (a, *args, b, **kwargs)
      {'kwargs': {'help': 0}, 'args': (), 'b': 5, 'a': 0}
  File "C:/test/errorHandler.py", line 133, in test2
      return a + b / 0
  </code></pre>
  
  <p>but, as soon as you are leaving 1 file you can't map a functions name against the basic namespace.</p>
  
  <blockquote>
    <p>file1: <code>import file2; try: file2.foo() except: ...</code><br>
    file2: <code>import file3; def foo(): file3.foo()</code><br>
    file3: <code>def foo(): return 0/0</code></p>
  </blockquote>
  
  <p>so essentialy, I'm looking for a way to get a function (like <code>&lt;function foo at 0x000002F4A43ACD08&gt;</code>) from a <code>FrameInfo</code> or <code>frame</code>-object, but the only information that I see is the name and the file and line. <br>
  (I don't like the idea of getting the signature by looking in the sourcefile at a specific line.)</p>
  
  <p>Best reference so far was the <a href="https://docs.python.org/library/inspect.html" rel="nofollow noreferrer"><code>Inspect</code>-documentation</a>, but I havent' found something usefull yet.</p>