From 93316ba8f1873b4627c2ef3a9292828c414a6791 Mon Sep 17 00:00:00 2001 From: Sylvain MARIE Date: Tue, 5 Nov 2019 12:06:16 +0100 Subject: [PATCH] `add_signature_parameters` now accepts that one specifies a custom index where to insert the new parameters. --- makefun/main.py | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/makefun/main.py b/makefun/main.py index 7cca05b..1a35366 100644 --- a/makefun/main.py +++ b/makefun/main.py @@ -811,22 +811,41 @@ def remove_signature_parameters(s, *param_names): return s.replace(parameters=params.values()) -def add_signature_parameters(s, # type: Signature - first=(), # type: Union[Parameter, Iterable[Parameter]] - last=(), # type: Union[Parameter, Iterable[Parameter]] +def add_signature_parameters(s, # type: Signature + first=(), # type: Union[Parameter, Iterable[Parameter]] + last=(), # type: Union[Parameter, Iterable[Parameter]] + custom=(), # type: Union[Parameter, Iterable[Parameter]] + custom_idx=-1 # type: int ): """ - Adds the provided parameters to the signature s (returns a new signature instance). + Adds the provided parameters to the signature `s` (returns a new signature instance). - :param s: + :param s: the original signature to edit :param first: a single element or a list of `Parameter` instances to be added at the beginning of the parameter's list :param last: a single element or a list of `Parameter` instances to be added at the end of the parameter's list - :return: + :param custom: a single element or a list of `Parameter` instances to be added at a custom position in the list. + that position is determined with `custom_idx` + :param custom_idx: the custom position to insert the `custom` parameters to. + :return: a new signature created from the original one by adding the specified parameters. """ params = OrderedDict(s.parameters.items()) lst = list(params.values()) + # insert at custom position (but keep the order, that's why we use 'reversed') + try: + for param in reversed(custom): + if param.name in params: + raise ValueError("Parameter with name '%s' is present twice in the signature to create" % param.name) + else: + lst.insert(custom_idx, param) + except TypeError: + # a single argument + if custom.name in params: + raise ValueError("Parameter with name '%s' is present twice in the signature to create" % custom.name) + else: + lst.insert(custom_idx, custom) + # prepend but keep the order try: for param in reversed(first):