From e4e0c1557d86559398743cca30aab33920133d27 Mon Sep 17 00:00:00 2001 From: Apostol Fet Date: Wed, 4 Dec 2024 23:46:46 +0300 Subject: [PATCH] Added handling of cases with double *args and **kwargs addition --- src/dishka/integrations/base.py | 36 +++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/src/dishka/integrations/base.py b/src/dishka/integrations/base.py index 92728bac..751a40d0 100644 --- a/src/dishka/integrations/base.py +++ b/src/dishka/integrations/base.py @@ -226,7 +226,7 @@ def auto_injected_func(*args: P.args, **kwargs: P.kwargs) -> T: def _add_params( - params: list[Parameter], + params: Sequence[Parameter], additional_params: Sequence[Parameter], ): params_kind_dict: dict[_ParameterKind, list[Parameter]] = {} @@ -237,13 +237,33 @@ def _add_params( for param in additional_params: params_kind_dict.setdefault(param.kind, []).append(param) - result_params = [] - result_params.extend(params_kind_dict.get(Parameter.POSITIONAL_ONLY, [])) - result_params.extend( - params_kind_dict.get(Parameter.POSITIONAL_OR_KEYWORD, []), + + var_positional = params_kind_dict.get(Parameter.VAR_POSITIONAL, []) + if len(var_positional) > 1: + param_names = (param.name for param in var_positional) + var_positional_names = ", *".join(param_names) + base_msg = "more than one var positional parameter: *" + msg = base_msg + var_positional_names + raise ValueError(msg) + + var_keyword = params_kind_dict.get(Parameter.VAR_KEYWORD, []) + if len(var_keyword) > 1: + var_keyword_names = ", **".join(param.name for param in var_keyword) + msg = "more than one var keyword parameter: " + var_keyword_names + raise ValueError(msg) + + positional_only = params_kind_dict.get(Parameter.POSITIONAL_ONLY, []) + positional_or_keyword = params_kind_dict.get( + Parameter.POSITIONAL_OR_KEYWORD, + [], ) - result_params.extend(params_kind_dict.get(Parameter.VAR_POSITIONAL, [])) - result_params.extend(params_kind_dict.get(Parameter.KEYWORD_ONLY, [])) - result_params.extend(params_kind_dict.get(Parameter.VAR_KEYWORD, [])) + keyword_only = params_kind_dict.get(Parameter.KEYWORD_ONLY, []) + + result_params = [] + result_params.extend(positional_only) + result_params.extend(positional_or_keyword) + result_params.extend(var_positional) + result_params.extend(keyword_only) + result_params.extend(var_keyword) return result_params