◐ Shell
clean mode source ↗

gh-108901: Add `Signature.from_code` method by sobolevn · Pull Request #108902 · python/cpython

The more I think about this, the more concerned I am :/

The existing getargs() function that we have in inspect gives accurate information. It doesn't claim to be able to tell you everything about the signature: it only claims to be able to tell you some information about the signature:

  • It gives you a list of argument names
  • It tells you the name of the *vararg argument, if there is one
  • It tells you the name of the **kwarg argument, if there is one

This new function, however, claims to be able to give you a complete and accurate signature from a code object (in that it constructs a Signature object from a code object). That's impossible, since code objects don't have information about which arguments have default values, and so the function gives you inaccurate information.

I'm not sure having a prominent note in the docs is sufficient to fix this issue: adding a new constructor that we know gives inaccurate results seems very troubling to me. Perhaps a better solution would be to document getargs and promote it to public API?