MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / _get_arg_spec

Function _get_arg_spec

tensorflow/tools/docs/parser.py:649–697  ·  view source on GitHub ↗

Extracts signature information from a function or functools.partial object. For functions, uses `tf_inspect.getfullargspec`. For `functools.partial` objects, corrects the signature of the underlying function to take into account the removed arguments. Args: func: A function whose signa

(func)

Source from the content-addressed store, hash-verified

647
648
649def _get_arg_spec(func):
650 """Extracts signature information from a function or functools.partial object.
651
652 For functions, uses `tf_inspect.getfullargspec`. For `functools.partial`
653 objects, corrects the signature of the underlying function to take into
654 account the removed arguments.
655
656 Args:
657 func: A function whose signature to extract.
658
659 Returns:
660 An `FullArgSpec` namedtuple `(args, varargs, varkw, defaults, etc.)`,
661 as returned by `tf_inspect.getfullargspec`.
662 """
663 # getfullargspec does not work for functools.partial objects directly.
664 if isinstance(func, functools.partial):
665 argspec = tf_inspect.getfullargspec(func.func)
666 # Remove the args from the original function that have been used up.
667 first_default_arg = (
668 len(argspec.args or []) - len(argspec.defaults or []))
669 partial_args = len(func.args)
670 argspec_args = []
671
672 if argspec.args:
673 argspec_args = list(argspec.args[partial_args:])
674
675 argspec_defaults = list(argspec.defaults or ())
676 if argspec.defaults and partial_args > first_default_arg:
677 argspec_defaults = list(argspec.defaults[partial_args-first_default_arg:])
678
679 first_default_arg = max(0, first_default_arg - partial_args)
680 for kwarg in (func.keywords or []):
681 if kwarg in (argspec.args or []):
682 i = argspec_args.index(kwarg)
683 argspec_args.pop(i)
684 if i >= first_default_arg:
685 argspec_defaults.pop(i-first_default_arg)
686 else:
687 first_default_arg -= 1
688 return tf_inspect.FullArgSpec(
689 args=argspec_args,
690 varargs=argspec.varargs,
691 varkw=argspec.varkw,
692 defaults=tuple(argspec_defaults),
693 kwonlyargs=[],
694 kwonlydefaults=None,
695 annotations={})
696 else: # Regular function or method, getargspec will work fine.
697 return tf_inspect.getfullargspec(func)
698
699
700def _remove_first_line_indent(string):

Callers 1

_generate_signatureFunction · 0.70

Calls 4

tupleFunction · 0.85
maxFunction · 0.50
indexMethod · 0.45
popMethod · 0.45

Tested by

no test coverage detected