diff options
| author | Yury Selivanov <yselivanov@sprymix.com> | 2015-05-16 17:45:09 (GMT) |
|---|---|---|
| committer | Yury Selivanov <yselivanov@sprymix.com> | 2015-05-16 17:45:09 (GMT) |
| commit | b907a513c8285410d93adc2895508985838fe1a7 (patch) | |
| tree | 80450ad00573547efc68174d32388ce1b140bbfc /Lib/inspect.py | |
| parent | 1392f71c3927c9d81969200f5dfff9fb832cde0b (diff) | |
| download | cpython-b907a513c8285410d93adc2895508985838fe1a7.zip cpython-b907a513c8285410d93adc2895508985838fe1a7.tar.gz cpython-b907a513c8285410d93adc2895508985838fe1a7.tar.bz2 | |
Issue 24190: Add inspect.BoundArguments.apply_defaults() method.
Diffstat (limited to 'Lib/inspect.py')
| -rw-r--r-- | Lib/inspect.py | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py index 9389f3b..8d2920a 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -2443,6 +2443,36 @@ class BoundArguments: return kwargs + def apply_defaults(self): + """Set default values for missing arguments. + + For variable-positional arguments (*args) the default is an + empty tuple. + + For variable-keyword arguments (**kwargs) the default is an + empty dict. + """ + arguments = self.arguments + if not arguments: + return + 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: + # This BoundArguments was likely produced by + # Signature.bind_partial(). + continue + new_arguments.append((name, val)) + self.arguments = OrderedDict(new_arguments) + def __eq__(self, other): return (self is other or (issubclass(other.__class__, BoundArguments) and |
