From ccbd2d735d3a6068c52f0ced7e6f7e2645554635 Mon Sep 17 00:00:00 2001 From: Bar Harel Date: Fri, 7 Oct 2016 20:49:07 +0300 Subject: [PATCH] Added apply_defaults Just like the default inspect module but does not contain the bug they have for an empty bind_partial. --- funcsigs/__init__.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/funcsigs/__init__.py b/funcsigs/__init__.py index fd2f47b..4f7fb3c 100644 --- a/funcsigs/__init__.py +++ b/funcsigs/__init__.py @@ -440,6 +440,29 @@ def __eq__(self, other): def __ne__(self, other): return not self.__eq__(other) + def apply_defaults(self): + arguments = self.arguments + + # Creating a new one and not modifying in-place for thread safety. + new_arguments = [] + + for name, param in self._signature.parameters.items(): + try: + new_arguments.append((name, arguments[name])) + except KeyError: + if param.default is not _empty: + val = param.default + elif param.kind is _VAR_POSITIONAL: + val = () + elif param.kind is _VAR_KEYWORD: + val = {} + else: + # BoundArguments was likely created by bind_partial + continue + new_arguments.append((name, val)) + + self.arguments = OrderedDict(new_arguments) + class Signature(object): '''A Signature object represents the overall signature of a function.