Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/traitlets/utils/getargspec.py: 15%
34 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-20 06:09 +0000
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-20 06:09 +0000
1"""
2 getargspec excerpted from:
4 sphinx.util.inspect
5 ~~~~~~~~~~~~~~~~~~~
6 Helpers for inspecting Python modules.
7 :copyright: Copyright 2007-2015 by the Sphinx team, see AUTHORS.
8 :license: BSD, see LICENSE for details.
9"""
10from __future__ import annotations
12import inspect
13from functools import partial
14from typing import Any
16# Unmodified from sphinx below this line
19def getargspec(func: Any) -> inspect.FullArgSpec:
20 """Like inspect.getargspec but supports functools.partial as well."""
21 if inspect.ismethod(func):
22 func = func.__func__
23 if type(func) is partial:
24 orig_func = func.func
25 argspec = getargspec(orig_func)
26 args = list(argspec[0])
27 defaults = list(argspec[3] or ())
28 kwoargs = list(argspec[4])
29 kwodefs = dict(argspec[5] or {})
30 if func.args:
31 args = args[len(func.args) :]
32 for arg in func.keywords or ():
33 try:
34 i = args.index(arg) - len(args)
35 del args[i]
36 try:
37 del defaults[i]
38 except IndexError:
39 pass
40 except ValueError: # must be a kwonly arg
41 i = kwoargs.index(arg)
42 del kwoargs[i]
43 del kwodefs[arg]
44 return inspect.FullArgSpec(
45 args, argspec[1], argspec[2], tuple(defaults), kwoargs, kwodefs, argspec[6]
46 )
47 while hasattr(func, "__wrapped__"):
48 func = func.__wrapped__
49 if not inspect.isfunction(func):
50 raise TypeError("%r is not a Python function" % func)
51 return inspect.getfullargspec(func)