getargspec.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. """
  2. getargspec excerpted from:
  3. sphinx.util.inspect
  4. ~~~~~~~~~~~~~~~~~~~
  5. Helpers for inspecting Python modules.
  6. :copyright: Copyright 2007-2015 by the Sphinx team, see AUTHORS.
  7. :license: BSD, see LICENSE for details.
  8. """
  9. from __future__ import annotations
  10. import inspect
  11. from functools import partial
  12. from typing import Any
  13. # Unmodified from sphinx below this line
  14. def getargspec(func: Any) -> inspect.FullArgSpec:
  15. """Like inspect.getargspec but supports functools.partial as well."""
  16. if inspect.ismethod(func):
  17. func = func.__func__
  18. if type(func) is partial:
  19. orig_func = func.func
  20. argspec = getargspec(orig_func)
  21. args = list(argspec[0])
  22. defaults = list(argspec[3] or ())
  23. kwoargs = list(argspec[4])
  24. kwodefs = dict(argspec[5] or {})
  25. if func.args:
  26. args = args[len(func.args) :]
  27. for arg in func.keywords or ():
  28. try:
  29. i = args.index(arg) - len(args)
  30. del args[i]
  31. try:
  32. del defaults[i]
  33. except IndexError:
  34. pass
  35. except ValueError: # must be a kwonly arg
  36. i = kwoargs.index(arg)
  37. del kwoargs[i]
  38. del kwodefs[arg]
  39. return inspect.FullArgSpec(
  40. args, argspec[1], argspec[2], tuple(defaults), kwoargs, kwodefs, argspec[6]
  41. )
  42. while hasattr(func, "__wrapped__"):
  43. func = func.__wrapped__
  44. if not inspect.isfunction(func):
  45. raise TypeError("%r is not a Python function" % func)
  46. return inspect.getfullargspec(func)